123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- <?php
- namespace Doctrine\Common\Cache;
- use Redis;
- class RedisCache extends CacheProvider
- {
-
- private $redis;
-
- public function setRedis(Redis $redis)
- {
- $redis->setOption(Redis::OPT_SERIALIZER, $this->getSerializerValue());
- $this->redis = $redis;
- }
-
- public function getRedis()
- {
- return $this->redis;
- }
-
- protected function doFetch($id)
- {
- return $this->redis->get($id);
- }
-
- protected function doFetchMultiple(array $keys)
- {
- $fetchedItems = $this->redis->mget($keys);
- return array_filter(
- array_combine($keys, $fetchedItems),
- function ($value) {
- return $value !== false;
- }
- );
- }
-
- protected function doContains($id)
- {
- return $this->redis->exists($id);
- }
-
- protected function doSave($id, $data, $lifeTime = 0)
- {
- if ($lifeTime > 0) {
- return $this->redis->setex($id, $lifeTime, $data);
- }
- return $this->redis->set($id, $data);
- }
-
- protected function doDelete($id)
- {
- return $this->redis->delete($id) >= 0;
- }
-
- protected function doFlush()
- {
- return $this->redis->flushDB();
- }
-
- protected function doGetStats()
- {
- $info = $this->redis->info();
- return array(
- Cache::STATS_HITS => $info['keyspace_hits'],
- Cache::STATS_MISSES => $info['keyspace_misses'],
- Cache::STATS_UPTIME => $info['uptime_in_seconds'],
- Cache::STATS_MEMORY_USAGE => $info['used_memory'],
- Cache::STATS_MEMORY_AVAILABLE => false
- );
- }
-
- protected function getSerializerValue()
- {
- if (defined('HHVM_VERSION')) {
- return Redis::SERIALIZER_PHP;
- }
- return defined('Redis::SERIALIZER_IGBINARY') ? Redis::SERIALIZER_IGBINARY : Redis::SERIALIZER_PHP;
- }
- }
|