Exemplo n.º 1
0
 private function getId($cacheId, $key)
 {
     $id = $this->getLockKey($cacheId, $key);
     if (!is_int($id) || $id > PHP_INT_MAX || $id < ~PHP_INT_MAX) {
         $id = \Cachet\Helper::hashToInt32($id);
     }
     return $id;
 }
Exemplo n.º 2
0
 function items($cacheId)
 {
     $fullPrefix = \Cachet\Helper::formatKey([$this->prefix, $cacheId]);
     $keyRegex = "~^" . preg_quote($fullPrefix, "~") . "~";
     $iter = new \APCIterator('user', $keyRegex, APC_ITER_VALUE, $this->iteratorChunkSize);
     return new \Cachet\Util\MapIterator($iter, function ($item) {
         return $item['value'];
     });
 }
Exemplo n.º 3
0
 public function __construct($dbInfo)
 {
     if (is_string($dbInfo)) {
         $dbInfo = ['dsn' => $dbInfo];
     }
     if (is_array($dbInfo)) {
         $this->params = $dbInfo;
     } elseif (is_callable($dbInfo)) {
         $this->creatorCallback = $dbInfo;
     } elseif (is_object($dbInfo)) {
         $this->pdo = $dbInfo;
         $this->engine = $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME);
     } else {
         throw new \InvalidArgumentException("Must pass a connection info array, a callback that returns a PDO " . "or a PDO instance, found " . \Cachet\Helper::getType($dbInfo));
     }
 }
Exemplo n.º 4
0
 private function change($method, $key, $by)
 {
     $formattedKey = \Cachet\Helper::formatKey([$this->prefix, $this->cacheId, $key]);
     $value = $method($formattedKey, abs($by), $success);
     if (!$success) {
         $check = false;
         if ($this->locker) {
             $this->locker->acquire($this->cacheId, $key);
             $check = apc_fetch($formattedKey);
             if ($check !== false) {
                 $value = $method($formattedKey, abs($by), $success);
             }
         }
         if ($check === false) {
             $this->set($key, $by);
             $value = $by;
         }
         if ($this->locker) {
             $this->locker->release($this->cacheId, $key);
         }
     }
     return $value;
 }
Exemplo n.º 5
0
 private function selectBackend($cacheId, $key)
 {
     $hashValue = "{$cacheId}/{$key}";
     $backendId = abs(\Cachet\Helper::hashToInt32($hashValue) % $this->backendCount);
     return $this->backends[$backendId];
 }
Exemplo n.º 6
0
 function decrement($key, $by = 1)
 {
     $redis = $this->connector->redis ?: $this->connector->connect();
     $key = \Cachet\Helper::formatKey([$this->prefix, 'counter', $key]);
     return $redis->decrBy($key, $by);
 }
Exemplo n.º 7
0
 protected function flushStore($cacheId)
 {
     $prefix = \Cachet\Helper::formatKey([$this->prefix, $cacheId]);
     xcache_unset_by_prefix($prefix);
 }
Exemplo n.º 8
0
 function decrement($key, $by = 1)
 {
     $formatted = \Cachet\Helper::formatKey([$this->prefix, 'counter', $key]);
     $value = xcache_dec($formatted, $by, $this->counterTTL);
     return $value;
 }
Exemplo n.º 9
0
 protected function deleteFromStore($cacheId, $key)
 {
     $formattedKey = \Cachet\Helper::formatKey([$this->prefix, $cacheId, $key]);
     $memcache = $this->connector->memcache ?: $this->connector->connect();
     return $memcache->delete($formattedKey);
 }
Exemplo n.º 10
0
 function set($key, $value)
 {
     $formattedKey = \Cachet\Helper::formatKey(['counter', $key]);
     $this->cache->set($formattedKey, $value);
 }
Exemplo n.º 11
0
 function keys($cacheId)
 {
     $prefix = \Cachet\Helper::formatKey([$this->prefix, $cacheId]) . "/";
     $len = strlen($prefix);
     $redis = $this->connector->redis ?: $this->connector->connect();
     $redisKeys = $redis->keys("{$prefix}*");
     $keys = [];
     foreach ($redisKeys as $key) {
         $keys[] = substr($key, $len);
     }
     sort($keys);
     return $keys;
 }
Exemplo n.º 12
0
 private function createSafeKey($key)
 {
     $safeKey = substr(preg_replace("/[^a-z\\d_\\-]/", "", strtolower($key)), 0, 50);
     // crc32 is platform dependent? not that it should matter, but it makes testing
     // quite tricky.
     $hashedKey = base_convert(\Cachet\Helper::hashToInt32($key), 10, 36) . ($safeKey ? "-{$safeKey}" : '');
     return $hashedKey;
 }
Exemplo n.º 13
0
 private function ensureValidValue($memcache, $formattedKey, $value)
 {
     if (!is_numeric($value) && !is_bool($value) && $value !== null) {
         $type = \Cachet\Helper::getType($value);
         throw new \UnexpectedValueException("Memcache counter expected numeric value, found {$type} at key {$key}");
     }
 }