aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/cache/Wrapper.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/cache/Wrapper.php
parentda0022e5c1abbf9825ae76debaabdff7e8623bb4 (diff)
parent97a188592c679890a25c37ab78463add76a52ff7 (diff)
Merge branch 'main' into issue-3911issue-3911
Diffstat (limited to 'lib/classes/cache/Wrapper.php')
-rw-r--r--lib/classes/cache/Wrapper.php95
1 files changed, 95 insertions, 0 deletions
diff --git a/lib/classes/cache/Wrapper.php b/lib/classes/cache/Wrapper.php
new file mode 100644
index 0000000..4e6342c
--- /dev/null
+++ b/lib/classes/cache/Wrapper.php
@@ -0,0 +1,95 @@
+<?php
+
+namespace Studip\Cache;
+
+use Psr\Cache\CacheItemInterface;
+
+/**
+ * The cache wrapper wraps a memory cache around another cache. This should
+ * reduce the accesses to the actual cache.
+ *
+ * @author Jan-Hendrik Willms <tleilax+studip@gmail.com>
+ * @license GPL2 or any later version
+ * @since Stud.IP 5.4
+ */
+class Wrapper extends Cache
+{
+ protected Cache $actual_cache;
+ protected MemoryCache $memory_cache;
+
+ public function __construct(Cache $actual_cache)
+ {
+ $this->actual_cache = $actual_cache;
+ $this->memory_cache = new MemoryCache();
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function expire($arg)
+ {
+ $this->memory_cache->expire($arg);
+ $this->actual_cache->expire($arg);
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function flush()
+ {
+ $this->memory_cache->flush();
+ $this->actual_cache->flush();
+ }
+
+ public static function getDisplayName(): string
+ {
+ return static::class;
+ }
+
+ public function getStats(): array
+ {
+ return $this->actual_cache->getStats();
+ }
+
+ public static function getConfig(): array
+ {
+ return [];
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public function getItem(string $key): CacheItemInterface
+ {
+ $cached = $this->memory_cache->getItem($key);
+ if ($cached->isHit()) {
+ return $cached;
+ }
+
+ $cached = $this->actual_cache->getItem($key);
+ if ($cached->isHit()) {
+ $this->memory_cache->save($cached);
+ }
+ return $cached;
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public function hasItem(string $key): bool
+ {
+ return $this->actual_cache->hasItem($key);
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public function save(CacheItemInterface $item): bool
+ {
+ if ($this->actual_cache->save($item)) {
+ return $this->memory_cache->save($item);
+ } else {
+ return false;
+ }
+ }
+}