src/Biz/System/Service/Impl/CacheServiceImpl.php line 12

Open in your IDE?
  1. <?php
  2. namespace Biz\System\Service\Impl;
  3. use Biz\BaseService;
  4. use Biz\System\Service\CacheService;
  5. class CacheServiceImpl extends BaseService implements CacheService
  6. {
  7.     public function get($name)
  8.     {
  9.         $datas $this->gets(array($name));
  10.         if (empty($datas)) {
  11.             return null;
  12.         }
  13.         return reset($datas);
  14.     }
  15.     public function gets(array $names)
  16.     {
  17.         $this->garbageCollection();
  18.         $names array_filter($names);
  19.         if (empty($names)) {
  20.             return array();
  21.         }
  22.         $datas = array();
  23.         $caches $this->getCacheDao()->findByNames($names);
  24.         $now time();
  25.         foreach ($caches as $cache) {
  26.             if ($cache['expiredTime'] > && $cache['expiredTime'] < $now) {
  27.                 continue;
  28.             }
  29.             $datas[$cache['name']] = $cache['serialized'] ? unserialize($cache['data']) : $cache['data'];
  30.         }
  31.         return $datas;
  32.     }
  33.     public function set($name$data$expiredTime 0)
  34.     {
  35.         $serialized is_string($data) ? 1;
  36.         $cache = array(
  37.             'name' => $name,
  38.             'data' => $serialized serialize($data) : $data,
  39.             'serialized' => $serialized,
  40.             'expiredTime' => $expiredTime,
  41.             'createdTime' => time(),
  42.         );
  43.         $cached $this->getCacheDao()->findByNames(array($name));
  44.         if (empty($cached)) {
  45.             return $this->getCacheDao()->create($cache);
  46.         } else {
  47.             return $this->getCacheDao()->updateByName($name$cache);
  48.         }
  49.     }
  50.     public function clear($name null)
  51.     {
  52.         if (!empty($name)) {
  53.             return $this->getCacheDao()->deleteByName($name);
  54.         } else {
  55.             return $this->getCacheDao()->deleteAll();
  56.         }
  57.     }
  58.     /**
  59.      * @todo
  60.      */
  61.     protected function garbageCollection()
  62.     {
  63.     }
  64.     protected function getCacheDao()
  65.     {
  66.         return $this->createDao('System:CacheDao');
  67.     }
  68. }