/** * Gets a lock or waits for it to become available * @param mixed $key Item to lock * @param int $timeout Time to wait for the key (seconds) * @param int $ttl TTL (seconds) * @return Lock The key * @throws LockException If the key is invalid * @throws LockTimeoutException If the lock is not acquired before the method times out and $ex true */ public function get($key, $timeout = null, $ttl = null, $ex = false) { if (!$key) { throw new LockException("Invalid Key"); } /** @var RedisClient $redis */ $redis = $this->resource_factory->get($this->redis_name); $end_time = time() + $timeout; $key = $this->key($key); do { $expire = $this->expire_time($ttl); if ($acquired = $redis->setnx($key, $expire)) { $acquired = new Lock($key, $expire, $redis); break; } if ($acquired = $this->recover($redis, $key)) { break; } if ($timeout === 0) { break; } usleep(self::USLEEP); } while (!is_numeric($timeout) || time() < $end_time); if (!$acquired && $ex) { throw new LockTimeoutException("Timeout exceeded"); } return $acquired; }
function testOneInstance() { ResourceFactory::getInstance()->register('test1', function () { return new \stdClass(); }); $this->assertSame(ResourceFactory::getInstance()->get('test1'), ResourceFactory::getInstance()->get('test1')); }
/** * @return \Redis */ private static function redis() { return \Splitice\ResourceFactory::getInstance()->get('redis'); }
function execute(array $arguments) { $redis = ResourceFactory::getInstance()->get('redis'); $default_ttl = isset($arguments[1]) ? (int) $arguments[1] : 6000; $deleted = 0; $index_key = RedisStorage::getIndexKey(); $set_cleanup = array($index_key); $keys = array(); foreach ($redis->sMembers($index_key) as $k) { $full_key = RedisStorage::PREFIX . $k; $ttl = $redis->ttl($full_key); if ($ttl == -1) { if ($redis->exists($full_key)) { $redis->expire($full_key, $default_ttl); $ttl = $default_ttl; } else { $set_cleanup[] = $k; continue; } } else { if ($ttl == -2) { $set_cleanup[] = $k; continue; } } if ($ttl <= 10) { $redis->del($full_key); $set_cleanup[] = $k; $deleted++; } else { $keys[$k] = $ttl; } } echo "{$deleted} keys deleted due to ttl expiry.\r\n"; if (count($set_cleanup) != 1) { $ret = call_user_func_array(array($redis, 'sRem'), $set_cleanup); if (!$ret) { echo "FAILURE: Attempted to remove ", count($set_cleanup), " keys from tracking set due to non-existance / ttl-expire.\r\n"; } else { echo count($set_cleanup), " keys removed from tracking set due to non-existance / ttl-expire.\r\n"; } } $count = $redis->scard($index_key); //trim db $targetSize = isset($arguments[0]) ? (int) $arguments[0] : 100000; if ($count > $targetSize) { $its = $count - $targetSize; uasort($keys, array($this, 'cmp')); foreach ($keys as $k => $v) { $redis->del(RedisStorage::PREFIX . $k); $redis->sRem($index_key, $k); $deleted++; unset($keys[$k]); $its--; if ($its <= 0) { break; } } echo "{$deleted} keys deleted to reduce the quantity.\r\n"; } echo "total - {$deleted} keys deleted - ", count($keys), " remaining out of {$count} keys.\r\n"; }