src/Util/SoftCache.php line 43

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: adv
  5.  * Date: 02.12.15
  6.  * Time: 19:19
  7.  */
  8. namespace Slivki\Util;
  9. use Memcached;
  10. use Symfony\Component\Config\Definition\Exception\Exception;
  11. class SoftCache extends SoftCacheBase {
  12.     const CACHE_KEY_ALL '-all-';
  13.     protected static $memcache;
  14.     //workaround
  15.     public static $serverAddress 'memcache'//TODO: move cache function to service
  16.     public function __construct($cacheKey) {
  17.         parent::__construct($cacheKey);
  18.         if (!static::$memcache) {
  19.             static::$memcache = new \Memcached();
  20.             static::$memcache->addServer(static::$serverAddress11211);
  21.         }
  22.     }
  23.     public function set($key$object$expire) {
  24.         if (!$object) {
  25.             $object self::EMPTY_VALUE;
  26.         }
  27.         $result = static::$memcache->set($this->cacheKey "-" $key$object$expire);
  28.         if (!$result) {
  29.             $logger Logger::instance('SoftCache');
  30.             $logger->info('setError: ' . static::$memcache->getResultMessage() . ' key=' $key);
  31.         }
  32.     }
  33.     public function get($key$default null) {
  34.         try {
  35.             $object = static::$memcache->get($this->cacheKey "-" $key);
  36.             if ($object == self::EMPTY_VALUE) {
  37.                 return null;
  38.             }
  39.         } catch (Exception $e) {
  40.             $logger Logger::instance('SoftCache');
  41.             $logger->info('Exception in get ' $key ' ' $e->getMessage());
  42.             $logger->info('Exception in get ' $key ' ' . static::$memcache->getResultMessage());
  43.         }
  44.         return $object;
  45.     }
  46.     public function setMulti($data$expire) {
  47.         $prefixedList = [];
  48.         foreach ($data as $key=>$item) {
  49.             $prefixedList[$this->cacheKey "-" $key] = $item;
  50.         }
  51.         $result = static::$memcache->setMulti($prefixedList$expire);
  52.         if (!$result) {
  53.             $logger Logger::instance('SoftCache');
  54.             $logger->info('setError: ' . static::$memcache->getResultMessage());
  55.         }
  56.     }
  57.     public function getMulti(array $keys) {
  58.         $keys array_unique($keys);
  59.         $prefixedKeys array_map(function ($key) {
  60.             return $this->cacheKey "-" $key;
  61.         }, $keys);
  62.         $values = static::$memcache->getMulti($prefixedKeysMemcached::GET_PRESERVE_ORDER);
  63.         if (!$values) {
  64.             return null;
  65.         }
  66.         $result array_combine($keys$values);
  67.         return $result;
  68.     }
  69.     public function add($key$value$expire) {
  70.         return static::$memcache->add($this->cacheKey "-" $key$value$expire);
  71.     }
  72.     public function delete($key) {
  73.         static::$memcache->delete($this->cacheKey "-" $key);
  74.     }
  75.     public function getAllKeys($subKey){
  76.         $keys = static::$memcache->getAllKeys();
  77.         if(!$keys){
  78.             return null;
  79.         }
  80.         $counters = [];
  81.         foreach ($keys as $key){
  82.             if(stristr($key$subKey)){
  83.                 $count = static::$memcache->get($key);
  84.                 $explodeArray explode('-visit-counter-'$key);
  85.                 $counters[$explodeArray[1]] = $count;
  86.             }
  87.         }
  88.         return $counters;
  89.     }
  90.     public function increment($key) {
  91.         return static::$memcache->increment($this->cacheKey "-" $key);
  92.     }
  93. }