3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 * The traditional CRM_Utils_Cache_Interface did not support multiple-key
17 * operations. To get drop-in compliance with PSR-16, we use a naive adapter.
18 * An operation like `getMultiple()` just calls `get()` multiple times.
20 * Ideally, these should be replaced with more performant/native versions.
22 trait CRM_Utils_Cache_NaiveMultipleTrait
{
25 * Obtains multiple cache items by their unique keys.
27 * @param iterable $keys A list of keys that can obtained in a single operation.
28 * @param mixed $default Default value to return for keys that do not exist.
30 * @return iterable A list of key => value pairs. Cache keys that do not exist or are stale will have $default as value.
32 * @throws \Psr\SimpleCache\InvalidArgumentException
33 * MUST be thrown if $keys is neither an array nor a Traversable,
34 * or if any of the $keys are not a legal value.
36 public function getMultiple($keys, $default = NULL) {
37 $this->assertIterable('getMultiple', $keys);
40 foreach ($keys as $key) {
41 $result[$key] = $this->get($key, $default);
47 * Persists a set of key => value pairs in the cache, with an optional TTL.
49 * @param iterable $values A list of key => value pairs for a multiple-set operation.
50 * @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
51 * the driver supports TTL then the library may set a default value
52 * for it or let the driver take care of that.
54 * @return bool True on success and false on failure.
56 * @throws \Psr\SimpleCache\InvalidArgumentException
57 * MUST be thrown if $values is neither an array nor a Traversable,
58 * or if any of the $values are not a legal value.
60 public function setMultiple($values, $ttl = NULL) {
61 $this->assertIterable('setMultiple', $values);
64 foreach ($values as $key => $value) {
68 $result = $this->set($key, $value, $ttl) ||
$result;
74 * Deletes multiple cache items in a single operation.
76 * @param iterable $keys A list of string-based keys to be deleted.
78 * @return bool True if the items were successfully removed. False if there was an error.
80 * @throws \Psr\SimpleCache\InvalidArgumentException
81 * MUST be thrown if $keys is neither an array nor a Traversable,
82 * or if any of the $keys are not a legal value.
84 public function deleteMultiple($keys) {
85 $this->assertIterable('deleteMultiple', $keys);
88 foreach ($keys as $key) {
89 $result = $this->delete($key) ||
$result;
97 * @throws \CRM_Utils_Cache_InvalidArgumentException
99 private function assertIterable($func, $keys) {
100 if (!is_array($keys) && !($keys instanceof Traversable
)) {
101 throw new CRM_Utils_Cache_InvalidArgumentException("$func expects iterable input");