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
17 * The traditional CRM_Utils_Cache_Interface did not support multiple-key
18 * operations. To get drop-in compliance with PSR-16, we use a naive adapter.
19 * An operation like `getMultiple()` just calls `get()` multiple times.
21 * Ideally, these should be replaced with more performant/native versions.
23 trait CRM_Utils_Cache_NaiveMultipleTrait
{
26 * Obtains multiple cache items by their unique keys.
28 * @param iterable $keys A list of keys that can obtained in a single operation.
29 * @param mixed $default Default value to return for keys that do not exist.
31 * @return iterable A list of key => value pairs. Cache keys that do not exist or are stale will have $default as value.
33 * @throws \Psr\SimpleCache\InvalidArgumentException
34 * MUST be thrown if $keys is neither an array nor a Traversable,
35 * or if any of the $keys are not a legal value.
37 public function getMultiple($keys, $default = NULL) {
38 $this->assertIterable('getMultiple', $keys);
41 foreach ($keys as $key) {
42 $result[$key] = $this->get($key, $default);
48 * Persists a set of key => value pairs in the cache, with an optional TTL.
50 * @param iterable $values A list of key => value pairs for a multiple-set operation.
51 * @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
52 * the driver supports TTL then the library may set a default value
53 * for it or let the driver take care of that.
55 * @return bool True on success and false on failure.
57 * @throws \Psr\SimpleCache\InvalidArgumentException
58 * MUST be thrown if $values is neither an array nor a Traversable,
59 * or if any of the $values are not a legal value.
61 public function setMultiple($values, $ttl = NULL) {
62 $this->assertIterable('setMultiple', $values);
65 foreach ($values as $key => $value) {
69 $result = $this->set($key, $value, $ttl) ||
$result;
75 * Deletes multiple cache items in a single operation.
77 * @param iterable $keys A list of string-based keys to be deleted.
79 * @return bool True if the items were successfully removed. False if there was an error.
81 * @throws \Psr\SimpleCache\InvalidArgumentException
82 * MUST be thrown if $keys is neither an array nor a Traversable,
83 * or if any of the $keys are not a legal value.
85 public function deleteMultiple($keys) {
86 $this->assertIterable('deleteMultiple', $keys);
89 foreach ($keys as $key) {
90 $result = $this->delete($key) ||
$result;
98 * @throws \CRM_Utils_Cache_InvalidArgumentException
100 private function assertIterable($func, $keys) {
101 if (!is_array($keys) && !($keys instanceof Traversable
)) {
102 throw new CRM_Utils_Cache_InvalidArgumentException("$func expects iterable input");