Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace SerginhoLD\Cache;
- use Bitrix\Main\Data;
- /**
- * Class Cache
- * @package SerginhoLD\Cache
- *
- * Decorator для работы с кешем
- * Невозможно наследовать класс Cache из ядра Bitrix, в функциях используется static для работы с private-переменными
- * (Версия модуля main: 16.5.16)
- *
- * @method static Data\ICacheEngine createCacheEngine()
- * @method static string getCacheEngineType
- * @method static void setShowCacheStat(bool $showCacheStat)
- * @method static bool getShowCacheStat()
- * @method static void setClearCache(bool $clearCache)
- * @method static void setClearCacheSession(bool $clearCacheSession)
- * @method static string getSalt()
- * @method static bool shouldClearCache()
- * @method static string getPath(string $uniqueString)
- * @method void clean(string $uniqueString, $initDir = false, string $baseDir = 'cache')
- * @method void cleanDir($initDir = false, string $baseDir = 'cache')
- * @method bool initCache(int $TTL, string $uniqueString, $initDir = false, string $baseDir = 'cache')
- * @method string output()
- * @method mixed getVars()
- * @method bool startDataCache($TTL = false, $uniqueString = false, $initDir = false, $vars = [], string $baseDir = 'cache')
- * @method void abortDataCache()
- * @method void endDataCache($vars = false)
- * @method bool isCacheExpired(string $path)
- * @method bool isStarted()
- * @method static bool clearCache($full = false, string $initDir = '')
- * @method void forceRewriting(bool $mode)
- */
- class Cache
- {
- /** @var Data\Cache */
- protected $oDataCache = null;
- /** @var int */
- protected $defaultDuration = 3600;
- /**
- * Cache constructor.
- * @param Data\ICacheEngine $oCacheEngine
- */
- public function __construct(Data\ICacheEngine $oCacheEngine)
- {
- $this->oDataCache = new Data\Cache($oCacheEngine);
- }
- /**
- * @return static
- */
- public static function createInstance()
- {
- $oCacheEngine = static::createCacheEngine();
- return new static($oCacheEngine);
- }
- /**
- * @param string $key
- * @param \Closure $closure
- * @param int|null $duration
- * @param $initDir
- * @param string $baseDir
- * @return mixed
- */
- public function getOrSet($key, \Closure $closure, $duration = null, $initDir = false, $baseDir = 'cache')
- {
- if (is_null($duration))
- $duration = $this->defaultDuration;
- if ($this->initCache($duration, $key, $initDir, $baseDir))
- {
- //var_dump('log-cache');
- return $this->getVars();
- }
- $value = call_user_func($closure, $this);
- if ($this->startDataCache())
- {
- //var_dump('log-startDataCache');
- $this->endDataCache($value);
- }
- return $value;
- }
- /**
- * @param $name
- * @param $arguments
- * @return mixed
- */
- public function __call($name, $arguments)
- {
- return call_user_func_array([$this->oDataCache, $name], $arguments);
- }
- /**
- * @param $name
- * @param $arguments
- * @return mixed
- */
- public static function __callStatic($name, $arguments)
- {
- return forward_static_call_array([Data\Cache::class, $name], $arguments);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement