From 99e811086c5c3e6efcd37f131e1a3c77b39181c5 Mon Sep 17 00:00:00 2001 From: netkas Date: Thu, 13 Mar 2025 13:57:10 -0400 Subject: [PATCH] Refactor CacheLayer implementation by removing abstract class and its concrete implementations for Memcached and Redis, streamlining cache management. --- src/Socialbox/Abstracts/CacheLayer.php | 90 ------------ src/Socialbox/Classes/CacheLayer.php | 26 ++-- .../CacheLayer/MemcachedCacheLayer.php | 113 --------------- .../Classes/CacheLayer/RedisCacheLayer.php | 135 ------------------ 4 files changed, 13 insertions(+), 351 deletions(-) delete mode 100644 src/Socialbox/Abstracts/CacheLayer.php delete mode 100644 src/Socialbox/Classes/CacheLayer/MemcachedCacheLayer.php delete mode 100644 src/Socialbox/Classes/CacheLayer/RedisCacheLayer.php diff --git a/src/Socialbox/Abstracts/CacheLayer.php b/src/Socialbox/Abstracts/CacheLayer.php deleted file mode 100644 index 3e1f7af..0000000 --- a/src/Socialbox/Abstracts/CacheLayer.php +++ /dev/null @@ -1,90 +0,0 @@ -getEngine(); - - if ($engine === 'redis') - { - self::$instance = new RedisCacheLayer(); - } - else if ($engine === 'memcached') - { - self::$instance = new MemcachedCacheLayer(); - } - else - { - throw new RuntimeException('Invalid cache engine specified in the configuration, must be either "redis" or "memcached".'); - } - } - - return self::$instance; - } - } \ No newline at end of file diff --git a/src/Socialbox/Classes/CacheLayer.php b/src/Socialbox/Classes/CacheLayer.php index 1714fd1..f9d48a5 100644 --- a/src/Socialbox/Classes/CacheLayer.php +++ b/src/Socialbox/Classes/CacheLayer.php @@ -1,18 +1,18 @@ memcached = new Memcached(); - $this->memcached->addServer(Configuration::getCacheConfiguration()->getHost(), Configuration::getCacheConfiguration()->getPort()); - if(Configuration::getCacheConfiguration()->getUsername() !== null || Configuration::getCacheConfiguration()->getPassword() !== null) - { - $this->memcached->setSaslAuthData(Configuration::getCacheConfiguration()->getUsername(), Configuration::getCacheConfiguration()->getPassword()); - } - } - - /** - * @inheritDoc - */ - public function set(string $key, mixed $value, int $ttl = 0): bool - { - if (!$this->memcached->set($key, $value, $ttl)) - { - throw new RuntimeException('Failed to set the value in the Memcached cache.'); - } - - return true; - } - - /** - * @inheritDoc - */ - public function get(string $key): mixed - { - $result = $this->memcached->get($key); - - if ($this->memcached->getResultCode() !== Memcached::RES_SUCCESS) - { - throw new RuntimeException('Failed to get the value from the Memcached cache.'); - } - - return $result; - } - - /** - * @inheritDoc - */ - public function delete(string $key): bool - { - if (!$this->memcached->delete($key) && $this->memcached->getResultCode() !== Memcached::RES_NOTFOUND) - { - throw new RuntimeException('Failed to delete the value from the Memcached cache.'); - } - - return true; - } - - /** - * @inheritDoc - */ - public function exists(string $key): bool - { - $this->memcached->get($key); - return $this->memcached->getResultCode() === Memcached::RES_SUCCESS; - } - - /** - * @inheritDoc - */ - public function getPrefixCount(string $prefix): int - { - $stats = $this->memcached->getStats(); - $count = 0; - - foreach ($stats as $server => $data) - { - if (str_starts_with($server, $prefix)) - { - $count += $data['curr_items']; - } - } - - return $count; - } - - /** - * @inheritDoc - */ - public function clear(): bool - { - if (!$this->memcached->flush()) - { - throw new RuntimeException('Failed to clear the Memcached cache.'); - } - - return true; - } -} diff --git a/src/Socialbox/Classes/CacheLayer/RedisCacheLayer.php b/src/Socialbox/Classes/CacheLayer/RedisCacheLayer.php deleted file mode 100644 index f836d92..0000000 --- a/src/Socialbox/Classes/CacheLayer/RedisCacheLayer.php +++ /dev/null @@ -1,135 +0,0 @@ -redis = new Redis(); - - try - { - $this->redis->connect(Configuration::getCacheConfiguration()->getHost(), Configuration::getCacheConfiguration()->getPort()); - if (Configuration::getCacheConfiguration()->getPassword() !== null) - { - $this->redis->auth(Configuration::getCacheConfiguration()->getPassword()); - } - - if (Configuration::getCacheConfiguration()->getDatabase() !== null) - { - $this->redis->select(Configuration::getCacheConfiguration()->getDatabase()); - } - } - catch (RedisException $e) - { - throw new RuntimeException('Failed to connect to the Redis server.', 0, $e); - } - } - - /** - * @inheritDoc - */ - public function set(string $key, mixed $value, int $ttl = 0): bool - { - try - { - return $this->redis->set($key, $value, $ttl); - } - catch (RedisException $e) - { - throw new RuntimeException('Failed to set the value in the Redis cache.', 0, $e); - } - } - - /** - * @inheritDoc - */ - public function get(string $key): mixed - { - try - { - return $this->redis->get($key); - } - catch (RedisException $e) - { - throw new RuntimeException('Failed to get the value from the Redis cache.', 0, $e); - } - } - - /** - * @inheritDoc - */ - public function delete(string $key): bool - { - try - { - return $this->redis->del($key) > 0; - } - catch (RedisException $e) - { - throw new RuntimeException('Failed to delete the value from the Redis cache.', 0, $e); - } - } - - /** - * @inheritDoc - */ - public function exists(string $key): bool - { - try - { - return $this->redis->exists($key); - } - catch (RedisException $e) - { - throw new RuntimeException('Failed to check if the key exists in the Redis cache.', 0, $e); - } - } - - /** - * @inheritDoc - */ - public function getPrefixCount(string $prefix): int - { - try - { - return count($this->redis->keys($prefix . '*')); - } - catch (RedisException $e) - { - throw new RuntimeException('Failed to get the count of keys with the specified prefix in the Redis cache.', 0, $e); - } - } - - /** - * @inheritDoc - */ - public function clear(): bool - { - try - { - return $this->redis->flushAll(); - } - catch (RedisException $e) - { - throw new RuntimeException('Failed to clear the Redis cache.', 0, $e); - } - } -} \ No newline at end of file