1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
taggedcache.php
См. документацию.
1<?php
8
9namespace Bitrix\Main\Data;
10
11use Bitrix\Main\Application;
12use Bitrix\Main\ArgumentException;
13use Bitrix\Main\Data\Internal\CacheTagTable;
14use Bitrix\Main\ObjectPropertyException;
15use Bitrix\Main\SystemException;
16
18{
19 protected int $limit = 200;
20 protected int $delLimit = 100;
21 protected array $cacheStack = [];
22 protected array $cacheTag = [];
23 protected string $salt = '';
24 protected bool $wasTagged = false;
26
27 public function __construct()
28 {
29 $this->pool = Application::getInstance()->getConnectionPool();
30 }
31
32 protected function initDbCache($path): void
33 {
34 if (!isset($this->cacheTag[$path]))
35 {
36 $this->cacheTag[$path] = [];
37 $this->pool->useMasterOnly(true);
38
39 $tags = CacheTagTable::query()
40 ->setSelect(['ID', 'TAG'])
41 ->where('SITE_ID', SITE_ID)
42 ->where('CACHE_SALT', $this->salt)
43 ->where('RELATIVE_PATH', $path)
44 ->fetchAll();
45
46 foreach ($tags as $tag)
47 {
48 $this->cacheTag[$path][$tag['TAG']] = true;
49 }
50
51 $this->pool->useMasterOnly(false);
52 unset($tags, $tag);
53 }
54 }
55
56 protected function initCompSalt(): void
57 {
58 if ($this->salt == '')
59 {
60 $this->salt = Cache::getSalt();
61 }
62 }
63
64 public function startTagCache($relativePath) : void
65 {
66 array_unshift($this->cacheStack, [$relativePath, []]);
67 }
68
73 public function endTagCache(): void
74 {
75 $this->initCompSalt();
76 if ($this->wasTagged)
77 {
78 $cnt = 0;
79 $tags = [];
80 $this->pool->useMasterOnly(true);
81
82 foreach ($this->cacheStack as $cache)
83 {
84 $path = $cache[0];
85 if ($path <> '')
86 {
87 $this->initDbCache($path);
88 foreach ($cache[1] as $tag => $t)
89 {
90 if (!isset($this->cacheTag[$path][$tag]))
91 {
92 $cnt++;
93 $tags[] = [
94 'TAG' => $tag,
95 'RELATIVE_PATH' => $path,
96 'SITE_ID' => SITE_ID,
97 'CACHE_SALT' => $this->salt
98 ];
99
100 $this->cacheTag[$path][$tag] = true;
101 }
102
103 if ($cnt > $this->limit)
104 {
105 CacheTagTable::addMulti($tags, true);
106 $cnt = 0;
107 $tags = [];
108 }
109 }
110 }
111 }
112
113 array_shift($this->cacheStack);
114
115 if (!empty($tags))
116 {
117 CacheTagTable::addMulti($tags, true);
118 }
119
120 $this->pool->useMasterOnly(false);
121 }
122 }
123
124 public function abortTagCache(): void
125 {
126 array_shift($this->cacheStack);
127 }
128
129 public function registerTag($tag): void
130 {
131 if (!empty($this->cacheStack))
132 {
133 $this->cacheStack = array_map(
134 function($val) use ($tag) {
135 $val[1][$tag] = true;
136 return $val;
138 );
139
140 $this->wasTagged = true;
141 }
142 }
143
149 public function clearByTag($tag): void
150 {
151 $cnt = 0;
152 $id = [];
153 $paths = [];
154
155 $this->pool->useMasterOnly(true);
156 $cache = Cache::createInstance();
157
158 $query = CacheTagTable::query()->setSelect(['ID', 'RELATIVE_PATH']);
159
160 if ($tag === true)
161 {
162 $query->whereNot('TAG', '*');
163 }
164 else
165 {
166 $query->where('TAG', $tag);
167 }
168
169 $res = $query->exec();
170 while ($ar = $res->fetch())
171 {
172 $cnt++;
173 $id[] = $ar['ID'];
174 $paths[$ar['RELATIVE_PATH']] = 1;
175 if ($cnt > $this->delLimit)
176 {
177 $this->deleteTags($cache, $id, $paths);
178
179 $cnt = 0;
180 $id = [];
181 $paths = [];
182 }
183 }
184
185 if (!empty($id))
186 {
187 $this->deleteTags($cache, $id, $paths);
188 }
189
190 $this->pool->useMasterOnly(false);
191 unset($res, $paths, $path, $id);
192 }
193
194 protected function deleteTags(Cache $cache, array $id, array $paths): void
195 {
196 CacheTagTable::deleteByFilter(['@ID' => $id]);
197 CacheTagTable::deleteByFilter(['@RELATIVE_PATH' => array_keys($paths)]);
198
199 foreach ($paths as $path => $v)
200 {
201 $cache->cleanDir($path);
202 unset($this->cacheTag[$path]);
203 }
204 }
205
206 public function deleteAllTags(): void
207 {
209 }
210}
$path
Определения access_edit.php:21
static getInstance()
Определения application.php:98
cleanDir($initDir=false, $baseDir='cache')
Определения cache.php:263
clearByTag($tag)
Определения taggedcache.php:149
ConnectionPool $pool
Определения taggedcache.php:25
initDbCache($path)
Определения taggedcache.php:32
startTagCache($relativePath)
Определения taggedcache.php:64
array $cacheTag
Определения taggedcache.php:22
array $cacheStack
Определения taggedcache.php:21
string $salt
Определения taggedcache.php:23
registerTag($tag)
Определения taggedcache.php:129
bool $wasTagged
Определения taggedcache.php:24
deleteTags(Cache $cache, array $id, array $paths)
Определения taggedcache.php:194
static addMulti($rows, $ignoreEvents=false)
Определения datamanager.php:1041
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
$query
Определения get_search.php:11
$ar
Определения options.php:199
$paths
Определения options.php:2080
$val
Определения options.php:1793
const SITE_ID
Определения sonet_set_content_view.php:12