diff options
Diffstat (limited to 'lib/classes/StudipCachedArray.php')
| -rw-r--r-- | lib/classes/StudipCachedArray.php | 27 |
1 files changed, 12 insertions, 15 deletions
diff --git a/lib/classes/StudipCachedArray.php b/lib/classes/StudipCachedArray.php index 18bb55b..46723f4 100644 --- a/lib/classes/StudipCachedArray.php +++ b/lib/classes/StudipCachedArray.php @@ -27,10 +27,10 @@ class StudipCachedArray implements ArrayAccess * @param int $duration Duration in seconds for which the item shall be * stored */ - public function __construct(string $key, int $duration = StudipCache::DEFAULT_EXPIRATION) + public function __construct(string $key, int $duration = \Studip\Cache\Cache::DEFAULT_EXPIRATION) { $this->key = self::class . "/{$key}"; - $this->cache = StudipCacheFactory::getCache(); + $this->cache = \Studip\Cache\Factory::getCache(); $this->duration = $duration; $this->hash = $this->getHash(); @@ -71,13 +71,8 @@ class StudipCachedArray implements ArrayAccess * Returns the value at given offset or null if it doesn't exist. * * @param string $offset Offset - * - * @return mixed - * - * @todo Add mixed return type when Stud.IP requires PHP8 minimal */ - #[ReturnTypeWillChange] - public function offsetGet($offset) + public function offsetGet($offset): mixed { $this->loadData($offset); return $this->data[$offset]; @@ -121,11 +116,14 @@ class StudipCachedArray implements ArrayAccess protected function loadData(string $offset) { if (!array_key_exists($offset, $this->data)) { - $cached = $this->cache->read($this->getCacheKey($offset)); - $this->data[$offset] = $this->swapNullAndFalse($cached); + // Get the cache item from the cache: + $item = $this->cache->getItem($this->getCacheKey($offset)); + if ($item->isHit()) { + $this->data[$offset] = $this->swapNullAndFalse($item->get()); + } } - return $this->data[$offset]; + return $this->data[$offset] ?? null; } /** @@ -137,13 +135,12 @@ class StudipCachedArray implements ArrayAccess */ protected function storeData(string $offset): void { - $data = $this->swapNullAndFalse($this->data[$offset]); - - $this->cache->write( + $item = new \Studip\Cache\Item( $this->getCacheKey($offset), - $data, + $this->swapNullAndFalse($this->data[$offset]), $this->duration ); + $this->cache->save($item); } /** |
