PageRenderTime 32ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/bitrix/modules/main/lib/data/managedcache.php

https://gitlab.com/neuser/bitrix-core
PHP | 196 lines | 150 code | 20 blank | 26 comment | 13 complexity | a620463a724356daefaa8ac2410fd5de MD5 | raw file
  1. <?php
  2. /**
  3. * Bitrix Framework
  4. * @package bitrix
  5. * @subpackage main
  6. * @copyright 2001-2014 Bitrix
  7. */
  8. namespace Bitrix\Main\Data;
  9. use Bitrix\Main;
  10. class ManagedCache
  11. {
  12. /**
  13. * @var Cache[]
  14. */
  15. protected $cache = array();
  16. protected $cache_init = array();
  17. protected $cachePath = array();
  18. protected $vars = array();
  19. protected $ttl = array();
  20. public function __construct()
  21. {
  22. }
  23. protected static function getDbType()
  24. {
  25. static $type = null;
  26. if ($type === null)
  27. {
  28. $type = Main\Application::getInstance()->getConnection()->getType();
  29. $type = strtoupper($type);
  30. }
  31. return $type;
  32. }
  33. // Tries to read cached variable value from the file
  34. // Returns true on success
  35. // otherwise returns false
  36. public function read($ttl, $uniqueId, $tableId = false)
  37. {
  38. if (!isset($this->cache_init[$uniqueId]))
  39. {
  40. $this->cache[$uniqueId] = Cache::createInstance();
  41. $this->cachePath[$uniqueId] = static::getDbType().($tableId === false ? "" : "/".$tableId);
  42. $this->ttl[$uniqueId] = $ttl;
  43. $this->cache_init[$uniqueId] = $this->cache[$uniqueId]->initCache($ttl, $uniqueId, $this->cachePath[$uniqueId], "managed_cache");
  44. }
  45. return $this->cache_init[$uniqueId] || array_key_exists($uniqueId, $this->vars);
  46. }
  47. public function getImmediate($ttl, $uniqueId, $tableId = false)
  48. {
  49. $cache = Cache::createInstance();
  50. $cachePath = static::getDbType().($tableId === false ? "" : "/".$tableId);
  51. if ($cache->initCache($ttl, $uniqueId, $cachePath, "managed_cache"))
  52. {
  53. return $cache->getVars();
  54. }
  55. return false;
  56. }
  57. /**
  58. * This method is used to read the variable value
  59. * from the cache after successfull Read
  60. *
  61. * @param string $uniqueId
  62. * @return mixed
  63. */
  64. public function get($uniqueId)
  65. {
  66. if (array_key_exists($uniqueId, $this->vars))
  67. {
  68. return $this->vars[$uniqueId];
  69. }
  70. elseif (isset($this->cache_init[$uniqueId]) && $this->cache_init[$uniqueId])
  71. {
  72. return $this->cache[$uniqueId]->getVars();
  73. }
  74. else
  75. {
  76. return false;
  77. }
  78. }
  79. // Sets new value to the variable
  80. public function set($uniqueId, $val)
  81. {
  82. if (isset($this->cache[$uniqueId]))
  83. {
  84. $this->vars[$uniqueId] = $val;
  85. }
  86. }
  87. public function setImmediate($uniqueId, $val)
  88. {
  89. if (isset($this->cache[$uniqueId]))
  90. {
  91. $obCache = Cache::createInstance();
  92. $obCache->startDataCache($this->ttl[$uniqueId], $uniqueId, $this->cachePath[$uniqueId], $val, "managed_cache");
  93. $obCache->endDataCache();
  94. unset($this->cache[$uniqueId]);
  95. unset($this->cache_init[$uniqueId]);
  96. unset($this->cachePath[$uniqueId]);
  97. unset($this->vars[$uniqueId]);
  98. }
  99. }
  100. // Marks cache entry as invalid
  101. public function clean($uniqueId, $tableId = false)
  102. {
  103. $obCache = Cache::createInstance();
  104. $obCache->clean(
  105. $uniqueId,
  106. static::getDbType().($tableId === false ? "" : "/".$tableId),
  107. "managed_cache"
  108. );
  109. if (isset($this->cache[$uniqueId]))
  110. {
  111. unset($this->cache[$uniqueId]);
  112. unset($this->cache_init[$uniqueId]);
  113. unset($this->cachePath[$uniqueId]);
  114. unset($this->vars[$uniqueId]);
  115. }
  116. }
  117. // Marks cache entries associated with the table as invalid
  118. public function cleanDir($tableId)
  119. {
  120. $dbType = static::getDbType();
  121. $strPath = $dbType."/".$tableId;
  122. foreach ($this->cachePath as $uniqueId => $Path)
  123. {
  124. if ($Path == $strPath)
  125. {
  126. unset($this->cache[$uniqueId]);
  127. unset($this->cache_init[$uniqueId]);
  128. unset($this->cachePath[$uniqueId]);
  129. unset($this->vars[$uniqueId]);
  130. }
  131. }
  132. $obCache = Cache::createInstance();
  133. $obCache->cleanDir($dbType."/".$tableId, "managed_cache");
  134. }
  135. // Clears all managed_cache
  136. public function cleanAll()
  137. {
  138. $this->cache = array();
  139. $this->cache_init = array();
  140. $this->cachePath = array();
  141. $this->vars = array();
  142. $this->ttl = array();
  143. $obCache = Cache::createInstance();
  144. $obCache->cleanDir(false, "managed_cache");
  145. }
  146. // Use it to flush cache to the files.
  147. // Causion: only at the end of all operations!
  148. public static function finalize()
  149. {
  150. $cacheManager = Main\Application::getInstance()->getManagedCache();
  151. $cache = Cache::createInstance();
  152. foreach ($cacheManager->cache as $uniqueId => $val)
  153. {
  154. if (array_key_exists($uniqueId, $cacheManager->vars))
  155. {
  156. $cache->startDataCache($cacheManager->ttl[$uniqueId], $uniqueId, $cacheManager->cachePath[$uniqueId], $cacheManager->vars[$uniqueId], "managed_cache");
  157. $cache->endDataCache();
  158. }
  159. }
  160. }
  161. public function getCompCachePath($relativePath)
  162. {
  163. // TODO: global var!
  164. global $BX_STATE;
  165. if ($BX_STATE === "WA")
  166. {
  167. $salt = Cache::getSalt();
  168. }
  169. else
  170. {
  171. $salt = "/".mb_substr(md5($BX_STATE), 0, 3);
  172. }
  173. $path = "/".SITE_ID.$relativePath.$salt;
  174. return $path;
  175. }
  176. }