Merge pull request #23912 from mlutfy/reportVars
[civicrm-core.git] / CRM / Utils / Cache / NaiveMultipleTrait.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
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.
19 *
20 * Ideally, these should be replaced with more performant/native versions.
21 */
22 trait CRM_Utils_Cache_NaiveMultipleTrait {
23
24 /**
25 * Obtains multiple cache items by their unique keys.
26 *
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.
29 *
30 * @return iterable A list of key => value pairs. Cache keys that do not exist or are stale will have $default as value.
31 *
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.
35 */
36 public function getMultiple($keys, $default = NULL) {
37 $this->assertIterable('getMultiple', $keys);
38
39 $result = [];
40 foreach ($keys as $key) {
41 $result[$key] = $this->get($key, $default);
42 }
43 return $result;
44 }
45
46 /**
47 * Persists a set of key => value pairs in the cache, with an optional TTL.
48 *
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.
53 *
54 * @return bool True on success and false on failure.
55 *
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.
59 */
60 public function setMultiple($values, $ttl = NULL) {
61 $this->assertIterable('setMultiple', $values);
62
63 $result = TRUE;
64 foreach ($values as $key => $value) {
65 if (is_int($key)) {
66 $key = (string) $key;
67 }
68 $result = $this->set($key, $value, $ttl) || $result;
69 }
70 return $result;
71 }
72
73 /**
74 * Deletes multiple cache items in a single operation.
75 *
76 * @param iterable $keys A list of string-based keys to be deleted.
77 *
78 * @return bool True if the items were successfully removed. False if there was an error.
79 *
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.
83 */
84 public function deleteMultiple($keys) {
85 $this->assertIterable('deleteMultiple', $keys);
86
87 $result = TRUE;
88 foreach ($keys as $key) {
89 $result = $this->delete($key) || $result;
90 }
91 return $result;
92 }
93
94 /**
95 * @param $func
96 * @param $keys
97 * @throws \CRM_Utils_Cache_InvalidArgumentException
98 */
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");
102 }
103 }
104
105 }