Merge pull request #12569 from civicrm/5.4
[civicrm-core.git] / CRM / Utils / Cache / NaiveMultipleTrait.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2018
32 *
33 * The traditional CRM_Utils_Cache_Interface did not support multiple-key
34 * operations. To get drop-in compliance with PSR-16, we use a naive adapter.
35 * An operation like `getMultiple()` just calls `get()` multiple times.
36 *
37 * Ideally, these should be replaced with more performant/native versions.
38 */
39 trait CRM_Utils_Cache_NaiveMultipleTrait {
40
41 /**
42 * Obtains multiple cache items by their unique keys.
43 *
44 * @param iterable $keys A list of keys that can obtained in a single operation.
45 * @param mixed $default Default value to return for keys that do not exist.
46 *
47 * @return iterable A list of key => value pairs. Cache keys that do not exist or are stale will have $default as value.
48 *
49 * @throws \Psr\SimpleCache\InvalidArgumentException
50 * MUST be thrown if $keys is neither an array nor a Traversable,
51 * or if any of the $keys are not a legal value.
52 */
53 public function getMultiple($keys, $default = NULL) {
54 $this->assertIterable('getMultiple', $keys);
55
56 $result = [];
57 foreach ($keys as $key) {
58 $result[$key] = $this->get($key, $default);
59 }
60 return $result;
61 }
62
63 /**
64 * Persists a set of key => value pairs in the cache, with an optional TTL.
65 *
66 * @param iterable $values A list of key => value pairs for a multiple-set operation.
67 * @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
68 * the driver supports TTL then the library may set a default value
69 * for it or let the driver take care of that.
70 *
71 * @return bool True on success and false on failure.
72 *
73 * @throws \Psr\SimpleCache\InvalidArgumentException
74 * MUST be thrown if $values is neither an array nor a Traversable,
75 * or if any of the $values are not a legal value.
76 */
77 public function setMultiple($values, $ttl = NULL) {
78 $this->assertIterable('setMultiple', $values);
79
80 $result = TRUE;
81 foreach ($values as $key => $value) {
82 if (is_int($key)) {
83 $key = (string) $key;
84 }
85 $result = $this->set($key, $value, $ttl) || $result;
86 }
87 return $result;
88 }
89
90 /**
91 * Deletes multiple cache items in a single operation.
92 *
93 * @param iterable $keys A list of string-based keys to be deleted.
94 *
95 * @return bool True if the items were successfully removed. False if there was an error.
96 *
97 * @throws \Psr\SimpleCache\InvalidArgumentException
98 * MUST be thrown if $keys is neither an array nor a Traversable,
99 * or if any of the $keys are not a legal value.
100 */
101 public function deleteMultiple($keys) {
102 $this->assertIterable('deleteMultiple', $keys);
103
104 $result = TRUE;
105 foreach ($keys as $key) {
106 $result = $this->delete($key) || $result;
107 }
108 return $result;
109 }
110
111 /**
112 * @param $keys
113 * @throws \CRM_Utils_Cache_InvalidArgumentException
114 */
115 private function assertIterable($func, $keys) {
116 if (!is_array($keys) && !($keys instanceof Traversable)) {
117 throw new CRM_Utils_Cache_InvalidArgumentException("$func expects iterable input");
118 }
119 }
120
121 }