redisClient = $redisClient; $this->prefix = $prefixKey; } /** * Stores a value in the cache. Overwrites the previous value if there was one. * * @param $key * @param $value * @param null $ttl */ public function store($key, $value, $ttl = null) { $key = $this->prefix.$key; $ttl = intval($ttl); if ($ttl > 0) { $this->redisClient->set($key, $value, 'EX', $ttl); } else { $this->redisClient->set($key, $value); } } /** * Attempt to retrieve a value from the cache * * Returns an array [$cacheHit, $value] * [true, $valueFromCache] - if it existed in the cache * [false, null] - if it didn't already exist in the cache * * @param $key * @return array */ public function get($key) { $key = $this->prefix.$key; list($wasHit, $value) = $this->redisClient->eval(self::getLuaScript, 1, $key); if ($wasHit) { return [true, $value]; } return [false, null]; } /** * @param $key */ public function delete($key) { $key = $this->prefix.$key; $this->redisClient->del([$key]); } }