From dee7ed34e9afeae25a810a4ced16c380adb7cdef Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Sun, 21 Apr 2024 02:16:21 +0900 Subject: [PATCH] Add helper classes that implement PSR-6 caching interface with Rhymix cache backend --- common/framework/helpers/CacheItemHelper.php | 111 ++++++++++++ .../framework/helpers/CacheItemPoolHelper.php | 160 ++++++++++++++++++ 2 files changed, 271 insertions(+) create mode 100644 common/framework/helpers/CacheItemHelper.php create mode 100644 common/framework/helpers/CacheItemPoolHelper.php diff --git a/common/framework/helpers/CacheItemHelper.php b/common/framework/helpers/CacheItemHelper.php new file mode 100644 index 000000000..7e6fc9399 --- /dev/null +++ b/common/framework/helpers/CacheItemHelper.php @@ -0,0 +1,111 @@ +key = $key; + } + + /** + * Returns the key for the current cache item. + * + * @return string + */ + public function getKey() + { + return $this->key; + } + + /** + * Retrieves the value of the item from the cache associated with this object's key. + * + * @return mixed + */ + public function get() + { + if ($this->value === null) + { + $this->value = \Rhymix\Framework\Cache::get($this->key); + } + return $this->value; + } + + /** + * Confirms if the cache item lookup resulted in a cache hit. + * + * @return bool + */ + public function isHit() + { + return \Rhymix\Framework\Cache::exists($this->key); + } + + /** + * Sets the value represented by this cache item. + * + * @param mixed $value + * @return static + */ + public function set($value) + { + $this->value = $value; + return $this; + } + + /** + * Sets the expiration time for this cache item. + * + * @param \DateTimeInterface|null $expiration + * @return static + */ + public function expiresAt($expiration) + { + $this->expires = $expiration->getTimestamp(); + return $this; + } + + /** + * Sets the expiration time for this cache item. + * + * @param int|\DateInterval|null $time + * @return static + */ + public function expiresAfter($time) + { + if ($time instanceof \DateInterval) + { + $date = new \DateTime(); + $date->add($time); + $this->expires = $date->getTimestamp(); + } + elseif (is_int($time)) + { + $this->expires = time() + $time; + } + else + { + $this->expires = null; + } + return $this; + } +} diff --git a/common/framework/helpers/CacheItemPoolHelper.php b/common/framework/helpers/CacheItemPoolHelper.php new file mode 100644 index 000000000..9bfbc4267 --- /dev/null +++ b/common/framework/helpers/CacheItemPoolHelper.php @@ -0,0 +1,160 @@ +force = $force; + } + + /** + * Returns a Cache Item representing the specified key. + * + * @param string $key + * @throws InvalidArgumentException + * @return CacheItemInterface + */ + public function getItem($key) + { + if (!is_scalar($key) || empty($key)) + { + throw new InvalidArgumentException; + } + return new CacheItemHelper((string)$key); + } + + /** + * Returns a traversable set of cache items. + * + * @param string[] $keys + * @throws InvalidArgumentException + * @return array + */ + public function getItems(array $keys = []) + { + $result = []; + foreach ($keys as $key) + { + if (!is_scalar($key) || empty($key)) + { + throw new InvalidArgumentException; + } + $result[(string)$key] = new CacheItemHelper((string)$key); + } + return $result; + } + + /** + * Confirms if the cache contains specified cache item. + * + * @param string $key + * @throws InvalidArgumentException + * @return bool + */ + public function hasItem($key) + { + return $this->getItem($key)->isHit(); + } + + /** + * Deletes all items in the pool. + * + * @return bool + */ + public function clear() + { + \Rhymix\Framework\Cache::clearAll(); + } + + /** + * Removes the item from the pool. + * + * @param string $key + * @throws InvalidArgumentException + * @return bool + */ + public function deleteItem($key) + { + if (!is_scalar($key) || empty($key)) + { + throw new InvalidArgumentException; + } + return \Rhymix\Framework\Cache::delete((string)$key); + } + + /** + * Removes multiple items from the pool. + * + * @param string[] $keys + * @throws InvalidArgumentException + * @return bool + */ + public function deleteItems(array $keys) + { + $result = true; + foreach ($keys as $key) + { + if (!is_scalar($key) || empty($key)) + { + throw new InvalidArgumentException; + } + if (!\Rhymix\Framework\Cache::delete((string)$key)) + { + $result = false; + } + } + return $result; + } + + /** + * Persists a cache item immediately. + * + * @param CacheItemInterface $item + * @return bool + */ + public function save(CacheItemInterface $item) + { + $ttl = $item->expires ? max(0, min(30 * 86400, $item->expires - time())) : 0; + return \Rhymix\Framework\Cache::set($item->key, $item->value, $ttl, $this->force); + } + + /** + * Sets a cache item to be persisted later. + * + * @param CacheItemInterface $item + * @return bool + */ + public function saveDeferred(CacheItemInterface $item) + { + return $this->save($item); + } + + /** + * Persists any deferred cache items. + * + * @return bool + */ + public function commit() + { + return true; + } +}