Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 10 | class RedisAdapter extends AbstractAdapter implements AdapterInterface |
||
| 11 | { |
||
| 12 | const EXPIRE_RESOLUTION_EX = 'ex'; |
||
| 13 | const EXPIRE_RESOLUTION_PX = 'px'; |
||
| 14 | |||
| 15 | protected Client $client; |
||
|
|
|||
| 16 | protected int $ttl = 0; |
||
| 17 | protected array $config; |
||
| 18 | |||
| 19 | public function __construct(array $config = [], bool $lazy = true) |
||
| 20 | { |
||
| 21 | $this->config = $config; |
||
| 22 | |||
| 23 | if (!$lazy) { |
||
| 24 | $this->getClient(); |
||
| 25 | } |
||
| 26 | } |
||
| 27 | |||
| 28 | protected function getClient(): Client |
||
| 29 | { |
||
| 30 | if (!$this->client instanceof Client) { |
||
| 31 | $this->createClient($this->config); |
||
| 32 | } |
||
| 33 | |||
| 34 | return $this->client; |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @return mixed |
||
| 39 | */ |
||
| 40 | public function get(string $key) |
||
| 41 | { |
||
| 42 | $value = $this->getClient()->get($key); |
||
| 43 | |||
| 44 | if (empty($value) && $this->getClient()->exists($key) == 0) { |
||
| 45 | throw new KeyNotFoundException(); |
||
| 46 | } |
||
| 47 | |||
| 48 | return $value; |
||
| 49 | } |
||
| 50 | |||
| 51 | public function getMulti(array $keys): array |
||
| 52 | { |
||
| 53 | $result = $this->getClient()->mget($keys); |
||
| 54 | $values = []; |
||
| 55 | |||
| 56 | foreach ($keys as $index => $key) { |
||
| 57 | if ($result[$index] !== null) { |
||
| 58 | $values[$key] = $result[$index]; |
||
| 59 | } |
||
| 60 | } |
||
| 61 | |||
| 62 | return $values; |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @param mixed $value |
||
| 67 | */ |
||
| 68 | public function set(string $key, $value): bool |
||
| 69 | { |
||
| 70 | if ($this->ttl == 0) { |
||
| 71 | $result = $this->getClient()->set($key, $value); |
||
| 72 | } else { |
||
| 73 | $result = $this->getClient()->set($key, $value, static::EXPIRE_RESOLUTION_EX, $this->ttl); |
||
| 74 | } |
||
| 75 | |||
| 76 | return $result->getPayload() == 'OK'; |
||
| 77 | } |
||
| 78 | |||
| 79 | public function setMulti(array $data): bool |
||
| 80 | { |
||
| 81 | /** @var iterable $responses */ |
||
| 82 | $responses = $this->getClient()->pipeline( |
||
| 83 | /** @var Client $pipe */ |
||
| 84 | function ($pipe) use ($data): void { |
||
| 85 | foreach ($data as $key => $value) { |
||
| 86 | if ($this->ttl == 0) { |
||
| 87 | $pipe->set($key, $value); |
||
| 88 | } else { |
||
| 89 | $pipe->set($key, $value, static::EXPIRE_RESOLUTION_EX, $this->ttl); |
||
| 90 | } |
||
| 91 | } |
||
| 92 | } |
||
| 93 | ); |
||
| 94 | |||
| 95 | $result = true; |
||
| 96 | foreach ($responses as $response) { |
||
| 97 | /** @var \Predis\Response\Status $response */ |
||
| 98 | $result = $result && ($response->getPayload() == 'OK'); |
||
| 99 | } |
||
| 100 | |||
| 101 | return $result; |
||
| 102 | } |
||
| 103 | |||
| 104 | public function contains(string $key): bool |
||
| 105 | { |
||
| 106 | return (bool) $this->getClient()->exists($key); |
||
| 107 | } |
||
| 108 | |||
| 109 | public function delete(string $key): bool |
||
| 110 | { |
||
| 111 | $this->getClient()->del([$key]); |
||
| 112 | |||
| 113 | return true; |
||
| 114 | } |
||
| 115 | |||
| 116 | public function deleteMulti(array $keys): bool |
||
| 117 | { |
||
| 118 | $this->getClient()->del($keys); |
||
| 119 | |||
| 120 | return true; |
||
| 121 | } |
||
| 122 | |||
| 123 | public function flush(): bool |
||
| 124 | { |
||
| 125 | $result = $this->getClient()->flushAll(); |
||
| 126 | |||
| 127 | return $result->getPayload() == 'OK'; |
||
| 128 | } |
||
| 129 | |||
| 130 | public function setClient(Client $client): void |
||
| 131 | { |
||
| 132 | $this->client = $client; |
||
| 133 | } |
||
| 134 | |||
| 135 | protected function createClient(array $config): void |
||
| 136 | { |
||
| 137 | $this->client = new Client($this->getConnectionParameters($config), ['prefix' => null]); |
||
| 138 | |||
| 139 | if (isset($config['ttl'])) { |
||
| 140 | $this->ttl = (int) $config['ttl']; |
||
| 141 | } |
||
| 142 | |||
| 143 | if (isset($config['cache_not_found_keys'])) { |
||
| 144 | $this->cacheNotFoundKeys = (bool) $config['cache_not_found_keys']; |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | protected function getConnectionParameters(array $config): array |
||
| 149 | { |
||
| 150 | $connectionParameters = []; |
||
| 151 | $connectionParameters['host'] = $config['host'] ?? '127.0.0.1'; |
||
| 152 | $connectionParameters['port'] = $config['port'] ?? 6379; |
||
| 153 | if (isset($config['database'])) { |
||
| 154 | $connectionParameters['database'] = $config['database']; |
||
| 155 | } |
||
| 156 | if (isset($config['password'])) { |
||
| 157 | $connectionParameters['password'] = $config['password']; |
||
| 158 | } |
||
| 159 | if (isset($config['connection_persistent'])) { |
||
| 160 | $connectionParameters['connection_persistent'] = $config['connection_persistent']; |
||
| 161 | } |
||
| 162 | |||
| 163 | return $connectionParameters; |
||
| 164 | } |
||
| 165 | |||
| 166 | public function setNamespace(string $namespace): void |
||
| 167 | { |
||
| 168 | $this->getClient()->getOptions()->prefix->setPrefix($namespace . ':'); |
||
| 169 | parent::setNamespace($namespace); |
||
| 170 | } |
||
| 171 | |||
| 172 | public function setTtl(int $ttl): void |
||
| 173 | { |
||
| 174 | $this->ttl = $ttl; |
||
| 175 | } |
||
| 176 | } |
||
| 177 |