|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ByJG\Cache\Engine; |
| 4 | + |
| 5 | +use ByJG\Cache\CacheEngineInterface; |
| 6 | +use Memcached; |
| 7 | +use Psr\Log\NullLogger; |
| 8 | + |
| 9 | +class RedisCacheEngine implements CacheEngineInterface |
| 10 | +{ |
| 11 | + |
| 12 | + /** |
| 13 | + * |
| 14 | + * @var \Redis |
| 15 | + */ |
| 16 | + protected $redis = null; |
| 17 | + |
| 18 | + protected $logger = null; |
| 19 | + |
| 20 | + protected $server = null; |
| 21 | + |
| 22 | + protected $password = null; |
| 23 | + |
| 24 | + public function __construct($server = null, $password = null, $logger = null) |
| 25 | + { |
| 26 | + $this->server = $server; |
| 27 | + if (is_null($server)) { |
| 28 | + $this->server = '127.0.0.1:6379'; |
| 29 | + } |
| 30 | + |
| 31 | + $this->password = $password; |
| 32 | + |
| 33 | + $this->logger = $logger; |
| 34 | + if (is_null($logger)) { |
| 35 | + $this->logger = new NullLogger(); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + protected function lazyLoadRedisServer() |
| 40 | + { |
| 41 | + if (is_null($this->redis)) { |
| 42 | + $this->redis = new \Redis(); |
| 43 | + $data = explode(":", $this->server); |
| 44 | + $this->redis->connect($data[0], isset($data[1]) ? $data[1] : 6379); |
| 45 | + |
| 46 | + if (!empty($this->password)) { |
| 47 | + $this->redis->auth($this->password); |
| 48 | + } |
| 49 | + $this->redis->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_NONE); |
| 50 | + |
| 51 | + $this->redis->info('redis_version'); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @param string $key The object KEY |
| 57 | + * @param int $ttl IGNORED IN MEMCACHED. |
| 58 | + * @return object Description |
| 59 | + */ |
| 60 | + public function get($key, $ttl = 0) |
| 61 | + { |
| 62 | + $this->lazyLoadRedisServer(); |
| 63 | + |
| 64 | + $value = $this->redis->get($key); |
| 65 | + $this->logger->info("[Redis Cache] Get '$key' result "); |
| 66 | + |
| 67 | + return ($value === false ? null : $value); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @param string $key The object Key |
| 72 | + * @param object $object The object to be cached |
| 73 | + * @param int $ttl The time to live in seconds of this objects |
| 74 | + * @return bool If the object is successfully posted |
| 75 | + */ |
| 76 | + public function set($key, $object, $ttl = 0) |
| 77 | + { |
| 78 | + $this->lazyLoadRedisServer(); |
| 79 | + |
| 80 | + $this->redis->set($key, $object, $ttl); |
| 81 | + $this->logger->info("[Redis Cache] Set '$key' result "); |
| 82 | + |
| 83 | + return true; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Unlock resource |
| 88 | + * @param string $key |
| 89 | + */ |
| 90 | + public function release($key) |
| 91 | + { |
| 92 | + $this->lazyLoadRedisServer(); |
| 93 | + |
| 94 | + $this->redis->delete($key); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * |
| 99 | + * @param string $key |
| 100 | + * @param string $str |
| 101 | + * @return bool |
| 102 | + */ |
| 103 | + public function append($key, $str) |
| 104 | + { |
| 105 | + $this->lazyLoadRedisServer(); |
| 106 | + |
| 107 | + $this->logger->info("[Redis Cache] Append '$key' in Memcached"); |
| 108 | + return $this->redis->append($key, $str); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Lock resource before set it. |
| 113 | + * @param string $key |
| 114 | + */ |
| 115 | + public function lock($key) |
| 116 | + { |
| 117 | + $this->lazyLoadRedisServer(); |
| 118 | + |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * UnLock resource after set it |
| 124 | + * @param string $key |
| 125 | + */ |
| 126 | + public function unlock($key) |
| 127 | + { |
| 128 | + $this->lazyLoadRedisServer(); |
| 129 | + |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + public function isAvailable() |
| 134 | + { |
| 135 | + if (!class_exists('\Redis')) { |
| 136 | + return false; |
| 137 | + } |
| 138 | + |
| 139 | + try { |
| 140 | + $this->lazyLoadRedisServer(); |
| 141 | + return true; |
| 142 | + } catch (\Exception $ex) { |
| 143 | + return false; |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments