@@ -27,34 +27,34 @@ |
||
| 27 | 27 | */ |
| 28 | 28 | class Item implements ExtendedCacheItemInterface |
| 29 | 29 | { |
| 30 | - use ItemBaseTrait { |
|
| 31 | - ItemBaseTrait::__construct as __BaseConstruct; |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Item constructor. |
|
| 36 | - * @param Driver $driver |
|
| 37 | - * @param $key |
|
| 38 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 39 | - */ |
|
| 40 | - public function __construct(RedisDriver $driver, $key) |
|
| 41 | - { |
|
| 42 | - $this->__BaseConstruct($driver, $key); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @param ExtendedCacheItemPoolInterface $driver |
|
| 47 | - * @return static |
|
| 48 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 49 | - */ |
|
| 50 | - public function setDriver(ExtendedCacheItemPoolInterface $driver) |
|
| 51 | - { |
|
| 52 | - if ($driver instanceof RedisDriver) { |
|
| 53 | - $this->driver = $driver; |
|
| 54 | - |
|
| 55 | - return $this; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - throw new PhpfastcacheInvalidArgumentException('Invalid driver instance'); |
|
| 59 | - } |
|
| 30 | + use ItemBaseTrait { |
|
| 31 | + ItemBaseTrait::__construct as __BaseConstruct; |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Item constructor. |
|
| 36 | + * @param Driver $driver |
|
| 37 | + * @param $key |
|
| 38 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 39 | + */ |
|
| 40 | + public function __construct(RedisDriver $driver, $key) |
|
| 41 | + { |
|
| 42 | + $this->__BaseConstruct($driver, $key); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @param ExtendedCacheItemPoolInterface $driver |
|
| 47 | + * @return static |
|
| 48 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 49 | + */ |
|
| 50 | + public function setDriver(ExtendedCacheItemPoolInterface $driver) |
|
| 51 | + { |
|
| 52 | + if ($driver instanceof RedisDriver) { |
|
| 53 | + $this->driver = $driver; |
|
| 54 | + |
|
| 55 | + return $this; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + throw new PhpfastcacheInvalidArgumentException('Invalid driver instance'); |
|
| 59 | + } |
|
| 60 | 60 | } |
| 61 | 61 | \ No newline at end of file |
@@ -33,159 +33,159 @@ |
||
| 33 | 33 | */ |
| 34 | 34 | class Driver implements ExtendedCacheItemPoolInterface, AggregatablePoolInterface |
| 35 | 35 | { |
| 36 | - use DriverBaseTrait; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * @return bool |
|
| 40 | - */ |
|
| 41 | - public function driverCheck(): bool |
|
| 42 | - { |
|
| 43 | - return extension_loaded('Redis'); |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @return DriverStatistic |
|
| 48 | - */ |
|
| 49 | - public function getStats(): DriverStatistic |
|
| 50 | - { |
|
| 51 | - // used_memory |
|
| 52 | - $info = $this->instance->info(); |
|
| 53 | - $date = (new DateTime())->setTimestamp(time() - $info['uptime_in_seconds']); |
|
| 54 | - |
|
| 55 | - return (new DriverStatistic()) |
|
| 56 | - ->setData(implode(', ', array_keys($this->itemInstances))) |
|
| 57 | - ->setRawData($info) |
|
| 58 | - ->setSize((int)$info['used_memory']) |
|
| 59 | - ->setInfo( |
|
| 60 | - sprintf( |
|
| 61 | - "The Redis daemon v%s is up since %s.\n For more information see RawData. \n Driver size includes the memory allocation size.", |
|
| 62 | - $info['redis_version'], |
|
| 63 | - $date->format(DATE_RFC2822) |
|
| 64 | - ) |
|
| 65 | - ); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * @return bool |
|
| 70 | - * @throws PhpfastcacheLogicException |
|
| 71 | - */ |
|
| 72 | - protected function driverConnect(): bool |
|
| 73 | - { |
|
| 74 | - if ($this->instance instanceof RedisClient) { |
|
| 75 | - throw new PhpfastcacheLogicException('Already connected to Redis server'); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * In case of an user-provided |
|
| 80 | - * Redis client just return here |
|
| 81 | - */ |
|
| 82 | - if ($this->getConfig()->getRedisClient() instanceof RedisClient) { |
|
| 83 | - /** |
|
| 84 | - * Unlike Predis, we can't test if we're are connected |
|
| 85 | - * or not, so let's just assume that we are |
|
| 86 | - */ |
|
| 87 | - $this->instance = $this->getConfig()->getRedisClient(); |
|
| 88 | - return true; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - $this->instance = $this->instance ?: new RedisClient(); |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * If path is provided we consider it as an UNIX Socket |
|
| 95 | - */ |
|
| 96 | - if ($this->getConfig()->getPath()) { |
|
| 97 | - $isConnected = $this->instance->connect($this->getConfig()->getPath()); |
|
| 98 | - } else { |
|
| 99 | - $isConnected = $this->instance->connect($this->getConfig()->getHost(), $this->getConfig()->getPort(), $this->getConfig()->getTimeout()); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - if (!$isConnected && $this->getConfig()->getPath()) { |
|
| 103 | - return false; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - if ($this->getConfig()->getOptPrefix()) { |
|
| 107 | - $this->instance->setOption(RedisClient::OPT_PREFIX, $this->getConfig()->getOptPrefix()); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - if ($this->getConfig()->getPassword() && !$this->instance->auth($this->getConfig()->getPassword())) { |
|
| 111 | - return false; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - if ($this->getConfig()->getDatabase() !== null) { |
|
| 115 | - $this->instance->select($this->getConfig()->getDatabase()); |
|
| 116 | - } |
|
| 117 | - return true; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * @param CacheItemInterface $item |
|
| 122 | - * @return null|array |
|
| 123 | - */ |
|
| 124 | - protected function driverRead(CacheItemInterface $item) |
|
| 125 | - { |
|
| 126 | - $val = $this->instance->get($item->getKey()); |
|
| 127 | - if ($val == false) { |
|
| 128 | - return null; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - return $this->decode($val); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * @param CacheItemInterface $item |
|
| 136 | - * @return mixed |
|
| 137 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 138 | - */ |
|
| 139 | - protected function driverWrite(CacheItemInterface $item): bool |
|
| 140 | - { |
|
| 141 | - /** |
|
| 142 | - * Check for Cross-Driver type confusion |
|
| 143 | - */ |
|
| 144 | - if ($item instanceof Item) { |
|
| 145 | - $ttl = $item->getExpirationDate()->getTimestamp() - time(); |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * @see https://redis.io/commands/setex |
|
| 149 | - * @see https://redis.io/commands/expire |
|
| 150 | - */ |
|
| 151 | - if ($ttl <= 0) { |
|
| 152 | - return $this->instance->expire($item->getKey(), 0); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - return $this->instance->setex($item->getKey(), $ttl, $this->encode($this->driverPreWrap($item))); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * @param CacheItemInterface $item |
|
| 163 | - * @return bool |
|
| 164 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 165 | - */ |
|
| 166 | - protected function driverDelete(CacheItemInterface $item): bool |
|
| 167 | - { |
|
| 168 | - /** |
|
| 169 | - * Check for Cross-Driver type confusion |
|
| 170 | - */ |
|
| 171 | - if ($item instanceof Item) { |
|
| 172 | - return (bool)$this->instance->del($item->getKey()); |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /******************** |
|
| 36 | + use DriverBaseTrait; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * @return bool |
|
| 40 | + */ |
|
| 41 | + public function driverCheck(): bool |
|
| 42 | + { |
|
| 43 | + return extension_loaded('Redis'); |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @return DriverStatistic |
|
| 48 | + */ |
|
| 49 | + public function getStats(): DriverStatistic |
|
| 50 | + { |
|
| 51 | + // used_memory |
|
| 52 | + $info = $this->instance->info(); |
|
| 53 | + $date = (new DateTime())->setTimestamp(time() - $info['uptime_in_seconds']); |
|
| 54 | + |
|
| 55 | + return (new DriverStatistic()) |
|
| 56 | + ->setData(implode(', ', array_keys($this->itemInstances))) |
|
| 57 | + ->setRawData($info) |
|
| 58 | + ->setSize((int)$info['used_memory']) |
|
| 59 | + ->setInfo( |
|
| 60 | + sprintf( |
|
| 61 | + "The Redis daemon v%s is up since %s.\n For more information see RawData. \n Driver size includes the memory allocation size.", |
|
| 62 | + $info['redis_version'], |
|
| 63 | + $date->format(DATE_RFC2822) |
|
| 64 | + ) |
|
| 65 | + ); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * @return bool |
|
| 70 | + * @throws PhpfastcacheLogicException |
|
| 71 | + */ |
|
| 72 | + protected function driverConnect(): bool |
|
| 73 | + { |
|
| 74 | + if ($this->instance instanceof RedisClient) { |
|
| 75 | + throw new PhpfastcacheLogicException('Already connected to Redis server'); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * In case of an user-provided |
|
| 80 | + * Redis client just return here |
|
| 81 | + */ |
|
| 82 | + if ($this->getConfig()->getRedisClient() instanceof RedisClient) { |
|
| 83 | + /** |
|
| 84 | + * Unlike Predis, we can't test if we're are connected |
|
| 85 | + * or not, so let's just assume that we are |
|
| 86 | + */ |
|
| 87 | + $this->instance = $this->getConfig()->getRedisClient(); |
|
| 88 | + return true; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + $this->instance = $this->instance ?: new RedisClient(); |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * If path is provided we consider it as an UNIX Socket |
|
| 95 | + */ |
|
| 96 | + if ($this->getConfig()->getPath()) { |
|
| 97 | + $isConnected = $this->instance->connect($this->getConfig()->getPath()); |
|
| 98 | + } else { |
|
| 99 | + $isConnected = $this->instance->connect($this->getConfig()->getHost(), $this->getConfig()->getPort(), $this->getConfig()->getTimeout()); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + if (!$isConnected && $this->getConfig()->getPath()) { |
|
| 103 | + return false; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + if ($this->getConfig()->getOptPrefix()) { |
|
| 107 | + $this->instance->setOption(RedisClient::OPT_PREFIX, $this->getConfig()->getOptPrefix()); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + if ($this->getConfig()->getPassword() && !$this->instance->auth($this->getConfig()->getPassword())) { |
|
| 111 | + return false; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + if ($this->getConfig()->getDatabase() !== null) { |
|
| 115 | + $this->instance->select($this->getConfig()->getDatabase()); |
|
| 116 | + } |
|
| 117 | + return true; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * @param CacheItemInterface $item |
|
| 122 | + * @return null|array |
|
| 123 | + */ |
|
| 124 | + protected function driverRead(CacheItemInterface $item) |
|
| 125 | + { |
|
| 126 | + $val = $this->instance->get($item->getKey()); |
|
| 127 | + if ($val == false) { |
|
| 128 | + return null; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + return $this->decode($val); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * @param CacheItemInterface $item |
|
| 136 | + * @return mixed |
|
| 137 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 138 | + */ |
|
| 139 | + protected function driverWrite(CacheItemInterface $item): bool |
|
| 140 | + { |
|
| 141 | + /** |
|
| 142 | + * Check for Cross-Driver type confusion |
|
| 143 | + */ |
|
| 144 | + if ($item instanceof Item) { |
|
| 145 | + $ttl = $item->getExpirationDate()->getTimestamp() - time(); |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * @see https://redis.io/commands/setex |
|
| 149 | + * @see https://redis.io/commands/expire |
|
| 150 | + */ |
|
| 151 | + if ($ttl <= 0) { |
|
| 152 | + return $this->instance->expire($item->getKey(), 0); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + return $this->instance->setex($item->getKey(), $ttl, $this->encode($this->driverPreWrap($item))); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * @param CacheItemInterface $item |
|
| 163 | + * @return bool |
|
| 164 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 165 | + */ |
|
| 166 | + protected function driverDelete(CacheItemInterface $item): bool |
|
| 167 | + { |
|
| 168 | + /** |
|
| 169 | + * Check for Cross-Driver type confusion |
|
| 170 | + */ |
|
| 171 | + if ($item instanceof Item) { |
|
| 172 | + return (bool)$this->instance->del($item->getKey()); |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /******************** |
|
| 179 | 179 | * |
| 180 | 180 | * PSR-6 Extended Methods |
| 181 | 181 | * |
| 182 | 182 | *******************/ |
| 183 | 183 | |
| 184 | - /** |
|
| 185 | - * @return bool |
|
| 186 | - */ |
|
| 187 | - protected function driverClear(): bool |
|
| 188 | - { |
|
| 189 | - return $this->instance->flushDB(); |
|
| 190 | - } |
|
| 184 | + /** |
|
| 185 | + * @return bool |
|
| 186 | + */ |
|
| 187 | + protected function driverClear(): bool |
|
| 188 | + { |
|
| 189 | + return $this->instance->flushDB(); |
|
| 190 | + } |
|
| 191 | 191 | } |
@@ -23,230 +23,230 @@ |
||
| 23 | 23 | |
| 24 | 24 | class Config extends ConfigurationOption |
| 25 | 25 | { |
| 26 | - /** |
|
| 27 | - * @var string |
|
| 28 | - */ |
|
| 29 | - protected $host = '127.0.0.1'; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @var int |
|
| 33 | - */ |
|
| 34 | - protected $port = 6379; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * @var string |
|
| 38 | - */ |
|
| 39 | - protected $password = ''; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @var int |
|
| 43 | - */ |
|
| 44 | - protected $database = 0; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @var Client |
|
| 48 | - */ |
|
| 49 | - protected $predisClient; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @var string |
|
| 53 | - */ |
|
| 54 | - protected $optPrefix = ''; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * @var int |
|
| 58 | - */ |
|
| 59 | - protected $timeout = 5; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @var bool |
|
| 63 | - */ |
|
| 64 | - protected $persistent = false; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * @var string |
|
| 68 | - */ |
|
| 69 | - protected $scheme = 'unix'; |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * @return array |
|
| 73 | - */ |
|
| 74 | - public function getPredisConfigArray(): array |
|
| 75 | - { |
|
| 76 | - return [ |
|
| 77 | - 'host' => $this->getHost(), |
|
| 78 | - 'port' => $this->getPort(), |
|
| 79 | - 'password' => $this->getPassword() ?: null, |
|
| 80 | - 'database' => $this->getDatabase(), |
|
| 81 | - 'timeout' => $this->getTimeout(), |
|
| 82 | - ]; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * @return string |
|
| 87 | - */ |
|
| 88 | - public function getHost(): string |
|
| 89 | - { |
|
| 90 | - return $this->host; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * @param string $host |
|
| 95 | - * @return Config |
|
| 96 | - */ |
|
| 97 | - public function setHost(string $host): self |
|
| 98 | - { |
|
| 99 | - $this->host = $host; |
|
| 100 | - return $this; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * @return int |
|
| 105 | - */ |
|
| 106 | - public function getPort(): int |
|
| 107 | - { |
|
| 108 | - return $this->port; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * @param int $port |
|
| 113 | - * @return Config |
|
| 114 | - */ |
|
| 115 | - public function setPort(int $port): self |
|
| 116 | - { |
|
| 117 | - $this->port = $port; |
|
| 118 | - return $this; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * @return null |
|
| 123 | - */ |
|
| 124 | - public function getPassword() |
|
| 125 | - { |
|
| 126 | - return $this->password; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * @param null $password |
|
| 131 | - * @return self |
|
| 132 | - */ |
|
| 133 | - public function setPassword(string $password): self |
|
| 134 | - { |
|
| 135 | - $this->password = $password; |
|
| 136 | - return $this; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * @return int |
|
| 141 | - */ |
|
| 142 | - public function getDatabase(): int |
|
| 143 | - { |
|
| 144 | - return $this->database; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * @param int $database |
|
| 149 | - * @return Config |
|
| 150 | - */ |
|
| 151 | - public function setDatabase(int $database): self |
|
| 152 | - { |
|
| 153 | - $this->database = $database; |
|
| 154 | - return $this; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * @return int |
|
| 159 | - */ |
|
| 160 | - public function getTimeout(): int |
|
| 161 | - { |
|
| 162 | - return $this->timeout; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * @param int $timeout |
|
| 167 | - * @return self |
|
| 168 | - */ |
|
| 169 | - public function setTimeout(int $timeout): self |
|
| 170 | - { |
|
| 171 | - $this->timeout = $timeout; |
|
| 172 | - return $this; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * @return Client|null |
|
| 177 | - */ |
|
| 178 | - public function getPredisClient() |
|
| 179 | - { |
|
| 180 | - return $this->predisClient; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * @param Client $predisClient |null |
|
| 185 | - * @return Config |
|
| 186 | - */ |
|
| 187 | - public function setPredisClient(Client $predisClient = null): Config |
|
| 188 | - { |
|
| 189 | - $this->predisClient = $predisClient; |
|
| 190 | - return $this; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * @return string |
|
| 195 | - * @since 7.0.2 |
|
| 196 | - */ |
|
| 197 | - public function getOptPrefix(): string |
|
| 198 | - { |
|
| 199 | - return $this->optPrefix; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * @param string $optPrefix |
|
| 204 | - * @return Config |
|
| 205 | - * @since 7.0.2 |
|
| 206 | - */ |
|
| 207 | - public function setOptPrefix(string $optPrefix): Config |
|
| 208 | - { |
|
| 209 | - $this->optPrefix = trim($optPrefix); |
|
| 210 | - return $this; |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - /** |
|
| 214 | - * @return bool |
|
| 215 | - */ |
|
| 216 | - public function isPersistent(): bool |
|
| 217 | - { |
|
| 218 | - return $this->persistent; |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - /** |
|
| 222 | - * @param bool $persistent |
|
| 223 | - * @return Config |
|
| 224 | - */ |
|
| 225 | - public function setPersistent(bool $persistent): Config |
|
| 226 | - { |
|
| 227 | - $this->persistent = $persistent; |
|
| 228 | - return $this; |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - /** |
|
| 232 | - * @return string |
|
| 233 | - */ |
|
| 234 | - public function getScheme(): string |
|
| 235 | - { |
|
| 236 | - return $this->scheme; |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * @param string $scheme |
|
| 241 | - * @return Config |
|
| 242 | - * @throws PhpfastcacheInvalidConfigurationException |
|
| 243 | - */ |
|
| 244 | - public function setScheme(string $scheme): Config |
|
| 245 | - { |
|
| 246 | - if (!in_array($scheme, ['unix', 'tls'], true)) { |
|
| 247 | - throw new PhpfastcacheInvalidConfigurationException('Invalid scheme: ' . $scheme); |
|
| 248 | - } |
|
| 249 | - $this->scheme = $scheme; |
|
| 250 | - return $this; |
|
| 251 | - } |
|
| 26 | + /** |
|
| 27 | + * @var string |
|
| 28 | + */ |
|
| 29 | + protected $host = '127.0.0.1'; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @var int |
|
| 33 | + */ |
|
| 34 | + protected $port = 6379; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * @var string |
|
| 38 | + */ |
|
| 39 | + protected $password = ''; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @var int |
|
| 43 | + */ |
|
| 44 | + protected $database = 0; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @var Client |
|
| 48 | + */ |
|
| 49 | + protected $predisClient; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @var string |
|
| 53 | + */ |
|
| 54 | + protected $optPrefix = ''; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * @var int |
|
| 58 | + */ |
|
| 59 | + protected $timeout = 5; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @var bool |
|
| 63 | + */ |
|
| 64 | + protected $persistent = false; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * @var string |
|
| 68 | + */ |
|
| 69 | + protected $scheme = 'unix'; |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * @return array |
|
| 73 | + */ |
|
| 74 | + public function getPredisConfigArray(): array |
|
| 75 | + { |
|
| 76 | + return [ |
|
| 77 | + 'host' => $this->getHost(), |
|
| 78 | + 'port' => $this->getPort(), |
|
| 79 | + 'password' => $this->getPassword() ?: null, |
|
| 80 | + 'database' => $this->getDatabase(), |
|
| 81 | + 'timeout' => $this->getTimeout(), |
|
| 82 | + ]; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * @return string |
|
| 87 | + */ |
|
| 88 | + public function getHost(): string |
|
| 89 | + { |
|
| 90 | + return $this->host; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * @param string $host |
|
| 95 | + * @return Config |
|
| 96 | + */ |
|
| 97 | + public function setHost(string $host): self |
|
| 98 | + { |
|
| 99 | + $this->host = $host; |
|
| 100 | + return $this; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * @return int |
|
| 105 | + */ |
|
| 106 | + public function getPort(): int |
|
| 107 | + { |
|
| 108 | + return $this->port; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * @param int $port |
|
| 113 | + * @return Config |
|
| 114 | + */ |
|
| 115 | + public function setPort(int $port): self |
|
| 116 | + { |
|
| 117 | + $this->port = $port; |
|
| 118 | + return $this; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * @return null |
|
| 123 | + */ |
|
| 124 | + public function getPassword() |
|
| 125 | + { |
|
| 126 | + return $this->password; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * @param null $password |
|
| 131 | + * @return self |
|
| 132 | + */ |
|
| 133 | + public function setPassword(string $password): self |
|
| 134 | + { |
|
| 135 | + $this->password = $password; |
|
| 136 | + return $this; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * @return int |
|
| 141 | + */ |
|
| 142 | + public function getDatabase(): int |
|
| 143 | + { |
|
| 144 | + return $this->database; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * @param int $database |
|
| 149 | + * @return Config |
|
| 150 | + */ |
|
| 151 | + public function setDatabase(int $database): self |
|
| 152 | + { |
|
| 153 | + $this->database = $database; |
|
| 154 | + return $this; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * @return int |
|
| 159 | + */ |
|
| 160 | + public function getTimeout(): int |
|
| 161 | + { |
|
| 162 | + return $this->timeout; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * @param int $timeout |
|
| 167 | + * @return self |
|
| 168 | + */ |
|
| 169 | + public function setTimeout(int $timeout): self |
|
| 170 | + { |
|
| 171 | + $this->timeout = $timeout; |
|
| 172 | + return $this; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * @return Client|null |
|
| 177 | + */ |
|
| 178 | + public function getPredisClient() |
|
| 179 | + { |
|
| 180 | + return $this->predisClient; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * @param Client $predisClient |null |
|
| 185 | + * @return Config |
|
| 186 | + */ |
|
| 187 | + public function setPredisClient(Client $predisClient = null): Config |
|
| 188 | + { |
|
| 189 | + $this->predisClient = $predisClient; |
|
| 190 | + return $this; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * @return string |
|
| 195 | + * @since 7.0.2 |
|
| 196 | + */ |
|
| 197 | + public function getOptPrefix(): string |
|
| 198 | + { |
|
| 199 | + return $this->optPrefix; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * @param string $optPrefix |
|
| 204 | + * @return Config |
|
| 205 | + * @since 7.0.2 |
|
| 206 | + */ |
|
| 207 | + public function setOptPrefix(string $optPrefix): Config |
|
| 208 | + { |
|
| 209 | + $this->optPrefix = trim($optPrefix); |
|
| 210 | + return $this; |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + /** |
|
| 214 | + * @return bool |
|
| 215 | + */ |
|
| 216 | + public function isPersistent(): bool |
|
| 217 | + { |
|
| 218 | + return $this->persistent; |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + /** |
|
| 222 | + * @param bool $persistent |
|
| 223 | + * @return Config |
|
| 224 | + */ |
|
| 225 | + public function setPersistent(bool $persistent): Config |
|
| 226 | + { |
|
| 227 | + $this->persistent = $persistent; |
|
| 228 | + return $this; |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + /** |
|
| 232 | + * @return string |
|
| 233 | + */ |
|
| 234 | + public function getScheme(): string |
|
| 235 | + { |
|
| 236 | + return $this->scheme; |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * @param string $scheme |
|
| 241 | + * @return Config |
|
| 242 | + * @throws PhpfastcacheInvalidConfigurationException |
|
| 243 | + */ |
|
| 244 | + public function setScheme(string $scheme): Config |
|
| 245 | + { |
|
| 246 | + if (!in_array($scheme, ['unix', 'tls'], true)) { |
|
| 247 | + throw new PhpfastcacheInvalidConfigurationException('Invalid scheme: ' . $scheme); |
|
| 248 | + } |
|
| 249 | + $this->scheme = $scheme; |
|
| 250 | + return $this; |
|
| 251 | + } |
|
| 252 | 252 | } |
| 253 | 253 | \ No newline at end of file |
@@ -27,34 +27,34 @@ |
||
| 27 | 27 | */ |
| 28 | 28 | class Item implements ExtendedCacheItemInterface |
| 29 | 29 | { |
| 30 | - use ItemBaseTrait { |
|
| 31 | - ItemBaseTrait::__construct as __BaseConstruct; |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Item constructor. |
|
| 36 | - * @param Driver $driver |
|
| 37 | - * @param $key |
|
| 38 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 39 | - */ |
|
| 40 | - public function __construct(PredisDriver $driver, $key) |
|
| 41 | - { |
|
| 42 | - $this->__BaseConstruct($driver, $key); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @param ExtendedCacheItemPoolInterface $driver |
|
| 47 | - * @return static |
|
| 48 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 49 | - */ |
|
| 50 | - public function setDriver(ExtendedCacheItemPoolInterface $driver) |
|
| 51 | - { |
|
| 52 | - if ($driver instanceof PredisDriver) { |
|
| 53 | - $this->driver = $driver; |
|
| 54 | - |
|
| 55 | - return $this; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - throw new PhpfastcacheInvalidArgumentException('Invalid driver instance'); |
|
| 59 | - } |
|
| 30 | + use ItemBaseTrait { |
|
| 31 | + ItemBaseTrait::__construct as __BaseConstruct; |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Item constructor. |
|
| 36 | + * @param Driver $driver |
|
| 37 | + * @param $key |
|
| 38 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 39 | + */ |
|
| 40 | + public function __construct(PredisDriver $driver, $key) |
|
| 41 | + { |
|
| 42 | + $this->__BaseConstruct($driver, $key); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @param ExtendedCacheItemPoolInterface $driver |
|
| 47 | + * @return static |
|
| 48 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 49 | + */ |
|
| 50 | + public function setDriver(ExtendedCacheItemPoolInterface $driver) |
|
| 51 | + { |
|
| 52 | + if ($driver instanceof PredisDriver) { |
|
| 53 | + $this->driver = $driver; |
|
| 54 | + |
|
| 55 | + return $this; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + throw new PhpfastcacheInvalidArgumentException('Invalid driver instance'); |
|
| 59 | + } |
|
| 60 | 60 | } |
| 61 | 61 | \ No newline at end of file |
@@ -35,180 +35,180 @@ |
||
| 35 | 35 | */ |
| 36 | 36 | class Driver implements ExtendedCacheItemPoolInterface, AggregatablePoolInterface |
| 37 | 37 | { |
| 38 | - use DriverBaseTrait; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @return bool |
|
| 42 | - */ |
|
| 43 | - public function driverCheck(): bool |
|
| 44 | - { |
|
| 45 | - if (extension_loaded('Redis')) { |
|
| 46 | - trigger_error('The native Redis extension is installed, you should use Redis instead of Predis to increase performances', E_USER_NOTICE); |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - return class_exists('Predis\Client'); |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @return string |
|
| 54 | - */ |
|
| 55 | - public function getHelp(): string |
|
| 56 | - { |
|
| 57 | - return <<<HELP |
|
| 38 | + use DriverBaseTrait; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @return bool |
|
| 42 | + */ |
|
| 43 | + public function driverCheck(): bool |
|
| 44 | + { |
|
| 45 | + if (extension_loaded('Redis')) { |
|
| 46 | + trigger_error('The native Redis extension is installed, you should use Redis instead of Predis to increase performances', E_USER_NOTICE); |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + return class_exists('Predis\Client'); |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @return string |
|
| 54 | + */ |
|
| 55 | + public function getHelp(): string |
|
| 56 | + { |
|
| 57 | + return <<<HELP |
|
| 58 | 58 | <p> |
| 59 | 59 | To install the Predis library via Composer: |
| 60 | 60 | <code>composer require "predis/predis" "~1.1.0"</code> |
| 61 | 61 | </p> |
| 62 | 62 | HELP; |
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * @return DriverStatistic |
|
| 67 | - */ |
|
| 68 | - public function getStats(): DriverStatistic |
|
| 69 | - { |
|
| 70 | - $info = $this->instance->info(); |
|
| 71 | - $size = (isset($info['Memory']['used_memory']) ? $info['Memory']['used_memory'] : 0); |
|
| 72 | - $version = (isset($info['Server']['redis_version']) ? $info['Server']['redis_version'] : 0); |
|
| 73 | - $date = (isset($info['Server']['uptime_in_seconds']) ? (new DateTime())->setTimestamp(time() - $info['Server']['uptime_in_seconds']) : 'unknown date'); |
|
| 74 | - |
|
| 75 | - return (new DriverStatistic()) |
|
| 76 | - ->setData(implode(', ', array_keys($this->itemInstances))) |
|
| 77 | - ->setRawData($info) |
|
| 78 | - ->setSize((int)$size) |
|
| 79 | - ->setInfo( |
|
| 80 | - sprintf( |
|
| 81 | - "The Redis daemon v%s is up since %s.\n For more information see RawData. \n Driver size includes the memory allocation size.", |
|
| 82 | - $version, |
|
| 83 | - $date->format(DATE_RFC2822) |
|
| 84 | - ) |
|
| 85 | - ); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * @return bool |
|
| 90 | - * @throws PhpfastcacheDriverException |
|
| 91 | - * @throws PhpfastcacheLogicException |
|
| 92 | - */ |
|
| 93 | - protected function driverConnect(): bool |
|
| 94 | - { |
|
| 95 | - if ($this->instance instanceof PredisClient) { |
|
| 96 | - throw new PhpfastcacheLogicException('Already connected to Predis server'); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * In case of an user-provided |
|
| 101 | - * Predis client just return here |
|
| 102 | - */ |
|
| 103 | - if ($this->getConfig()->getPredisClient() instanceof PredisClient) { |
|
| 104 | - $this->instance = $this->getConfig()->getPredisClient(); |
|
| 105 | - if (!$this->instance->isConnected()) { |
|
| 106 | - $this->instance->connect(); |
|
| 107 | - } |
|
| 108 | - return true; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - $options = []; |
|
| 112 | - |
|
| 113 | - if ($this->getConfig()->getOptPrefix()) { |
|
| 114 | - $options['prefix'] = $this->getConfig()->getOptPrefix(); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - if (!empty($this->getConfig()->getPath())) { |
|
| 118 | - $this->instance = new PredisClient( |
|
| 119 | - [ |
|
| 120 | - 'scheme' => $this->getConfig()->getScheme(), |
|
| 121 | - 'persistent' => $this->getConfig()->isPersistent(), |
|
| 122 | - 'timeout' => $this->getConfig()->getTimeout(), |
|
| 123 | - 'path' => $this->getConfig()->getPath(), |
|
| 124 | - ], $options |
|
| 125 | - ); |
|
| 126 | - } else { |
|
| 127 | - $this->instance = new PredisClient($this->getConfig()->getPredisConfigArray(), $options); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - try { |
|
| 131 | - $this->instance->connect(); |
|
| 132 | - } catch (PredisConnectionException $e) { |
|
| 133 | - throw new PhpfastcacheDriverException( |
|
| 134 | - 'Failed to connect to predis server. Check the Predis documentation: https://github.com/nrk/predis/tree/v1.1#how-to-install-and-use-predis', |
|
| 135 | - 0, |
|
| 136 | - $e |
|
| 137 | - ); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - return true; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * @param CacheItemInterface $item |
|
| 145 | - * @return null|array |
|
| 146 | - */ |
|
| 147 | - protected function driverRead(CacheItemInterface $item) |
|
| 148 | - { |
|
| 149 | - $val = $this->instance->get($item->getKey()); |
|
| 150 | - if ($val == false) { |
|
| 151 | - return null; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - return $this->decode($val); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * @param CacheItemInterface $item |
|
| 159 | - * @return mixed |
|
| 160 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 161 | - */ |
|
| 162 | - protected function driverWrite(CacheItemInterface $item): bool |
|
| 163 | - { |
|
| 164 | - /** |
|
| 165 | - * Check for Cross-Driver type confusion |
|
| 166 | - */ |
|
| 167 | - if ($item instanceof Item) { |
|
| 168 | - $ttl = $item->getExpirationDate()->getTimestamp() - time(); |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * @see https://redis.io/commands/setex |
|
| 172 | - * @see https://redis.io/commands/expire |
|
| 173 | - */ |
|
| 174 | - if ($ttl <= 0) { |
|
| 175 | - return (bool)$this->instance->expire($item->getKey(), 0); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - return $this->instance->setex($item->getKey(), $ttl, $this->encode($this->driverPreWrap($item)))->getPayload() === 'OK'; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - /******************** |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * @return DriverStatistic |
|
| 67 | + */ |
|
| 68 | + public function getStats(): DriverStatistic |
|
| 69 | + { |
|
| 70 | + $info = $this->instance->info(); |
|
| 71 | + $size = (isset($info['Memory']['used_memory']) ? $info['Memory']['used_memory'] : 0); |
|
| 72 | + $version = (isset($info['Server']['redis_version']) ? $info['Server']['redis_version'] : 0); |
|
| 73 | + $date = (isset($info['Server']['uptime_in_seconds']) ? (new DateTime())->setTimestamp(time() - $info['Server']['uptime_in_seconds']) : 'unknown date'); |
|
| 74 | + |
|
| 75 | + return (new DriverStatistic()) |
|
| 76 | + ->setData(implode(', ', array_keys($this->itemInstances))) |
|
| 77 | + ->setRawData($info) |
|
| 78 | + ->setSize((int)$size) |
|
| 79 | + ->setInfo( |
|
| 80 | + sprintf( |
|
| 81 | + "The Redis daemon v%s is up since %s.\n For more information see RawData. \n Driver size includes the memory allocation size.", |
|
| 82 | + $version, |
|
| 83 | + $date->format(DATE_RFC2822) |
|
| 84 | + ) |
|
| 85 | + ); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * @return bool |
|
| 90 | + * @throws PhpfastcacheDriverException |
|
| 91 | + * @throws PhpfastcacheLogicException |
|
| 92 | + */ |
|
| 93 | + protected function driverConnect(): bool |
|
| 94 | + { |
|
| 95 | + if ($this->instance instanceof PredisClient) { |
|
| 96 | + throw new PhpfastcacheLogicException('Already connected to Predis server'); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * In case of an user-provided |
|
| 101 | + * Predis client just return here |
|
| 102 | + */ |
|
| 103 | + if ($this->getConfig()->getPredisClient() instanceof PredisClient) { |
|
| 104 | + $this->instance = $this->getConfig()->getPredisClient(); |
|
| 105 | + if (!$this->instance->isConnected()) { |
|
| 106 | + $this->instance->connect(); |
|
| 107 | + } |
|
| 108 | + return true; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + $options = []; |
|
| 112 | + |
|
| 113 | + if ($this->getConfig()->getOptPrefix()) { |
|
| 114 | + $options['prefix'] = $this->getConfig()->getOptPrefix(); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + if (!empty($this->getConfig()->getPath())) { |
|
| 118 | + $this->instance = new PredisClient( |
|
| 119 | + [ |
|
| 120 | + 'scheme' => $this->getConfig()->getScheme(), |
|
| 121 | + 'persistent' => $this->getConfig()->isPersistent(), |
|
| 122 | + 'timeout' => $this->getConfig()->getTimeout(), |
|
| 123 | + 'path' => $this->getConfig()->getPath(), |
|
| 124 | + ], $options |
|
| 125 | + ); |
|
| 126 | + } else { |
|
| 127 | + $this->instance = new PredisClient($this->getConfig()->getPredisConfigArray(), $options); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + try { |
|
| 131 | + $this->instance->connect(); |
|
| 132 | + } catch (PredisConnectionException $e) { |
|
| 133 | + throw new PhpfastcacheDriverException( |
|
| 134 | + 'Failed to connect to predis server. Check the Predis documentation: https://github.com/nrk/predis/tree/v1.1#how-to-install-and-use-predis', |
|
| 135 | + 0, |
|
| 136 | + $e |
|
| 137 | + ); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + return true; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * @param CacheItemInterface $item |
|
| 145 | + * @return null|array |
|
| 146 | + */ |
|
| 147 | + protected function driverRead(CacheItemInterface $item) |
|
| 148 | + { |
|
| 149 | + $val = $this->instance->get($item->getKey()); |
|
| 150 | + if ($val == false) { |
|
| 151 | + return null; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + return $this->decode($val); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * @param CacheItemInterface $item |
|
| 159 | + * @return mixed |
|
| 160 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 161 | + */ |
|
| 162 | + protected function driverWrite(CacheItemInterface $item): bool |
|
| 163 | + { |
|
| 164 | + /** |
|
| 165 | + * Check for Cross-Driver type confusion |
|
| 166 | + */ |
|
| 167 | + if ($item instanceof Item) { |
|
| 168 | + $ttl = $item->getExpirationDate()->getTimestamp() - time(); |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * @see https://redis.io/commands/setex |
|
| 172 | + * @see https://redis.io/commands/expire |
|
| 173 | + */ |
|
| 174 | + if ($ttl <= 0) { |
|
| 175 | + return (bool)$this->instance->expire($item->getKey(), 0); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + return $this->instance->setex($item->getKey(), $ttl, $this->encode($this->driverPreWrap($item)))->getPayload() === 'OK'; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + /******************** |
|
| 185 | 185 | * |
| 186 | 186 | * PSR-6 Extended Methods |
| 187 | 187 | * |
| 188 | 188 | *******************/ |
| 189 | 189 | |
| 190 | - /** |
|
| 191 | - * @param CacheItemInterface $item |
|
| 192 | - * @return bool |
|
| 193 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 194 | - */ |
|
| 195 | - protected function driverDelete(CacheItemInterface $item): bool |
|
| 196 | - { |
|
| 197 | - /** |
|
| 198 | - * Check for Cross-Driver type confusion |
|
| 199 | - */ |
|
| 200 | - if ($item instanceof Item) { |
|
| 201 | - return (bool)$this->instance->del([$item->getKey()]); |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * @return bool |
|
| 209 | - */ |
|
| 210 | - protected function driverClear(): bool |
|
| 211 | - { |
|
| 212 | - return $this->instance->flushdb()->getPayload() === 'OK'; |
|
| 213 | - } |
|
| 190 | + /** |
|
| 191 | + * @param CacheItemInterface $item |
|
| 192 | + * @return bool |
|
| 193 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 194 | + */ |
|
| 195 | + protected function driverDelete(CacheItemInterface $item): bool |
|
| 196 | + { |
|
| 197 | + /** |
|
| 198 | + * Check for Cross-Driver type confusion |
|
| 199 | + */ |
|
| 200 | + if ($item instanceof Item) { |
|
| 201 | + return (bool)$this->instance->del([$item->getKey()]); |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * @return bool |
|
| 209 | + */ |
|
| 210 | + protected function driverClear(): bool |
|
| 211 | + { |
|
| 212 | + return $this->instance->flushdb()->getPayload() === 'OK'; |
|
| 213 | + } |
|
| 214 | 214 | } |
@@ -20,5 +20,5 @@ |
||
| 20 | 20 | |
| 21 | 21 | class Config extends ConfigurationOption |
| 22 | 22 | { |
| 23 | - use IOConfigurationOptionTrait; |
|
| 23 | + use IOConfigurationOptionTrait; |
|
| 24 | 24 | } |
| 25 | 25 | \ No newline at end of file |
@@ -27,34 +27,34 @@ |
||
| 27 | 27 | */ |
| 28 | 28 | class Item implements ExtendedCacheItemInterface |
| 29 | 29 | { |
| 30 | - use ItemBaseTrait { |
|
| 31 | - ItemBaseTrait::__construct as __BaseConstruct; |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Item constructor. |
|
| 36 | - * @param Driver $driver |
|
| 37 | - * @param $key |
|
| 38 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 39 | - */ |
|
| 40 | - public function __construct(SqliteDriver $driver, $key) |
|
| 41 | - { |
|
| 42 | - $this->__BaseConstruct($driver, $key); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @param ExtendedCacheItemPoolInterface $driver |
|
| 47 | - * @return static |
|
| 48 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 49 | - */ |
|
| 50 | - public function setDriver(ExtendedCacheItemPoolInterface $driver) |
|
| 51 | - { |
|
| 52 | - if ($driver instanceof SqliteDriver) { |
|
| 53 | - $this->driver = $driver; |
|
| 54 | - |
|
| 55 | - return $this; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - throw new PhpfastcacheInvalidArgumentException('Invalid driver instance'); |
|
| 59 | - } |
|
| 30 | + use ItemBaseTrait { |
|
| 31 | + ItemBaseTrait::__construct as __BaseConstruct; |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Item constructor. |
|
| 36 | + * @param Driver $driver |
|
| 37 | + * @param $key |
|
| 38 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 39 | + */ |
|
| 40 | + public function __construct(SqliteDriver $driver, $key) |
|
| 41 | + { |
|
| 42 | + $this->__BaseConstruct($driver, $key); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @param ExtendedCacheItemPoolInterface $driver |
|
| 47 | + * @return static |
|
| 48 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 49 | + */ |
|
| 50 | + public function setDriver(ExtendedCacheItemPoolInterface $driver) |
|
| 51 | + { |
|
| 52 | + if ($driver instanceof SqliteDriver) { |
|
| 53 | + $this->driver = $driver; |
|
| 54 | + |
|
| 55 | + return $this; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + throw new PhpfastcacheInvalidArgumentException('Invalid driver instance'); |
|
| 59 | + } |
|
| 60 | 60 | } |
| 61 | 61 | \ No newline at end of file |
@@ -32,335 +32,335 @@ |
||
| 32 | 32 | */ |
| 33 | 33 | class Driver implements ExtendedCacheItemPoolInterface, AggregatablePoolInterface |
| 34 | 34 | { |
| 35 | - use DriverBaseTrait, IOHelperTrait; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * |
|
| 39 | - */ |
|
| 40 | - protected const INDEXING_FILE = 'indexing'; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * @var int |
|
| 44 | - */ |
|
| 45 | - protected $maxSize = 10; // 10 mb |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * @var int |
|
| 49 | - */ |
|
| 50 | - protected $currentDB = 1; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @var string |
|
| 54 | - */ |
|
| 55 | - protected $SqliteDir = ''; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @var PDO |
|
| 59 | - */ |
|
| 60 | - protected $indexing; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * @return bool |
|
| 64 | - */ |
|
| 65 | - public function driverCheck(): bool |
|
| 66 | - { |
|
| 67 | - return extension_loaded('pdo_sqlite') && (is_writable($this->getSqliteDir()) || @mkdir($this->getSqliteDir(), $this->getDefaultChmod(), true)); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * @return string |
|
| 72 | - * @throws PhpfastcacheCoreException |
|
| 73 | - */ |
|
| 74 | - public function getSqliteDir(): string |
|
| 75 | - { |
|
| 76 | - return $this->SqliteDir ?: $this->getPath(); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * @return array |
|
| 81 | - */ |
|
| 82 | - public function __sleep(): array |
|
| 83 | - { |
|
| 84 | - return array_diff(array_keys(get_object_vars($this)), ['indexing', 'instance']); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * @return bool |
|
| 89 | - * @throws PhpfastcacheIOException |
|
| 90 | - */ |
|
| 91 | - protected function driverConnect(): bool |
|
| 92 | - { |
|
| 93 | - if (!file_exists($this->getSqliteDir()) && !@mkdir($this->getSqliteDir(), $this->getDefaultChmod(), true)) { |
|
| 94 | - throw new PhpfastcacheIOException(sprintf('Sqlite cannot write in "%s", aborting...', $this->getPath())); |
|
| 95 | - } |
|
| 96 | - if (!file_exists($this->getPath())) { |
|
| 97 | - if (!mkdir($this->getPath(), $this->getDefaultChmod(), true) |
|
| 98 | - ) { |
|
| 99 | - $this->fallback = true; |
|
| 100 | - } |
|
| 101 | - } |
|
| 102 | - $this->SqliteDir = $this->getPath(); |
|
| 103 | - |
|
| 104 | - return true; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * @param CacheItemInterface $item |
|
| 109 | - * @return null|array |
|
| 110 | - */ |
|
| 111 | - protected function driverRead(CacheItemInterface $item) |
|
| 112 | - { |
|
| 113 | - try { |
|
| 114 | - $stm = $this->getDb($item->getEncodedKey()) |
|
| 115 | - ->prepare("SELECT * FROM `caching` WHERE `keyword`=:keyword LIMIT 1"); |
|
| 116 | - $stm->execute( |
|
| 117 | - [ |
|
| 118 | - ':keyword' => $item->getEncodedKey(), |
|
| 119 | - ] |
|
| 120 | - ); |
|
| 121 | - $row = $stm->fetch(PDO::FETCH_ASSOC); |
|
| 122 | - } catch (PDOException $e) { |
|
| 123 | - try { |
|
| 124 | - $stm = $this->getDb($item->getEncodedKey(), true) |
|
| 125 | - ->prepare("SELECT * FROM `caching` WHERE `keyword`=:keyword LIMIT 1"); |
|
| 126 | - $stm->execute( |
|
| 127 | - [ |
|
| 128 | - ':keyword' => $item->getEncodedKey(), |
|
| 129 | - ] |
|
| 130 | - ); |
|
| 131 | - $row = $stm->fetch(PDO::FETCH_ASSOC); |
|
| 132 | - } catch (PDOException $e) { |
|
| 133 | - return null; |
|
| 134 | - } |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - if (isset($row['object'])) { |
|
| 138 | - return $this->decode($row['object']); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - return null; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * @param string $keyword |
|
| 146 | - * @param bool $reset |
|
| 147 | - * @return PDO |
|
| 148 | - */ |
|
| 149 | - public function getDb(string $keyword, bool $reset = false): PDO |
|
| 150 | - { |
|
| 151 | - /** |
|
| 152 | - * Default is phpfastcache |
|
| 153 | - */ |
|
| 154 | - $instant = $this->indexing($keyword); |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * init instant |
|
| 158 | - */ |
|
| 159 | - if (!isset($this->instance[$instant])) { |
|
| 160 | - // check DB Files ready or not |
|
| 161 | - $tableCreated = false; |
|
| 162 | - if ($reset || !file_exists($this->SqliteDir . '/db' . $instant)) { |
|
| 163 | - $tableCreated = true; |
|
| 164 | - } |
|
| 165 | - $PDO = new PDO('sqlite:' . $this->SqliteDir . '/db' . $instant); |
|
| 166 | - $PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
| 167 | - |
|
| 168 | - if ($tableCreated) { |
|
| 169 | - $this->initDB($PDO); |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - $this->instance[$instant] = $PDO; |
|
| 173 | - unset($PDO); |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - return $this->instance[$instant]; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * INIT Instant DB |
|
| 181 | - * Return Database of Keyword |
|
| 182 | - * @param $keyword |
|
| 183 | - * @return int |
|
| 184 | - */ |
|
| 185 | - public function indexing($keyword) |
|
| 186 | - { |
|
| 187 | - if ($this->indexing == null) { |
|
| 188 | - $tableCreated = false; |
|
| 189 | - if (!file_exists($this->SqliteDir . '/indexing')) { |
|
| 190 | - $tableCreated = true; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - $PDO = new PDO("sqlite:" . $this->SqliteDir . '/' . self::INDEXING_FILE); |
|
| 194 | - $PDO->setAttribute( |
|
| 195 | - PDO::ATTR_ERRMODE, |
|
| 196 | - PDO::ERRMODE_EXCEPTION |
|
| 197 | - ); |
|
| 198 | - |
|
| 199 | - if ($tableCreated) { |
|
| 200 | - $this->initIndexing($PDO); |
|
| 201 | - } |
|
| 202 | - $this->indexing = $PDO; |
|
| 203 | - unset($PDO); |
|
| 204 | - |
|
| 205 | - $stm = $this->indexing->prepare("SELECT MAX(`db`) as `db` FROM `balancing`"); |
|
| 206 | - $stm->execute(); |
|
| 207 | - $row = $stm->fetch(PDO::FETCH_ASSOC); |
|
| 208 | - if (!isset($row['db'])) { |
|
| 209 | - $db = 1; |
|
| 210 | - } elseif ($row['db'] <= 1) { |
|
| 211 | - $db = 1; |
|
| 212 | - } else { |
|
| 213 | - $db = $row['db']; |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - // check file size |
|
| 217 | - |
|
| 218 | - $size = file_exists($this->SqliteDir . '/db' . $db) ? filesize($this->SqliteDir . '/db' . $db) : 1; |
|
| 219 | - $size = round($size / 1024 / 1024, 1); |
|
| 220 | - |
|
| 221 | - |
|
| 222 | - if ($size > $this->maxSize) { |
|
| 223 | - $db++; |
|
| 224 | - } |
|
| 225 | - $this->currentDB = $db; |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - // look for keyword |
|
| 229 | - $stm = $this->indexing->prepare("SELECT * FROM `balancing` WHERE `keyword`=:keyword LIMIT 1"); |
|
| 230 | - $stm->execute( |
|
| 231 | - [ |
|
| 232 | - ':keyword' => $keyword, |
|
| 233 | - ] |
|
| 234 | - ); |
|
| 235 | - $row = $stm->fetch(PDO::FETCH_ASSOC); |
|
| 236 | - if (isset($row['db']) && $row['db'] != '') { |
|
| 237 | - $db = $row['db']; |
|
| 238 | - } else { |
|
| 239 | - /* |
|
| 35 | + use DriverBaseTrait, IOHelperTrait; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * |
|
| 39 | + */ |
|
| 40 | + protected const INDEXING_FILE = 'indexing'; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * @var int |
|
| 44 | + */ |
|
| 45 | + protected $maxSize = 10; // 10 mb |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * @var int |
|
| 49 | + */ |
|
| 50 | + protected $currentDB = 1; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @var string |
|
| 54 | + */ |
|
| 55 | + protected $SqliteDir = ''; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @var PDO |
|
| 59 | + */ |
|
| 60 | + protected $indexing; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * @return bool |
|
| 64 | + */ |
|
| 65 | + public function driverCheck(): bool |
|
| 66 | + { |
|
| 67 | + return extension_loaded('pdo_sqlite') && (is_writable($this->getSqliteDir()) || @mkdir($this->getSqliteDir(), $this->getDefaultChmod(), true)); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * @return string |
|
| 72 | + * @throws PhpfastcacheCoreException |
|
| 73 | + */ |
|
| 74 | + public function getSqliteDir(): string |
|
| 75 | + { |
|
| 76 | + return $this->SqliteDir ?: $this->getPath(); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * @return array |
|
| 81 | + */ |
|
| 82 | + public function __sleep(): array |
|
| 83 | + { |
|
| 84 | + return array_diff(array_keys(get_object_vars($this)), ['indexing', 'instance']); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * @return bool |
|
| 89 | + * @throws PhpfastcacheIOException |
|
| 90 | + */ |
|
| 91 | + protected function driverConnect(): bool |
|
| 92 | + { |
|
| 93 | + if (!file_exists($this->getSqliteDir()) && !@mkdir($this->getSqliteDir(), $this->getDefaultChmod(), true)) { |
|
| 94 | + throw new PhpfastcacheIOException(sprintf('Sqlite cannot write in "%s", aborting...', $this->getPath())); |
|
| 95 | + } |
|
| 96 | + if (!file_exists($this->getPath())) { |
|
| 97 | + if (!mkdir($this->getPath(), $this->getDefaultChmod(), true) |
|
| 98 | + ) { |
|
| 99 | + $this->fallback = true; |
|
| 100 | + } |
|
| 101 | + } |
|
| 102 | + $this->SqliteDir = $this->getPath(); |
|
| 103 | + |
|
| 104 | + return true; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * @param CacheItemInterface $item |
|
| 109 | + * @return null|array |
|
| 110 | + */ |
|
| 111 | + protected function driverRead(CacheItemInterface $item) |
|
| 112 | + { |
|
| 113 | + try { |
|
| 114 | + $stm = $this->getDb($item->getEncodedKey()) |
|
| 115 | + ->prepare("SELECT * FROM `caching` WHERE `keyword`=:keyword LIMIT 1"); |
|
| 116 | + $stm->execute( |
|
| 117 | + [ |
|
| 118 | + ':keyword' => $item->getEncodedKey(), |
|
| 119 | + ] |
|
| 120 | + ); |
|
| 121 | + $row = $stm->fetch(PDO::FETCH_ASSOC); |
|
| 122 | + } catch (PDOException $e) { |
|
| 123 | + try { |
|
| 124 | + $stm = $this->getDb($item->getEncodedKey(), true) |
|
| 125 | + ->prepare("SELECT * FROM `caching` WHERE `keyword`=:keyword LIMIT 1"); |
|
| 126 | + $stm->execute( |
|
| 127 | + [ |
|
| 128 | + ':keyword' => $item->getEncodedKey(), |
|
| 129 | + ] |
|
| 130 | + ); |
|
| 131 | + $row = $stm->fetch(PDO::FETCH_ASSOC); |
|
| 132 | + } catch (PDOException $e) { |
|
| 133 | + return null; |
|
| 134 | + } |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + if (isset($row['object'])) { |
|
| 138 | + return $this->decode($row['object']); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + return null; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * @param string $keyword |
|
| 146 | + * @param bool $reset |
|
| 147 | + * @return PDO |
|
| 148 | + */ |
|
| 149 | + public function getDb(string $keyword, bool $reset = false): PDO |
|
| 150 | + { |
|
| 151 | + /** |
|
| 152 | + * Default is phpfastcache |
|
| 153 | + */ |
|
| 154 | + $instant = $this->indexing($keyword); |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * init instant |
|
| 158 | + */ |
|
| 159 | + if (!isset($this->instance[$instant])) { |
|
| 160 | + // check DB Files ready or not |
|
| 161 | + $tableCreated = false; |
|
| 162 | + if ($reset || !file_exists($this->SqliteDir . '/db' . $instant)) { |
|
| 163 | + $tableCreated = true; |
|
| 164 | + } |
|
| 165 | + $PDO = new PDO('sqlite:' . $this->SqliteDir . '/db' . $instant); |
|
| 166 | + $PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
| 167 | + |
|
| 168 | + if ($tableCreated) { |
|
| 169 | + $this->initDB($PDO); |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + $this->instance[$instant] = $PDO; |
|
| 173 | + unset($PDO); |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + return $this->instance[$instant]; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * INIT Instant DB |
|
| 181 | + * Return Database of Keyword |
|
| 182 | + * @param $keyword |
|
| 183 | + * @return int |
|
| 184 | + */ |
|
| 185 | + public function indexing($keyword) |
|
| 186 | + { |
|
| 187 | + if ($this->indexing == null) { |
|
| 188 | + $tableCreated = false; |
|
| 189 | + if (!file_exists($this->SqliteDir . '/indexing')) { |
|
| 190 | + $tableCreated = true; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + $PDO = new PDO("sqlite:" . $this->SqliteDir . '/' . self::INDEXING_FILE); |
|
| 194 | + $PDO->setAttribute( |
|
| 195 | + PDO::ATTR_ERRMODE, |
|
| 196 | + PDO::ERRMODE_EXCEPTION |
|
| 197 | + ); |
|
| 198 | + |
|
| 199 | + if ($tableCreated) { |
|
| 200 | + $this->initIndexing($PDO); |
|
| 201 | + } |
|
| 202 | + $this->indexing = $PDO; |
|
| 203 | + unset($PDO); |
|
| 204 | + |
|
| 205 | + $stm = $this->indexing->prepare("SELECT MAX(`db`) as `db` FROM `balancing`"); |
|
| 206 | + $stm->execute(); |
|
| 207 | + $row = $stm->fetch(PDO::FETCH_ASSOC); |
|
| 208 | + if (!isset($row['db'])) { |
|
| 209 | + $db = 1; |
|
| 210 | + } elseif ($row['db'] <= 1) { |
|
| 211 | + $db = 1; |
|
| 212 | + } else { |
|
| 213 | + $db = $row['db']; |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + // check file size |
|
| 217 | + |
|
| 218 | + $size = file_exists($this->SqliteDir . '/db' . $db) ? filesize($this->SqliteDir . '/db' . $db) : 1; |
|
| 219 | + $size = round($size / 1024 / 1024, 1); |
|
| 220 | + |
|
| 221 | + |
|
| 222 | + if ($size > $this->maxSize) { |
|
| 223 | + $db++; |
|
| 224 | + } |
|
| 225 | + $this->currentDB = $db; |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + // look for keyword |
|
| 229 | + $stm = $this->indexing->prepare("SELECT * FROM `balancing` WHERE `keyword`=:keyword LIMIT 1"); |
|
| 230 | + $stm->execute( |
|
| 231 | + [ |
|
| 232 | + ':keyword' => $keyword, |
|
| 233 | + ] |
|
| 234 | + ); |
|
| 235 | + $row = $stm->fetch(PDO::FETCH_ASSOC); |
|
| 236 | + if (isset($row['db']) && $row['db'] != '') { |
|
| 237 | + $db = $row['db']; |
|
| 238 | + } else { |
|
| 239 | + /* |
|
| 240 | 240 | * Insert new to Indexing |
| 241 | 241 | */ |
| 242 | - $db = $this->currentDB; |
|
| 243 | - $stm = $this->indexing->prepare("INSERT INTO `balancing` (`keyword`,`db`) VALUES(:keyword, :db)"); |
|
| 244 | - $stm->execute( |
|
| 245 | - [ |
|
| 246 | - ':keyword' => $keyword, |
|
| 247 | - ':db' => $db, |
|
| 248 | - ] |
|
| 249 | - ); |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - return $db; |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * INIT Indexing DB |
|
| 257 | - * @param PDO $db |
|
| 258 | - */ |
|
| 259 | - public function initIndexing(PDO $db) |
|
| 260 | - { |
|
| 261 | - // delete everything before reset indexing |
|
| 262 | - $dir = opendir($this->SqliteDir); |
|
| 263 | - while ($file = readdir($dir)) { |
|
| 264 | - if ($file != '.' && $file != '..' && $file != 'indexing' && $file != 'dbfastcache') { |
|
| 265 | - unlink($this->SqliteDir . '/' . $file); |
|
| 266 | - } |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - $db->exec('DROP TABLE if exists "balancing"'); |
|
| 270 | - $db->exec('CREATE TABLE "balancing" ("keyword" VARCHAR PRIMARY KEY NOT NULL UNIQUE, "db" INTEGER)'); |
|
| 271 | - $db->exec('CREATE INDEX "db" ON "balancing" ("db")'); |
|
| 272 | - $db->exec('CREATE UNIQUE INDEX "lookup" ON "balancing" ("keyword")'); |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * INIT NEW DB |
|
| 277 | - * @param PDO $db |
|
| 278 | - */ |
|
| 279 | - public function initDB(PDO $db) |
|
| 280 | - { |
|
| 281 | - $db->exec('drop table if exists "caching"'); |
|
| 282 | - $db->exec('CREATE TABLE "caching" ("id" INTEGER PRIMARY KEY AUTOINCREMENT, "keyword" VARCHAR UNIQUE, "object" BLOB, "exp" INTEGER)'); |
|
| 283 | - $db->exec('CREATE UNIQUE INDEX "cleanup" ON "caching" ("keyword","exp")'); |
|
| 284 | - $db->exec('CREATE INDEX "exp" ON "caching" ("exp")'); |
|
| 285 | - $db->exec('CREATE UNIQUE INDEX "keyword" ON "caching" ("keyword")'); |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * @param CacheItemInterface $item |
|
| 290 | - * @return mixed |
|
| 291 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 292 | - */ |
|
| 293 | - protected function driverWrite(CacheItemInterface $item): bool |
|
| 294 | - { |
|
| 295 | - /** |
|
| 296 | - * Check for Cross-Driver type confusion |
|
| 297 | - */ |
|
| 298 | - if ($item instanceof Item) { |
|
| 299 | - try { |
|
| 300 | - $stm = $this->getDb($item->getEncodedKey()) |
|
| 301 | - ->prepare("INSERT OR REPLACE INTO `caching` (`keyword`,`object`,`exp`) values(:keyword,:object,:exp)"); |
|
| 302 | - $stm->execute( |
|
| 303 | - [ |
|
| 304 | - ':keyword' => $item->getEncodedKey(), |
|
| 305 | - ':object' => $this->encode($this->driverPreWrap($item)), |
|
| 306 | - ':exp' => $item->getExpirationDate()->getTimestamp(), |
|
| 307 | - ] |
|
| 308 | - ); |
|
| 309 | - |
|
| 310 | - return true; |
|
| 311 | - } catch (PDOException $e) { |
|
| 312 | - return false; |
|
| 313 | - } |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - /** |
|
| 320 | - * @param CacheItemInterface $item |
|
| 321 | - * @return bool |
|
| 322 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 323 | - */ |
|
| 324 | - protected function driverDelete(CacheItemInterface $item): bool |
|
| 325 | - { |
|
| 326 | - /** |
|
| 327 | - * Check for Cross-Driver type confusion |
|
| 328 | - */ |
|
| 329 | - if ($item instanceof Item) { |
|
| 330 | - try { |
|
| 331 | - $stm = $this->getDb($item->getEncodedKey()) |
|
| 332 | - ->prepare("DELETE FROM `caching` WHERE (`exp` <= :U) OR (`keyword`=:keyword) "); |
|
| 333 | - |
|
| 334 | - return $stm->execute( |
|
| 335 | - [ |
|
| 336 | - ':keyword' => $item->getEncodedKey(), |
|
| 337 | - ':U' => time(), |
|
| 338 | - ] |
|
| 339 | - ); |
|
| 340 | - } catch (PDOException $e) { |
|
| 341 | - return false; |
|
| 342 | - } |
|
| 343 | - } else { |
|
| 344 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 345 | - } |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - /** |
|
| 349 | - * @return bool |
|
| 350 | - */ |
|
| 351 | - protected function driverClear(): bool |
|
| 352 | - { |
|
| 353 | - $this->instance = []; |
|
| 354 | - $this->indexing = null; |
|
| 355 | - |
|
| 356 | - // delete everything before reset indexing |
|
| 357 | - $dir = opendir($this->getSqliteDir()); |
|
| 358 | - while ($file = readdir($dir)) { |
|
| 359 | - if ($file != '.' && $file != '..') { |
|
| 360 | - unlink($this->getSqliteDir() . '/' . $file); |
|
| 361 | - } |
|
| 362 | - } |
|
| 363 | - |
|
| 364 | - return true; |
|
| 365 | - } |
|
| 242 | + $db = $this->currentDB; |
|
| 243 | + $stm = $this->indexing->prepare("INSERT INTO `balancing` (`keyword`,`db`) VALUES(:keyword, :db)"); |
|
| 244 | + $stm->execute( |
|
| 245 | + [ |
|
| 246 | + ':keyword' => $keyword, |
|
| 247 | + ':db' => $db, |
|
| 248 | + ] |
|
| 249 | + ); |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + return $db; |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * INIT Indexing DB |
|
| 257 | + * @param PDO $db |
|
| 258 | + */ |
|
| 259 | + public function initIndexing(PDO $db) |
|
| 260 | + { |
|
| 261 | + // delete everything before reset indexing |
|
| 262 | + $dir = opendir($this->SqliteDir); |
|
| 263 | + while ($file = readdir($dir)) { |
|
| 264 | + if ($file != '.' && $file != '..' && $file != 'indexing' && $file != 'dbfastcache') { |
|
| 265 | + unlink($this->SqliteDir . '/' . $file); |
|
| 266 | + } |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + $db->exec('DROP TABLE if exists "balancing"'); |
|
| 270 | + $db->exec('CREATE TABLE "balancing" ("keyword" VARCHAR PRIMARY KEY NOT NULL UNIQUE, "db" INTEGER)'); |
|
| 271 | + $db->exec('CREATE INDEX "db" ON "balancing" ("db")'); |
|
| 272 | + $db->exec('CREATE UNIQUE INDEX "lookup" ON "balancing" ("keyword")'); |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * INIT NEW DB |
|
| 277 | + * @param PDO $db |
|
| 278 | + */ |
|
| 279 | + public function initDB(PDO $db) |
|
| 280 | + { |
|
| 281 | + $db->exec('drop table if exists "caching"'); |
|
| 282 | + $db->exec('CREATE TABLE "caching" ("id" INTEGER PRIMARY KEY AUTOINCREMENT, "keyword" VARCHAR UNIQUE, "object" BLOB, "exp" INTEGER)'); |
|
| 283 | + $db->exec('CREATE UNIQUE INDEX "cleanup" ON "caching" ("keyword","exp")'); |
|
| 284 | + $db->exec('CREATE INDEX "exp" ON "caching" ("exp")'); |
|
| 285 | + $db->exec('CREATE UNIQUE INDEX "keyword" ON "caching" ("keyword")'); |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * @param CacheItemInterface $item |
|
| 290 | + * @return mixed |
|
| 291 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 292 | + */ |
|
| 293 | + protected function driverWrite(CacheItemInterface $item): bool |
|
| 294 | + { |
|
| 295 | + /** |
|
| 296 | + * Check for Cross-Driver type confusion |
|
| 297 | + */ |
|
| 298 | + if ($item instanceof Item) { |
|
| 299 | + try { |
|
| 300 | + $stm = $this->getDb($item->getEncodedKey()) |
|
| 301 | + ->prepare("INSERT OR REPLACE INTO `caching` (`keyword`,`object`,`exp`) values(:keyword,:object,:exp)"); |
|
| 302 | + $stm->execute( |
|
| 303 | + [ |
|
| 304 | + ':keyword' => $item->getEncodedKey(), |
|
| 305 | + ':object' => $this->encode($this->driverPreWrap($item)), |
|
| 306 | + ':exp' => $item->getExpirationDate()->getTimestamp(), |
|
| 307 | + ] |
|
| 308 | + ); |
|
| 309 | + |
|
| 310 | + return true; |
|
| 311 | + } catch (PDOException $e) { |
|
| 312 | + return false; |
|
| 313 | + } |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + /** |
|
| 320 | + * @param CacheItemInterface $item |
|
| 321 | + * @return bool |
|
| 322 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 323 | + */ |
|
| 324 | + protected function driverDelete(CacheItemInterface $item): bool |
|
| 325 | + { |
|
| 326 | + /** |
|
| 327 | + * Check for Cross-Driver type confusion |
|
| 328 | + */ |
|
| 329 | + if ($item instanceof Item) { |
|
| 330 | + try { |
|
| 331 | + $stm = $this->getDb($item->getEncodedKey()) |
|
| 332 | + ->prepare("DELETE FROM `caching` WHERE (`exp` <= :U) OR (`keyword`=:keyword) "); |
|
| 333 | + |
|
| 334 | + return $stm->execute( |
|
| 335 | + [ |
|
| 336 | + ':keyword' => $item->getEncodedKey(), |
|
| 337 | + ':U' => time(), |
|
| 338 | + ] |
|
| 339 | + ); |
|
| 340 | + } catch (PDOException $e) { |
|
| 341 | + return false; |
|
| 342 | + } |
|
| 343 | + } else { |
|
| 344 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 345 | + } |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + /** |
|
| 349 | + * @return bool |
|
| 350 | + */ |
|
| 351 | + protected function driverClear(): bool |
|
| 352 | + { |
|
| 353 | + $this->instance = []; |
|
| 354 | + $this->indexing = null; |
|
| 355 | + |
|
| 356 | + // delete everything before reset indexing |
|
| 357 | + $dir = opendir($this->getSqliteDir()); |
|
| 358 | + while ($file = readdir($dir)) { |
|
| 359 | + if ($file != '.' && $file != '..') { |
|
| 360 | + unlink($this->getSqliteDir() . '/' . $file); |
|
| 361 | + } |
|
| 362 | + } |
|
| 363 | + |
|
| 364 | + return true; |
|
| 365 | + } |
|
| 366 | 366 | } |
@@ -27,34 +27,34 @@ |
||
| 27 | 27 | */ |
| 28 | 28 | class Item implements ExtendedCacheItemInterface |
| 29 | 29 | { |
| 30 | - use ItemBaseTrait { |
|
| 31 | - ItemBaseTrait::__construct as __BaseConstruct; |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Item constructor. |
|
| 36 | - * @param Driver $driver |
|
| 37 | - * @param $key |
|
| 38 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 39 | - */ |
|
| 40 | - public function __construct(WincacheDriver $driver, $key) |
|
| 41 | - { |
|
| 42 | - $this->__BaseConstruct($driver, $key); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @param ExtendedCacheItemPoolInterface $driver |
|
| 47 | - * @return static |
|
| 48 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 49 | - */ |
|
| 50 | - public function setDriver(ExtendedCacheItemPoolInterface $driver) |
|
| 51 | - { |
|
| 52 | - if ($driver instanceof WincacheDriver) { |
|
| 53 | - $this->driver = $driver; |
|
| 54 | - |
|
| 55 | - return $this; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - throw new PhpfastcacheInvalidArgumentException('Invalid driver instance'); |
|
| 59 | - } |
|
| 30 | + use ItemBaseTrait { |
|
| 31 | + ItemBaseTrait::__construct as __BaseConstruct; |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Item constructor. |
|
| 36 | + * @param Driver $driver |
|
| 37 | + * @param $key |
|
| 38 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 39 | + */ |
|
| 40 | + public function __construct(WincacheDriver $driver, $key) |
|
| 41 | + { |
|
| 42 | + $this->__BaseConstruct($driver, $key); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @param ExtendedCacheItemPoolInterface $driver |
|
| 47 | + * @return static |
|
| 48 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 49 | + */ |
|
| 50 | + public function setDriver(ExtendedCacheItemPoolInterface $driver) |
|
| 51 | + { |
|
| 52 | + if ($driver instanceof WincacheDriver) { |
|
| 53 | + $this->driver = $driver; |
|
| 54 | + |
|
| 55 | + return $this; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + throw new PhpfastcacheInvalidArgumentException('Invalid driver instance'); |
|
| 59 | + } |
|
| 60 | 60 | } |
| 61 | 61 | \ No newline at end of file |