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