Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
taggedcache.php
1<?php
9namespace Bitrix\Main\Data;
10
11use Bitrix\Main;
12
14{
15 protected $compCacheStack = [];
16 protected $salt = false;
17 protected $cacheTag = [];
18 protected $wasTagged = false;
19 protected $isMySql = false;
20 protected $pool = false;
21
22 public function __construct()
23 {
24 $this->pool = Main\Application::getInstance()->getConnectionPool();
25 $this->isMySql = ($this->pool->getConnection()->getType() === "mysql");
26 }
27
28 protected function initDbCache($path)
29 {
30 if (!isset($this->cacheTag[$path]))
31 {
32 $this->cacheTag[$path] = [];
33
34 $con = Main\Application::getConnection();
35 $sqlHelper = $con->getSqlHelper();
36
37 $this->pool->useMasterOnly(true);
38
39 $rs = $con->query("
40 SELECT TAG
41 FROM b_cache_tag
42 WHERE SITE_ID = '".$sqlHelper->forSql(SITE_ID, 2)."'
43 AND CACHE_SALT = '".$sqlHelper->forSql($this->salt, 4)."'
44 AND RELATIVE_PATH = '".$sqlHelper->forSql($path)."'
45 ");
46
47 while ($ar = $rs->fetch())
48 {
49 $this->cacheTag[$path][$ar["TAG"]] = true;
50 }
51
52 $this->pool->useMasterOnly(false);
53 }
54 }
55
56 protected function initCompSalt()
57 {
58 if ($this->salt === false)
59 {
60 $this->salt = Cache::getSalt();
61 }
62 }
63
64 public function startTagCache($relativePath)
65 {
66 array_unshift($this->compCacheStack, [$relativePath, []]);
67 }
68
69 public function endTagCache()
70 {
71 $this->initCompSalt();
72
73 if ($this->wasTagged)
74 {
75 $this->pool->useMasterOnly(true);
76
77 $con = Main\Application::getConnection();
78 $sqlHelper = $con->getSqlHelper();
79
80 // TODO: SITE_ID
81 $siteIdForSql = $sqlHelper->forSql(SITE_ID, 2);
82 $cacheSaltForSql = $this->salt;
83
84 $strSqlPrefix = "
85 INSERT ".($this->isMySql ? "IGNORE": "")." INTO b_cache_tag (SITE_ID, CACHE_SALT, RELATIVE_PATH, TAG)
86 VALUES
87 ";
88 $maxValuesLen = $this->isMySql ? 2048: 0;
89 $strSqlValues = "";
90
91 foreach ($this->compCacheStack as $arCompCache)
92 {
93 $path = $arCompCache[0];
94 if ($path <> '')
95 {
96 $this->initDbCache($path);
97 $sqlRELATIVE_PATH = $sqlHelper->forSql($path, 255);
98
99 $sql = ",\n('".$siteIdForSql."', '".$cacheSaltForSql."', '".$sqlRELATIVE_PATH."',";
100
101 foreach ($arCompCache[1] as $tag => $t)
102 {
103 if (!isset($this->cacheTag[$path][$tag]))
104 {
105 $strSqlValues .= $sql." '".$sqlHelper->forSql($tag, 100)."')";
106 if (mb_strlen($strSqlValues) > $maxValuesLen)
107 {
108 $con->queryExecute($strSqlPrefix.mb_substr($strSqlValues, 2));
109 $strSqlValues = "";
110 }
111 $this->cacheTag[$path][$tag] = true;
112 }
113 }
114 }
115 }
116 if ($strSqlValues <> '')
117 {
118 $con->queryExecute($strSqlPrefix.mb_substr($strSqlValues, 2));
119 }
120
121 $this->pool->useMasterOnly(false);
122 }
123
124 array_shift($this->compCacheStack);
125 }
126
127 public function abortTagCache()
128 {
129 array_shift($this->compCacheStack);
130 }
131
132 public function registerTag($tag)
133 {
134 if (!empty($this->compCacheStack))
135 {
136 $this->compCacheStack[0][1][$tag] = true;
137 $this->wasTagged = true;
138 }
139 }
140
141 public function clearByTag($tag)
142 {
143 $this->pool->useMasterOnly(true);
144
145 $con = Main\Application::getConnection();
146 $helper = $con->getSqlHelper();
147
148 if ($tag === true)
149 {
150 $where = " WHERE TAG <> '*'";
151 }
152 else
153 {
154 $where = " WHERE TAG = '".$helper->forSql($tag)."'";
155 }
156
157 $dirs = [];
158 $rs = $con->query("SELECT ID, RELATIVE_PATH FROM b_cache_tag".$where);
159 while ($ar = $rs->fetch())
160 {
161 $dirs[$ar["RELATIVE_PATH"]][] = $ar["ID"];
162 }
163
164 $con->queryExecute("DELETE FROM b_cache_tag".$where);
165
166 $max_length = 102400;
167 $sql = "DELETE FROM b_cache_tag WHERE ID in (%s)";
168 $where_list = array();
169 $length = 0;
170 $cache = Cache::createInstance();
171 foreach ($dirs as $path => $ar)
172 {
173 $cache->cleanDir($path);
174 unset($this->cacheTag[$path]);
175
176 foreach ($ar as $cacheTagId)
177 {
178 $where = intval($cacheTagId);
179 $length += mb_strlen($where) + 1;
180 $where_list[] = $where;
181 if ($length > $max_length)
182 {
183 $con->queryExecute(sprintf($sql, implode(",", $where_list)));
184 $where_list = array();
185 $length = 0;
186 }
187 }
188 }
189
190 if ($where_list)
191 {
192 $con->queryExecute(sprintf($sql, implode(",", $where_list)));
193 }
194
195 $this->pool->useMasterOnly(false);
196 }
197
198 public function deleteAllTags()
199 {
200 $con = Main\Application::getConnection();
201 $con->query("TRUNCATE TABLE b_cache_tag");
202 }
203}
static createInstance($params=[])
Definition cache.php:134