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