Bitrix-D7  20.0.0
taggedcache.php
См. документацию.
1 <?php
2 /**
3  * Bitrix Framework
4  * @package bitrix
5  * @subpackage main
6  * @copyright 2001-2014 Bitrix
7  */
8 
9 namespace Bitrix\Main\Data;
10 
11 use Bitrix\Main;
12 
14 {
15  protected $compCacheStack = [];
16  protected $salt = false;
17  protected $cacheTag = [];
18  protected $wasTagged = false;
19  protected $isMySql = false;
20 
21  public function __construct()
22  {
23  $this->isMySql = (static::getDbType() === "MYSQL");
24  }
25 
26  protected static function getDbType()
27  {
28  static $type = null;
29  if ($type === null)
30  {
31  $cm = Main\Application::getInstance()->getConnectionPool();
32  $type = $cm->getDefaultConnectionType();
33  }
34  return $type;
35  }
36 
37  protected function initDbCache($path)
38  {
39  if (!isset($this->cacheTag[$path]))
40  {
41  $this->cacheTag[$path] = [];
42 
44  $sqlHelper = $con->getSqlHelper();
45 
46  $rs = $con->query("
47  SELECT TAG
48  FROM b_cache_tag
49  WHERE SITE_ID = '".$sqlHelper->forSql(SITE_ID, 2)."'
50  AND CACHE_SALT = '".$sqlHelper->forSql($this->salt, 4)."'
51  AND RELATIVE_PATH = '".$sqlHelper->forSql($path)."'
52  ");
53  while ($ar = $rs->fetch())
54  {
55  $this->cacheTag[$path][$ar["TAG"]] = true;
56  }
57  }
58  }
59 
60  protected function initCompSalt()
61  {
62  if ($this->salt === false)
63  {
64  $this->salt = Cache::getSalt();
65  }
66  }
67 
68  public function startTagCache($relativePath)
69  {
70  array_unshift($this->compCacheStack, array($relativePath, array()));
71  }
72 
73  public function endTagCache()
74  {
75  $this->initCompSalt();
76 
77  if ($this->wasTagged)
78  {
80  $sqlHelper = $con->getSqlHelper();
81 
82  // TODO: SITE_ID
83  $siteIdForSql = $sqlHelper->forSql(SITE_ID, 2);
84  $cacheSaltForSql = $this->salt;
85 
86  $strSqlPrefix = "
87  INSERT ".($this->isMySql ? "IGNORE": "")." INTO b_cache_tag (SITE_ID, CACHE_SALT, RELATIVE_PATH, TAG)
88  VALUES
89  ";
90  $maxValuesLen = $this->isMySql ? 2048: 0;
91  $strSqlValues = "";
92 
93  foreach ($this->compCacheStack as $arCompCache)
94  {
95  $path = $arCompCache[0];
96  if ($path <> '')
97  {
98  $this->initDbCache($path);
99  $sqlRELATIVE_PATH = $sqlHelper->forSql($path, 255);
100 
101  $sql = ",\n('".$siteIdForSql."', '".$cacheSaltForSql."', '".$sqlRELATIVE_PATH."',";
102 
103  foreach ($arCompCache[1] as $tag => $t)
104  {
105  if (!isset($this->cacheTag[$path][$tag]))
106  {
107  $strSqlValues .= $sql." '".$sqlHelper->forSql($tag, 100)."')";
108  if (strlen($strSqlValues) > $maxValuesLen)
109  {
110  $con->queryExecute($strSqlPrefix.substr($strSqlValues, 2));
111  $strSqlValues = "";
112  }
113  $this->cacheTag[$path][$tag] = true;
114  }
115  }
116  }
117  }
118  if ($strSqlValues <> '')
119  {
120  $con->queryExecute($strSqlPrefix.substr($strSqlValues, 2));
121  }
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  {
144  $sqlHelper = $con->getSqlHelper();
145 
146  if ($tag === true)
147  {
148  $sqlWhere = " WHERE TAG <> '*'";
149  }
150  else
151  {
152  $sqlWhere = " WHERE TAG = '".$sqlHelper->forSql($tag)."'";
153  }
154 
155  $dirs = array();
156  $rs = $con->query("SELECT * FROM b_cache_tag".$sqlWhere);
157  while ($ar = $rs->fetch())
158  {
159  $dirs[$ar["RELATIVE_PATH"]] = $ar;
160  }
161 
162  $con->queryExecute("DELETE FROM b_cache_tag".$sqlWhere);
163 
164  $cache = Cache::createInstance();
165  foreach ($dirs as $path => $ar)
166  {
167  $con->queryExecute("
168  DELETE FROM b_cache_tag
169  WHERE SITE_ID = '".$sqlHelper->forSql($ar["SITE_ID"])."'
170  AND CACHE_SALT = '".$sqlHelper->forSql($ar["CACHE_SALT"])."'
171  AND RELATIVE_PATH = '".$sqlHelper->forSql($ar["RELATIVE_PATH"])."'
172  ");
173 
174  $cache->cleanDir($path);
175  unset($this->cacheTag[$path]);
176  }
177  }
178 }
Bitrix\Main\Data\TaggedCache\__construct
__construct()
Definition: taggedcache.php:21
Bitrix\Main\Data
Definition: aliases.php:105
Bitrix\Main\Data\TaggedCache\$cacheTag
$cacheTag
Definition: taggedcache.php:17
Bitrix\Main
Bitrix\Main\Data\TaggedCache\$isMySql
$isMySql
Definition: taggedcache.php:19
Bitrix\Main\Data\TaggedCache\$compCacheStack
$compCacheStack
Definition: taggedcache.php:15
Bitrix\Main\Data\Cache\getSalt
static getSalt()
Definition: cache.php:192
Bitrix\Main\Data\TaggedCache\registerTag
registerTag($tag)
Definition: taggedcache.php:132
Bitrix\Main\Data\TaggedCache\initCompSalt
initCompSalt()
Definition: taggedcache.php:60
Bitrix\Main\Data\TaggedCache
Definition: taggedcache.php:13
Bitrix\Main\Data\TaggedCache\getDbType
static getDbType()
Definition: taggedcache.php:26
Bitrix\Main\Data\TaggedCache\clearByTag
clearByTag($tag)
Definition: taggedcache.php:141
Bitrix\Main\Application\getConnection
static getConnection($name="")
Static method returns database connection for the specified name.
Definition: main/lib/application.php:381
Bitrix\Main\Data\TaggedCache\abortTagCache
abortTagCache()
Definition: taggedcache.php:127
Bitrix\Main\Data\TaggedCache\initDbCache
initDbCache($path)
Definition: taggedcache.php:37
Bitrix\Main\Data\TaggedCache\endTagCache
endTagCache()
Definition: taggedcache.php:73
Bitrix\Main\Application\getInstance
static getInstance()
Returns current instance of the Application.
Definition: main/lib/application.php:86
Bitrix\Main\Data\TaggedCache\$salt
$salt
Definition: taggedcache.php:16
Bitrix\Main\Data\TaggedCache\$wasTagged
$wasTagged
Definition: taggedcache.php:18
Bitrix\Main\Data\TaggedCache\startTagCache
startTagCache($relativePath)
Definition: taggedcache.php:68
Bitrix\Main\Data\Cache\createInstance
static createInstance($params=[])
Definition: cache.php:153