Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
cacheenginememcache.php
1<?php
2namespace Bitrix\Main\Data;
3
5{
6 public function getConnectionName(): string
7 {
8 return 'cache.memcache';
9 }
10
11 public static function getConnectionClass()
12 {
13 return MemcacheConnection::class;
14 }
15
16 protected static function getExpire($ttl)
17 {
18 $ttl = (int) $ttl;
19 if ($ttl > 2592000)
20 {
21 $ttl = microtime(1) + $ttl;
22 }
23
24 return $ttl;
25 }
26
27 public function set($key, $ttl, $value)
28 {
29 $ttl = self::getExpire($ttl);
30 return self::$engine->set($key, $value, 0, $ttl);
31 }
32
33 public function get($key)
34 {
35 return self::$engine->get($key);
36 }
37
38 public function del($key)
39 {
40 if (is_array($key))
41 {
42 foreach ($key as $item)
43 {
44 self::$engine->delete($item);
45 }
46 }
47 else
48 {
49 self::$engine->delete($key);
50 }
51 }
52
53 public function setNotExists($key, $ttl, $value)
54 {
55 $ttl = self::getExpire($ttl);
56 return self::$engine->add($key, $value, 0, $ttl);
57 }
58
59 public function addToSet($key, $value)
60 {
61 $cacheKey = sha1($key . '|' . $value);
62 if (array_key_exists($cacheKey, self::$listKeys))
63 {
64 return;
65 }
66
67 $iexKey = $key . '|iex|' . $cacheKey;
68 $itemExist = self::$engine->get($iexKey);
69 if ($itemExist == $cacheKey)
70 {
71 return;
72 }
73
74 $list = self::$engine->get($key);
75
76 if (!is_array($list))
77 {
78 $list = [];
79 }
80
81 if (!array_key_exists($value, $list))
82 {
83 $list[$value] = 1;
84 $this->set($key, 0, $list);
85 self::$listKeys[$cacheKey] = 1;
86 }
87
88 $this->set($iexKey, 2591000, $cacheKey);
89 }
90
91 public function getSet($key): array
92 {
93 $list = self::$engine->get($key);
94 if (!is_array($list) || empty($list))
95 {
96 return [];
97 }
98
99 return array_keys($list);
100 }
101
102 public function deleteBySet($key, $prefix = '')
103 {
104 $list = self::$engine->get($key);
105 if (is_array($list) && !empty($list))
106 {
107 foreach ($list as $iKey => $value)
108 {
109 if ($prefix == '')
110 {
111 $this->del($iKey);
112 }
113 else
114 {
115 $this->del($prefix . $iKey);
116 }
117
118 $cacheKey = sha1($key . '|' . $iKey);
119 $iexKey = $key . '|iex|' . $cacheKey;
120 $this->del($iexKey);
121 }
122 }
123 }
124
125 public function delFromSet($key, $member)
126 {
127 $list = self::$engine->get($key);
128
129 if (is_array($list) && !empty($list))
130 {
131 $rewrite = false;
132 if (is_array($member))
133 {
134 foreach ($member as $keyID)
135 {
136 if (array_key_exists($keyID, $list))
137 {
138 $rewrite = true;
139 $cacheKey = sha1($key . '|' . $keyID);
140 unset($list[$keyID]);
141 unset(self::$listKeys[$cacheKey]);
142
143 $iexKey = $key . '|iex|' . $cacheKey;
144 $this->del($iexKey);
145 }
146 }
147 }
148 elseif (array_key_exists($member, $list))
149 {
150 $rewrite = true;
151 $cacheKey = sha1($key . '|' . $member);
152 unset(self::$listKeys[$cacheKey]);
153 unset($list[$member]);
154
155 $iexKey = $key . '|iex|' . $cacheKey;
156 $this->del($iexKey);
157 }
158
159 if ($rewrite)
160 {
161 if (empty($list))
162 {
163 $this->del($key);
164 }
165 else
166 {
167 $this->set($key, 0, $list);
168 }
169 }
170 }
171 }
172}