3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
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.
37 * Ideally, these should be replaced with more performant/native versions.
39 trait CRM_Utils_Cache_NaiveMultipleTrait
{
42 * Obtains multiple cache items by their unique keys.
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.
47 * @return iterable A list of key => value pairs. Cache keys that do not exist or are stale will have $default as value.
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.
53 public function getMultiple($keys, $default = NULL) {
54 $this->assertIterable('getMultiple', $keys);
57 foreach ($keys as $key) {
58 $result[$key] = $this->get($key, $default);
64 * Persists a set of key => value pairs in the cache, with an optional TTL.
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.
71 * @return bool True on success and false on failure.
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.
77 public function setMultiple($values, $ttl = NULL) {
78 $this->assertIterable('setMultiple', $values);
81 foreach ($values as $key => $value) {
85 $result = $this->set($key, $value, $ttl) ||
$result;
91 * Deletes multiple cache items in a single operation.
93 * @param iterable $keys A list of string-based keys to be deleted.
95 * @return bool True if the items were successfully removed. False if there was an error.
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.
101 public function deleteMultiple($keys) {
102 $this->assertIterable('deleteMultiple', $keys);
105 foreach ($keys as $key) {
106 $result = $this->delete($key) ||
$result;
113 * @throws \CRM_Utils_Cache_InvalidArgumentException
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");