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; }
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']; }); }
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)); } }
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; }
private function selectBackend($cacheId, $key) { $hashValue = "{$cacheId}/{$key}"; $backendId = abs(\Cachet\Helper::hashToInt32($hashValue) % $this->backendCount); return $this->backends[$backendId]; }
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); }
protected function flushStore($cacheId) { $prefix = \Cachet\Helper::formatKey([$this->prefix, $cacheId]); xcache_unset_by_prefix($prefix); }
function decrement($key, $by = 1) { $formatted = \Cachet\Helper::formatKey([$this->prefix, 'counter', $key]); $value = xcache_dec($formatted, $by, $this->counterTTL); return $value; }
protected function deleteFromStore($cacheId, $key) { $formattedKey = \Cachet\Helper::formatKey([$this->prefix, $cacheId, $key]); $memcache = $this->connector->memcache ?: $this->connector->connect(); return $memcache->delete($formattedKey); }
function set($key, $value) { $formattedKey = \Cachet\Helper::formatKey(['counter', $key]); $this->cache->set($formattedKey, $value); }
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; }
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; }
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}"); } }