|
| 1 | +<? |
| 2 | + |
| 3 | +namespace App\GraphQL\Boilerplate; |
| 4 | + |
| 5 | +// use Symfony\Component\Cache\Adapter\FilesystemAdapter; // @see https://symfony.com/doc/current/components/cache/adapters/filesystem_adapter.html |
| 6 | +use Symfony\Component\Cache\Adapter\AbstractAdapter; |
| 7 | +use Symfony\Component\Cache\Adapter\RedisAdapter; // @see https://symfony.com/doc/current/components/cache/adapters/redis_adapter.html |
| 8 | + |
| 9 | +/** |
| 10 | + * PSR-6 Compatible Caching Utility |
| 11 | + * @see https://www.php-fig.org/psr/psr-6/ |
| 12 | + * @see https://symfony.com/doc/current/components/cache.html#cache-component-psr6-caching |
| 13 | + * |
| 14 | + * @usage |
| 15 | + * |
| 16 | + // With Symfony Contracts... |
| 17 | + use App\GraphQL\Boilerplate\CacheManager; |
| 18 | + $memoization = CacheManager::getInstance(); |
| 19 | + $value = $memoization->get('FOO', function (\Symfony\Contracts\Cache\ItemInterface $item) { |
| 20 | + $item->expiresAfter(10); //seconds |
| 21 | + return 'BAR'; |
| 22 | + }); |
| 23 | +
|
| 24 | + * @usage |
| 25 | + * |
| 26 | + // Without Contracts (pure PSR-6)... |
| 27 | + $memoization = CacheManager::getInstance(); |
| 28 | + $foo = $memoization->getItem('FOO'); |
| 29 | + if (!$foo->isHit()) { |
| 30 | + // foo item does not exist in the cache |
| 31 | + $foo->set('BAR')->expiresAfter(10); |
| 32 | + $memoization->save($foo); |
| 33 | + } |
| 34 | + // retrieve the value stored by the item |
| 35 | + $value = $foo->get(); |
| 36 | + * |
| 37 | + */ |
| 38 | +class CacheManager |
| 39 | +{ |
| 40 | + |
| 41 | + // protected $instanceId; |
| 42 | + protected static $instance; |
| 43 | + |
| 44 | + protected $redisClient = null; |
| 45 | + protected $redisAdapter = null; |
| 46 | + |
| 47 | + protected $cacheKeyPrefix = ''; |
| 48 | + |
| 49 | + public static function defaultOptions () { |
| 50 | + return [ |
| 51 | + // Enables or disables compression of items. This requires phpredis v4 or higher with LZF support enabled. |
| 52 | + 'compression' => true, |
| 53 | + |
| 54 | + // Enables or disables lazy connections to the backend. It’s false by default when using this as a stand-alone component and true by default when using it inside a Symfony application. |
| 55 | + 'lazy' => true, |
| 56 | + |
| 57 | + // Enables or disables use of persistent connections. A value of 0 disables persistent connections, and a value of 1 enables them. |
| 58 | + 'persistent' => 1, |
| 59 | + |
| 60 | + // Specifies the persistent id string to use for a persistent connection. |
| 61 | + 'persistent_id' => str_replace('\\', '_', __CLASS__), |
| 62 | + |
| 63 | + // Specifies the TCP-keepalive timeout (in seconds) of the connection. This requires phpredis v4 or higher and a TCP-keepalive enabled server. This option is useful in order to detect dead peers (clients that cannot be reached even if they look connected). |
| 64 | + 'tcp_keepalive' => 40, |
| 65 | + |
| 66 | + // Specifies the time (in seconds) used to connect to a Redis server before the connection attempt times out. |
| 67 | + 'timeout' => 20, |
| 68 | + |
| 69 | + // Specifies the time (in seconds) used when performing read operations on the underlying network resource before the operation times out. |
| 70 | + 'read_timeout' => 5, |
| 71 | + |
| 72 | + // Specifies the delay (in milliseconds) between reconnection attempts in case the client loses connection with the server. |
| 73 | + 'retry_interval' => 600, |
| 74 | + ]; |
| 75 | + } |
| 76 | + |
| 77 | + public function __construct($cacheKeyPrefix = null) |
| 78 | + { |
| 79 | + // $this->instanceId = uuid_create(UUID_TYPE_RANDOM); // @see https://symfony.com/blog/introducing-the-new-symfony-uuid-polyfill |
| 80 | + $this->setCacheKeyPrefix(str_replace('\\', '.', __CLASS__), false); |
| 81 | + |
| 82 | + if ($cacheKeyPrefix) { |
| 83 | + $this->setCacheKeyPrefix($cacheKeyPrefix, true); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + public static function getInstance () { |
| 88 | + if (!self::$instance) { |
| 89 | + self::$instance = new self(); |
| 90 | + } |
| 91 | + return self::$instance; |
| 92 | + } |
| 93 | + |
| 94 | + public function adapter () : AbstractAdapter |
| 95 | + { |
| 96 | + return $this->getRedisAdapter(); |
| 97 | + } |
| 98 | + |
| 99 | + public function __invoke($invoked) : AbstractAdapter |
| 100 | + { |
| 101 | + return $this->adapter(); |
| 102 | + } |
| 103 | + |
| 104 | + public function __call($name, $arguments = []) |
| 105 | + { |
| 106 | + if (!isset($arguments) || !is_array($arguments)) { |
| 107 | + $arguments = []; |
| 108 | + } |
| 109 | + return call_user_func_array([$this->adapter(), $name], $arguments); |
| 110 | + } |
| 111 | + |
| 112 | + public function setCacheKeyPrefix (string $prefix, bool $isAppendMode = true) |
| 113 | + { |
| 114 | + if ($isAppendMode) { |
| 115 | + $this->cacheKeyPrefix .= '--'.$prefix; |
| 116 | + } |
| 117 | + else { |
| 118 | + $this->cacheKeyPrefix = $prefix; |
| 119 | + } |
| 120 | + return $this; |
| 121 | + } |
| 122 | + |
| 123 | + public function setRedisAdapter (RedisAdapter $adapter) |
| 124 | + { |
| 125 | + if ($this->redisAdapter !== null) { |
| 126 | + throw new \Exception('Cannot set redis adapter when already initialized.'); |
| 127 | + } |
| 128 | + $this->redisAdapter = $adapter; |
| 129 | + return $this; |
| 130 | + } |
| 131 | + |
| 132 | + public function setRedisClient (\Symfony\Component\Cache\Traits\RedisProxy $client) |
| 133 | + { |
| 134 | + if ($this->redisClient !== null || $this->redisAdapter !== null) { |
| 135 | + throw new \Exception('Cannot set redis client when already initialized.'); |
| 136 | + } |
| 137 | + $this->redisClient = $client; |
| 138 | + return $this; |
| 139 | + } |
| 140 | + |
| 141 | + protected function getRedisClient ($dsn = null, $options = null) : \Symfony\Component\Cache\Traits\RedisProxy |
| 142 | + { |
| 143 | + // pass a single DSN string to register a single server with the client |
| 144 | + if (!isset($dsn)) { |
| 145 | + $dsn = 'redis://gql_slim_redis:6379'; |
| 146 | + } |
| 147 | + if ($this->redisClient === null) { |
| 148 | + $this->setRedisClient( |
| 149 | + RedisAdapter::createConnection( |
| 150 | + $dsn, |
| 151 | + is_array($options) ? array_merge(self::defaultOptions(), $options) : self::defaultOptions() |
| 152 | + ) |
| 153 | + ); |
| 154 | + } |
| 155 | + return $this->redisClient; |
| 156 | + } |
| 157 | + |
| 158 | + protected function getRedisAdapter () : RedisAdapter |
| 159 | + { |
| 160 | + // pass a single DSN string to register a single server with the client |
| 161 | + if ($this->redisAdapter === null) { |
| 162 | + $this->redisAdapter = new RedisAdapter( |
| 163 | + $this->getRedisClient(), |
| 164 | + // Namespace: the string prefixed to the keys of the items stored in this cache |
| 165 | + $this->cacheKeyPrefix, |
| 166 | + // TTL: the default lifetime (in seconds) for cache items that do not define their |
| 167 | + // own lifetime, with a value 0 causing items to be stored indefinitely (i.e. |
| 168 | + // until RedisAdapter::clear() is invoked or the server(s) are purged) |
| 169 | + 3 * 60 * 60 // 3h |
| 170 | + ); |
| 171 | + } |
| 172 | + return $this->redisAdapter; |
| 173 | + } |
| 174 | + |
| 175 | +} |
0 commit comments