o2system /
cache
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * This file is part of the O2System Framework package. |
||
| 4 | * |
||
| 5 | * For the full copyright and license information, please view the LICENSE |
||
| 6 | * file that was distributed with this source code. |
||
| 7 | * |
||
| 8 | * @author Steeve Andrian Salim |
||
| 9 | * @copyright Copyright (c) Steeve Andrian Salim |
||
| 10 | */ |
||
| 11 | |||
| 12 | // ------------------------------------------------------------------------ |
||
| 13 | |||
| 14 | namespace O2System\Cache\Adapters\Redis; |
||
| 15 | |||
| 16 | // ------------------------------------------------------------------------ |
||
| 17 | |||
| 18 | use O2System\Cache\Abstracts\AbstractAdapter; |
||
| 19 | use O2System\Spl\Exceptions\Logic\DomainException; |
||
| 20 | use O2System\Spl\Exceptions\Logic\InvalidArgumentException; |
||
| 21 | use O2System\Spl\Exceptions\RuntimeException; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Class Adapter |
||
| 25 | * |
||
| 26 | * @package O2System\Cache\Adapters\File |
||
| 27 | */ |
||
| 28 | abstract class Adapter extends AbstractAdapter |
||
| 29 | {
|
||
| 30 | /** |
||
| 31 | * Adapter::$platform |
||
| 32 | * |
||
| 33 | * Adapter Platform Name |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $platform = 'Redis'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Adapter::$redis |
||
| 41 | * |
||
| 42 | * Redis Instance |
||
| 43 | * |
||
| 44 | * @var \Redis |
||
| 45 | */ |
||
| 46 | protected $redis; |
||
| 47 | |||
| 48 | // ------------------------------------------------------------------------ |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Adapter::connect |
||
| 52 | * |
||
| 53 | * @param array $config Cache adapter connection configuration. |
||
| 54 | * |
||
| 55 | * @return void |
||
| 56 | * @throws \O2System\Spl\Exceptions\RuntimeException |
||
| 57 | */ |
||
| 58 | public function connect(array $config) |
||
| 59 | {
|
||
| 60 | $this->config = array_merge( |
||
| 61 | [ |
||
| 62 | 'host' => '127.0.0.1', |
||
| 63 | 'port' => 6379, |
||
| 64 | 'password' => null, |
||
| 65 | 'timeout' => 0, |
||
| 66 | ], |
||
| 67 | $config |
||
| 68 | ); |
||
| 69 | |||
| 70 | $this->redis = new \Redis(); |
||
| 71 | |||
| 72 | try {
|
||
| 73 | if ( ! $this->redis->connect( |
||
| 74 | $this->config[ 'host' ], |
||
| 75 | ($this->config[ 'host' ][ 0 ] === '/' ? 0 |
||
| 76 | : $this->config[ 'port' ]), |
||
| 77 | $this->config[ 'timeout' ] |
||
| 78 | ) |
||
| 79 | ) {
|
||
| 80 | throw new RuntimeException('CACHE_REDIS_E_CONNECTION_FAILED');
|
||
| 81 | } |
||
| 82 | |||
| 83 | if (isset($this->config[ 'password' ]) AND ! $this->redis->auth($this->config[ 'password' ])) {
|
||
| 84 | throw new DomainException('CACHE_REDIS_E_AUTHENTICATION_FAILED');
|
||
| 85 | } |
||
| 86 | |||
| 87 | if (isset($this->config[ 'dbIndex' ]) AND ! $this->redis->select($this->config[ 'dbIndex' ])) {
|
||
| 88 | throw new RuntimeException('CACHE_REDIS_E_DB_CONNECTION_FAILED');
|
||
| 89 | } |
||
| 90 | } catch (\RedisException $e) {
|
||
| 91 | throw new RuntimeException('E_REDIS_ADAPTER_CONNECTION_REFUSED', $e->getCode(), [$e->getMessage()]);
|
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | // ------------------------------------------------------------------------ |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Adapter::increment |
||
| 99 | * |
||
| 100 | * Increment a raw value offset. |
||
| 101 | * |
||
| 102 | * @param string $key Cache item key. |
||
| 103 | * @param int $step Increment step to add. |
||
| 104 | * |
||
| 105 | * @throws \O2System\Spl\Exceptions\Logic\InvalidArgumentException |
||
| 106 | * If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException |
||
| 107 | * MUST be thrown. |
||
| 108 | * |
||
| 109 | * @return mixed New value on success or FALSE on failure. |
||
| 110 | */ |
||
| 111 | public function increment($key, $step = 1) |
||
| 112 | {
|
||
| 113 | if ( ! is_string($key)) {
|
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 114 | throw new InvalidArgumentException('E_INVALID_ARGUMENT_STRING_CACHE_EXCEPTION');
|
||
| 115 | } |
||
| 116 | |||
| 117 | return $this->redis->hIncrBy($this->prefixKey . $key, 'data', $step); |
||
| 118 | } |
||
| 119 | |||
| 120 | // ------------------------------------------------------------------------ |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Adapter::decrement |
||
| 124 | * |
||
| 125 | * Decrement a raw value offset. |
||
| 126 | * |
||
| 127 | * @param string $key Cache item key. |
||
| 128 | * @param int $step Decrement step to add. |
||
| 129 | * |
||
| 130 | * @throws \O2System\Spl\Exceptions\Logic\InvalidArgumentException |
||
| 131 | * If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException |
||
| 132 | * MUST be thrown. |
||
| 133 | * |
||
| 134 | * @return mixed New value on success or FALSE on failure. |
||
| 135 | */ |
||
| 136 | public function decrement($key, $step = 1) |
||
| 137 | {
|
||
| 138 | if ( ! is_string($key)) {
|
||
|
0 ignored issues
–
show
|
|||
| 139 | throw new InvalidArgumentException('E_INVALID_ARGUMENT_STRING_CACHE_EXCEPTION');
|
||
| 140 | } |
||
| 141 | |||
| 142 | return $this->redis->hIncrBy($this->prefixKey . $key, 'data', -$step); |
||
| 143 | } |
||
| 144 | |||
| 145 | // ------------------------------------------------------------------------ |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Adapter::getInfo |
||
| 149 | * |
||
| 150 | * Gets item pool adapter info. |
||
| 151 | * |
||
| 152 | * @return mixed |
||
| 153 | */ |
||
| 154 | public function getInfo() |
||
| 155 | {
|
||
| 156 | return call_user_func_array([&$this->redis, 'info'], func_get_args()); |
||
| 157 | } |
||
| 158 | |||
| 159 | // ------------------------------------------------------------------------ |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Adapter::getInfo |
||
| 163 | * |
||
| 164 | * Gets item pool adapter stats. |
||
| 165 | * |
||
| 166 | * @return mixed |
||
| 167 | */ |
||
| 168 | public function getStats() |
||
| 169 | {
|
||
| 170 | return $this->redis->info('stats');
|
||
| 171 | } |
||
| 172 | |||
| 173 | // ------------------------------------------------------------------------ |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Adapter::isSupported |
||
| 177 | * |
||
| 178 | * Checks if this adapter is supported on this system. |
||
| 179 | * |
||
| 180 | * @return bool Returns FALSE if not supported. |
||
| 181 | */ |
||
| 182 | public function isSupported() |
||
| 183 | {
|
||
| 184 | return (bool)(extension_loaded('redis') && class_exists('Redis', false));
|
||
| 185 | } |
||
| 186 | |||
| 187 | // ------------------------------------------------------------------------ |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Adapter::isConnected |
||
| 191 | * |
||
| 192 | * Checks if this adapter has a successful connection. |
||
| 193 | * |
||
| 194 | * @return bool Returns FALSE if not supported. |
||
| 195 | */ |
||
| 196 | public function isConnected() |
||
| 197 | {
|
||
| 198 | return (bool)($this->redis instanceof \Redis); |
||
| 199 | } |
||
| 200 | } |