aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/StudipCachedArray.php
diff options
context:
space:
mode:
authorPhilipp Schüttlöffel <schuettloeffel@zqs.uni-hannover.de>2024-09-24 10:53:31 +0200
committerPhilipp Schüttlöffel <schuettloeffel@zqs.uni-hannover.de>2024-09-24 10:53:31 +0200
commit4459dd7917f4d1c34f40bb68f0e991e9c3d53e4c (patch)
tree5c07151ae61276d334e88f6309c30d439a85c12e /lib/classes/StudipCachedArray.php
parentda0022e5c1abbf9825ae76debaabdff7e8623bb4 (diff)
parent97a188592c679890a25c37ab78463add76a52ff7 (diff)
Merge branch 'main' into issue-3911issue-3911
Diffstat (limited to 'lib/classes/StudipCachedArray.php')
-rw-r--r--lib/classes/StudipCachedArray.php27
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);
}
/**