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 +--------------------------------------------------------------------+
29 * Class CRM_Core_PrevNextCache_Memory
31 * Store the previous/next cache in a Redis set.
33 * Each logical prev-next cache corresponds to three distinct items in Redis:
34 * - "{prefix}/{qfKey}/list" - Sorted set of `entity_id`, with all entities
35 * - "{prefix}/{qfkey}/sel" - Sorted set of `entity_id`, with only entities marked by user
36 * - "{prefix}/{qfkey}/data" - Hash mapping from `entity_id` to `data`
38 * @link https://github.com/phpredis/phpredis
40 class CRM_Core_PrevNextCache_Redis
implements CRM_Core_PrevNextCache_Interface
{
55 * CRM_Core_PrevNextCache_Redis constructor.
56 * @param array $settings
58 public function __construct($settings) {
59 $this->redis
= CRM_Utils_Cache_Redis
::connect($settings);
60 $this->prefix
= isset($settings['prefix']) ?
$settings['prefix'] : '';
61 $this->prefix
.= \CRM_Utils_Cache
::DELIMITER
. 'prevnext' . \CRM_Utils_Cache
::DELIMITER
;
64 public function fillWithSql($cacheKey, $sql, $sqlParams = []) {
65 $dao = CRM_Core_DAO
::executeQuery($sql, $sqlParams, FALSE, NULL, FALSE, TRUE, TRUE);
66 if (is_a($dao, 'DB_Error')) {
67 throw new CRM_Core_Exception($dao->message
);
70 list($allKey, $dataKey, , $maxScore) = $this->initCacheKey($cacheKey);
72 while ($dao->fetch()) {
73 list (, $entity_id, $data) = array_values($dao->toArray());
75 $this->redis
->zAdd($allKey, $maxScore, $entity_id);
76 $this->redis
->hSet($dataKey, $entity_id, $data);
82 public function fillWithArray($cacheKey, $rows) {
83 list($allKey, $dataKey, , $maxScore) = $this->initCacheKey($cacheKey);
85 foreach ($rows as $row) {
87 $this->redis
->zAdd($allKey, $maxScore, $row['entity_id1']);
88 $this->redis
->hSet($dataKey, $row['entity_id1'], $row['data']);
94 public function fetch($cacheKey, $offset, $rowCount) {
95 $allKey = $this->key($cacheKey, 'all');
96 return $this->redis
->zRange($allKey, $offset, $offset +
$rowCount - 1);
99 public function markSelection($cacheKey, $action, $ids = NULL) {
100 $allKey = $this->key($cacheKey, 'all');
101 $selKey = $this->key($cacheKey, 'sel');
103 if ($action === 'select') {
104 foreach ((array) $ids as $id) {
105 $score = $this->redis
->zScore($allKey, $id);
106 $this->redis
->zAdd($selKey, $score, $id);
109 elseif ($action === 'unselect' && $ids === NULL) {
110 $this->redis
->delete($selKey);
111 $this->redis
->setTimeout($selKey, self
::TTL
);
113 elseif ($action === 'unselect' && $ids !== NULL) {
114 foreach ((array) $ids as $id) {
115 $this->redis
->zDelete($selKey, $id);
120 public function getSelection($cacheKey, $action = 'get') {
121 $allKey = $this->key($cacheKey, 'all');
122 $selKey = $this->key($cacheKey, 'sel');
124 if ($action === 'get') {
126 foreach ($this->redis
->zRange($selKey, 0, -1) as $entity_id) {
127 $result[$entity_id] = 1;
129 return [$cacheKey => $result];
131 elseif ($action === 'getall') {
133 foreach ($this->redis
->zRange($allKey, 0, -1) as $entity_id) {
134 $result[$entity_id] = 1;
136 return [$cacheKey => $result];
139 throw new \
CRM_Core_Exception("Unrecognized action: $action");
143 public function getPositions($cacheKey, $id1) {
144 $allKey = $this->key($cacheKey, 'all');
145 $dataKey = $this->key($cacheKey, 'data');
147 $rank = $this->redis
->zRank($allKey, $id1);
148 if (!is_int($rank) ||
$rank < 0) {
149 return ['foundEntry' => 0];
152 $pos = ['foundEntry' => 1];
156 foreach ($this->redis
->zRange($allKey, $rank - 1, $rank - 1) as $value) {
157 $pos['prev']['id1'] = $value;
159 $pos['prev']['data'] = $this->redis
->hGet($dataKey, $pos['prev']['id1']);
162 $count = $this->getCount($cacheKey);
163 if ($count > $rank +
1) {
165 foreach ($this->redis
->zRange($allKey, $rank +
1, $rank +
1) as $value) {
166 $pos['next']['id1'] = $value;
168 $pos['next']['data'] = $this->redis
->hGet($dataKey, $pos['next']['id1']);
174 public function deleteItem($id = NULL, $cacheKey = NULL) {
175 if ($id === NULL && $cacheKey !== NULL) {
176 // Delete by cacheKey.
177 $allKey = $this->key($cacheKey, 'all');
178 $selKey = $this->key($cacheKey, 'sel');
179 $dataKey = $this->key($cacheKey, 'data');
180 $this->redis
->delete($allKey, $selKey, $dataKey);
182 elseif ($id === NULL && $cacheKey === NULL) {
183 // Delete everything.
184 $keys = $this->redis
->keys($this->prefix
. '*');
185 $this->redis
->del($keys);
187 elseif ($id !== NULL && $cacheKey !== NULL) {
188 // Delete a specific contact, within a specific cache.
189 $this->redis
->zDelete($this->key($cacheKey, 'all'), $id);
190 $this->redis
->zDelete($this->key($cacheKey, 'sel'), $id);
191 $this->redis
->hDel($this->key($cacheKey, 'data'), $id);
193 elseif ($id !== NULL && $cacheKey === NULL) {
194 // Delete a specific contact, across all prevnext caches.
195 $allKeys = $this->redis
->keys($this->key('*', 'all'));
196 foreach ($allKeys as $allKey) {
197 $parts = explode(\CRM_Utils_Cache
::DELIMITER
, $allKey);
199 $tmpCacheKey = array_pop($parts);
200 $this->deleteItem($id, $tmpCacheKey);
204 throw new CRM_Core_Exception("Not implemented: Redis::deleteItem");
208 public function getCount($cacheKey) {
209 $allKey = $this->key($cacheKey, 'all');
210 return $this->redis
->zSize($allKey);
214 * Construct the full path to a cache item.
216 * @param string $cacheKey
217 * Identifier for this saved search.
218 * Ex: 'abcd1234abcd1234'.
219 * @param string $item
220 * Ex: 'list', 'rel', 'data'.
222 * Ex: 'dmaster/prevnext/abcd1234abcd1234/list'
224 private function key($cacheKey, $item) {
225 return $this->prefix
. $cacheKey . \CRM_Utils_Cache
::DELIMITER
. $item;
229 * Initialize any data-structures or timeouts for the cache-key.
231 * This is non-destructive -- if data already exists, it's preserved.
234 * 0 => string $allItemsCacheKey,
235 * 1 => string $dataItemsCacheKey,
236 * 2 => string $selectedItemsCacheKey,
237 * 3 => int $maxExistingScore
239 private function initCacheKey($cacheKey) {
240 $allKey = $this->key($cacheKey, 'all');
241 $selKey = $this->key($cacheKey, 'sel');
242 $dataKey = $this->key($cacheKey, 'data');
244 $this->redis
->setTimeout($allKey, self
::TTL
);
245 $this->redis
->setTimeout($dataKey, self
::TTL
);
246 $this->redis
->setTimeout($selKey, self
::TTL
);
249 foreach ($this->redis
->zRange($allKey, -1, -1, TRUE) as $lastElem => $lastScore) {
250 $maxScore = $lastScore;
252 return [$allKey, $dataKey, $selKey, $maxScore];