vendor/lm/cache/src/CacheService.php line 210

Open in your IDE?
  1. <?php
  2. namespace Lm\Service\Cache;
  3. class CacheService
  4. {
  5.     use LmCachePluggableTrait;
  6.     const CACHE_INSTANCE 1;
  7.     const CACHE_STATIC 2;
  8.     private $_instanceCache = [];
  9.     /**
  10.      * @var CacheObject[]
  11.      */
  12.     private static $_staticCache = [];
  13.     /**
  14.      * 連想配列のハッシュ値を返す
  15.      *
  16.      * @param mixed $data
  17.      * @return false|string
  18.      */
  19.     public static function hash($data)
  20.     {
  21.         // 連想配列をハッシュ化
  22.         $result hash('sha256'base64_encode(serialize($data)));
  23.         //
  24.         return $result;
  25.     }
  26.     /**
  27.      * @param $cache_mode
  28.      * @param $cache
  29.      * @return bool
  30.      * @throws \Exception
  31.      */
  32.     public function getCache($cache_mode, &$cache)
  33.     {
  34.         if ($cache_mode == self::CACHE_INSTANCE) {
  35.             $cache $this->_instanceCache;
  36.         } else if ($cache_mode == self::CACHE_STATIC) {
  37.             $cache self::$_staticCache;
  38.         } else {
  39.             throw new \Exception('The parameter “$cache_mode” is invalid.');
  40.         }
  41.         //
  42.         return true;
  43.     }
  44.     public function getKeys($cache_mode self::CACHE_STATIC)
  45.     {
  46.         if ($cache_mode == self::CACHE_INSTANCE) {
  47.             $result array_keys($this->_instanceCache);
  48.         } else if ($cache_mode == self::CACHE_STATIC) {
  49.             $result array_keys(self::$_staticCache);
  50.         } else {
  51.             throw new \Exception('The parameter “$cache_mode” is invalid.');
  52.         }
  53.         return $result;
  54.     }
  55.     public function test($key$cache_mode self::CACHE_STATIC)
  56.     {
  57. //        if ($this->getCache($cache_mode, $cache)) {
  58. //            //
  59. //            var_dump($cache);
  60. //            //
  61. //            return isset($cache[$key]);
  62. //        }
  63. //        //
  64. //        return false;
  65.         if ($cache_mode == self::CACHE_INSTANCE) {
  66.             //
  67.             $result = isset($this->_instanceCache[$key]);
  68.         } else if ($cache_mode == self::CACHE_STATIC) {
  69.             //
  70.             $result = isset(self::$_staticCache[$key]);
  71.         } else {
  72.             //
  73.             throw new \Exception('The parameter “$cache_mode” is invalid.');
  74.         }
  75.         //
  76.         return $result;
  77.     }
  78.     public function get($key$cache_mode self::CACHE_STATIC)
  79.     {
  80. //        if ($this->getCache($cache_mode, $cache)) {
  81. //            //
  82. //            return $cache[$key];
  83. //        }
  84.         if ($cache_mode == self::CACHE_INSTANCE) {
  85.             //
  86.             $value $this->_instanceCache[$key];
  87.         } else if ($cache_mode == self::CACHE_STATIC) {
  88.             //
  89.             $value self::$_staticCache[$key];
  90.         } else {
  91.             //
  92.             throw new \Exception('The parameter “$cache_mode” is invalid.');
  93.         }
  94.         //
  95.         return $value;
  96.     }
  97.     public function set($key$value$cache_mode self::CACHE_STATIC)
  98.     {
  99. //        if ($this->getCache($cache_mode, $cache)) {
  100. //            //
  101. //            $cache[$key] = $value;
  102. //            //
  103. //            return true;
  104. //        }
  105. //        //
  106. //        return false;
  107.         if ($cache_mode == self::CACHE_INSTANCE) {
  108.             //
  109.             $this->_instanceCache[$key] = $value;
  110.         } else if ($cache_mode == self::CACHE_STATIC) {
  111.             //
  112.             self::$_staticCache[$key] = $value;
  113.         } else {
  114.             //
  115.             throw new \Exception('The parameter “$cache_mode” is invalid.');
  116.         }
  117.         //
  118.         return true;
  119.     }
  120.     public function clear($cache_mode null)
  121.     {
  122.         if ($cache_mode === null) {
  123.             foreach ([self::CACHE_INSTANCEself::CACHE_STATIC] as $cache_mode) {
  124.                 $this->clear($cache_mode);
  125.             }
  126.         } else if ($this->getCache($cache_mode$cache)) {
  127.             //
  128.             $cache = [];
  129.         }
  130.     }
  131.     /**
  132.      * @param string|mixed|null $label
  133.      * @return string
  134.      */
  135.     public static function getCacheKey($label null$level 2)
  136.     {
  137.         //
  138.         $caller debug_backtrace()[$level];
  139.         //
  140.         $cacheKey "{$caller['class']}::{$caller['function']}()";
  141.         //
  142.         if (!empty($label)) {
  143.             if (is_array($label)) {
  144.                 // 配列の場合はハッシュ値を取得
  145.                 $label CacheService::hash($label);
  146.             }
  147.             $cacheKey .= "-{$label}";
  148.         }
  149.         //
  150.         return $cacheKey;
  151.     }
  152.     /**
  153.      * @param callable $callback
  154.      * @return array
  155.      * @throws \ReflectionException
  156.      */
  157.     public static function getCallbackVariables(callable $callback)
  158.     {
  159.         //
  160.         $reflection = new \ReflectionFunction($callback);
  161.         $label $reflection->getStaticVariables();
  162.         //
  163.         return $label;
  164.     }
  165.     public static function getCached(callable $callback$label null)
  166.     {
  167.         //
  168.         $staticVariables self::getCallbackVariables($callback);
  169.         // $labelの省略時、コールバック関数にuse()ステートメントで渡されたパラメータを採用する
  170.         if ($label === null) {
  171.             $label $staticVariables;
  172.         }
  173.         //
  174.         $cacheKey self::getCacheKey($label);
  175.         // var_dump($cacheKey);
  176.         if (static::isCacheEnabled()) {
  177.             //
  178.             if (isset(self::$_staticCache[$cacheKey])) {
  179.                 //
  180.                 // echo 'キャッシュあり';
  181.                 //
  182.                 $cache self::$_staticCache[$cacheKey];
  183.                 //
  184.                 $result $cache->getResult();
  185.                 //
  186.                 foreach ($staticVariables as $key => &$value) {
  187.                     $value $cache->getStaticVariable($key);
  188.                 }
  189.             } else {
  190.                 //
  191.                 // echo 'キャッシュなし';
  192.                 //
  193.                 $result $callback();
  194.                 //
  195.                 self::$_staticCache[$cacheKey] = new CacheObject($result$staticVariables);
  196.             }
  197.         } else {
  198.             //
  199.             $result $callback();
  200.         }
  201.         //
  202.         return $result;
  203.     }
  204. }