Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. <?php
  2. namespace SerginhoLD\Cache;
  3.  
  4. use Bitrix\Main\Data;
  5.  
  6. /**
  7. * Class Cache
  8. * @package SerginhoLD\Cache
  9. *
  10. * Decorator для работы с кешем
  11. * Невозможно наследовать класс Cache из ядра Bitrix, в функциях используется static для работы с private-переменными
  12. * (Версия модуля main: 16.5.16)
  13. *
  14. * @method static Data\ICacheEngine createCacheEngine()
  15. * @method static string getCacheEngineType
  16. * @method static void setShowCacheStat(bool $showCacheStat)
  17. * @method static bool getShowCacheStat()
  18. * @method static void setClearCache(bool $clearCache)
  19. * @method static void setClearCacheSession(bool $clearCacheSession)
  20. * @method static string getSalt()
  21. * @method static bool shouldClearCache()
  22. * @method static string getPath(string $uniqueString)
  23. * @method void clean(string $uniqueString, $initDir = false, string $baseDir = 'cache')
  24. * @method void cleanDir($initDir = false, string $baseDir = 'cache')
  25. * @method bool initCache(int $TTL, string $uniqueString, $initDir = false, string $baseDir = 'cache')
  26. * @method string output()
  27. * @method mixed getVars()
  28. * @method bool startDataCache($TTL = false, $uniqueString = false, $initDir = false, $vars = [], string $baseDir = 'cache')
  29. * @method void abortDataCache()
  30. * @method void endDataCache($vars = false)
  31. * @method bool isCacheExpired(string $path)
  32. * @method bool isStarted()
  33. * @method static bool clearCache($full = false, string $initDir = '')
  34. * @method void forceRewriting(bool $mode)
  35. */
  36. class Cache
  37. {
  38. /** @var Data\Cache */
  39. protected $oDataCache = null;
  40.  
  41. /** @var int */
  42. protected $defaultDuration = 3600;
  43.  
  44. /**
  45. * Cache constructor.
  46. * @param Data\ICacheEngine $oCacheEngine
  47. */
  48. public function __construct(Data\ICacheEngine $oCacheEngine)
  49. {
  50. $this->oDataCache = new Data\Cache($oCacheEngine);
  51. }
  52.  
  53. /**
  54. * @return static
  55. */
  56. public static function createInstance()
  57. {
  58. $oCacheEngine = static::createCacheEngine();
  59. return new static($oCacheEngine);
  60. }
  61.  
  62. /**
  63. * @param string $key
  64. * @param \Closure $closure
  65. * @param int|null $duration
  66. * @param $initDir
  67. * @param string $baseDir
  68. * @return mixed
  69. */
  70. public function getOrSet($key, \Closure $closure, $duration = null, $initDir = false, $baseDir = 'cache')
  71. {
  72. if (is_null($duration))
  73. $duration = $this->defaultDuration;
  74.  
  75. if ($this->initCache($duration, $key, $initDir, $baseDir))
  76. {
  77. //var_dump('log-cache');
  78. return $this->getVars();
  79. }
  80.  
  81. $value = call_user_func($closure, $this);
  82.  
  83. if ($this->startDataCache())
  84. {
  85. //var_dump('log-startDataCache');
  86. $this->endDataCache($value);
  87. }
  88.  
  89. return $value;
  90. }
  91.  
  92. /**
  93. * @param $name
  94. * @param $arguments
  95. * @return mixed
  96. */
  97. public function __call($name, $arguments)
  98. {
  99. return call_user_func_array([$this->oDataCache, $name], $arguments);
  100. }
  101.  
  102. /**
  103. * @param $name
  104. * @param $arguments
  105. * @return mixed
  106. */
  107. public static function __callStatic($name, $arguments)
  108. {
  109. return forward_static_call_array([Data\Cache::class, $name], $arguments);
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement