3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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) {
65 $dao = CRM_Core_DAO
::executeQuery($sql, [], 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);
83 public function fillWithArray($cacheKey, $rows) {
84 list($allKey, $dataKey, , $maxScore) = $this->initCacheKey($cacheKey);
86 foreach ($rows as $row) {
88 $this->redis
->zAdd($allKey, $maxScore, $row['entity_id1']);
89 $this->redis
->hSet($dataKey, $row['entity_id1'], $row['data']);
95 public function fetch($cacheKey, $offset, $rowCount) {
96 $allKey = $this->key($cacheKey, 'all');
97 return $this->redis
->zRange($allKey, $offset, $offset +
$rowCount - 1);
100 public function markSelection($cacheKey, $action, $ids = NULL) {
101 $allKey = $this->key($cacheKey, 'all');
102 $selKey = $this->key($cacheKey, 'sel');
104 if ($action === 'select') {
105 foreach ((array) $ids as $id) {
106 $score = $this->redis
->zScore($allKey, $id);
107 $this->redis
->zAdd($selKey, $score, $id);
110 elseif ($action === 'unselect' && $ids === NULL) {
111 $this->redis
->delete($selKey);
112 $this->redis
->setTimeout($selKey, self
::TTL
);
114 elseif ($action === 'unselect' && $ids !== NULL) {
115 foreach ((array) $ids as $id) {
116 $this->redis
->zDelete($selKey, $id);
121 public function getSelection($cacheKey, $action = 'get') {
122 $allKey = $this->key($cacheKey, 'all');
123 $selKey = $this->key($cacheKey, 'sel');
125 if ($action === 'get') {
127 foreach ($this->redis
->zRange($selKey, 0, -1) as $entity_id) {
128 $result[$entity_id] = 1;
130 return [$cacheKey => $result];
132 elseif ($action === 'getall') {
134 foreach ($this->redis
->zRange($allKey, 0, -1) as $entity_id) {
135 $result[$entity_id] = 1;
137 return [$cacheKey => $result];
140 throw new \
CRM_Core_Exception("Unrecognized action: $action");
144 public function getPositions($cacheKey, $id1) {
145 $allKey = $this->key($cacheKey, 'all');
146 $dataKey = $this->key($cacheKey, 'data');
148 $rank = $this->redis
->zRank($allKey, $id1);
149 if (!is_int($rank) ||
$rank < 0) {
150 return ['foundEntry' => 0];
153 $pos = ['foundEntry' => 1];
157 foreach ($this->redis
->zRange($allKey, $rank - 1, $rank - 1) as $value) {
158 $pos['prev']['id1'] = $value;
160 $pos['prev']['data'] = $this->redis
->hGet($dataKey, $pos['prev']['id1']);
163 $count = $this->getCount($cacheKey);
164 if ($count > $rank +
1) {
166 foreach ($this->redis
->zRange($allKey, $rank +
1, $rank +
1) as $value) {
167 $pos['next']['id1'] = $value;
169 $pos['next']['data'] = $this->redis
->hGet($dataKey, $pos['next']['id1']);
175 public function deleteItem($id = NULL, $cacheKey = NULL) {
176 if ($id === NULL && $cacheKey !== NULL) {
177 // Delete by cacheKey.
178 $allKey = $this->key($cacheKey, 'all');
179 $selKey = $this->key($cacheKey, 'sel');
180 $dataKey = $this->key($cacheKey, 'data');
181 $this->redis
->delete($allKey, $selKey, $dataKey);
183 elseif ($id === NULL && $cacheKey === NULL) {
184 // Delete everything.
185 $keys = $this->redis
->keys($this->prefix
. '*');
186 $this->redis
->del($keys);
188 elseif ($id !== NULL && $cacheKey !== NULL) {
189 // Delete a specific contact, within a specific cache.
190 $this->redis
->zDelete($this->key($cacheKey, 'all'), $id);
191 $this->redis
->zDelete($this->key($cacheKey, 'sel'), $id);
192 $this->redis
->hDel($this->key($cacheKey, 'data'), $id);
194 elseif ($id !== NULL && $cacheKey === NULL) {
195 // Delete a specific contact, across all prevnext caches.
196 $allKeys = $this->redis
->keys($this->key('*', 'all'));
197 foreach ($allKeys as $allKey) {
198 $parts = explode(\CRM_Utils_Cache
::DELIMITER
, $allKey);
200 $tmpCacheKey = array_pop($parts);
201 $this->deleteItem($id, $tmpCacheKey);
205 throw new CRM_Core_Exception("Not implemented: Redis::deleteItem");
209 public function getCount($cacheKey) {
210 $allKey = $this->key($cacheKey, 'all');
211 return $this->redis
->zSize($allKey);
215 * Construct the full path to a cache item.
217 * @param string $cacheKey
218 * Identifier for this saved search.
219 * Ex: 'abcd1234abcd1234'.
220 * @param string $item
221 * Ex: 'list', 'rel', 'data'.
223 * Ex: 'dmaster/prevnext/abcd1234abcd1234/list'
225 private function key($cacheKey, $item) {
226 return $this->prefix
. $cacheKey . \CRM_Utils_Cache
::DELIMITER
. $item;
230 * Initialize any data-structures or timeouts for the cache-key.
232 * This is non-destructive -- if data already exists, it's preserved.
235 * 0 => string $allItemsCacheKey,
236 * 1 => string $dataItemsCacheKey,
237 * 2 => string $selectedItemsCacheKey,
238 * 3 => int $maxExistingScore
240 private function initCacheKey($cacheKey) {
241 $allKey = $this->key($cacheKey, 'all');
242 $selKey = $this->key($cacheKey, 'sel');
243 $dataKey = $this->key($cacheKey, 'data');
245 $this->redis
->setTimeout($allKey, self
::TTL
);
246 $this->redis
->setTimeout($dataKey, self
::TTL
);
247 $this->redis
->setTimeout($selKey, self
::TTL
);
250 foreach ($this->redis
->zRange($allKey, -1, -1, TRUE) as $lastElem => $lastScore) {
251 $maxScore = $lastScore;
253 return array($allKey, $dataKey, $selKey, $maxScore);