aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/cache/DbCache.php
diff options
context:
space:
mode:
authorMarcus Eibrink-Lunzenauer <lunzenauer@elan-ev.de>2024-06-18 13:18:06 +0000
committerMarcus Eibrink-Lunzenauer <lunzenauer@elan-ev.de>2024-06-18 13:18:06 +0000
commit33fd1358507b4a5abb3dcebe78d407d0567717c1 (patch)
tree6bd8f6959da4c3fc1b8907c0bbc28eb9e10d4a5a /lib/classes/cache/DbCache.php
parent42d46671c0309bddb71a91bbfdc5f2fa2e44384e (diff)
Deprecate `StudipAutoloader` and use composer's `autoload`
Closes #4282 Merge request studip/studip!3099
Diffstat (limited to 'lib/classes/cache/DbCache.php')
-rw-r--r--lib/classes/cache/DbCache.php143
1 files changed, 143 insertions, 0 deletions
diff --git a/lib/classes/cache/DbCache.php b/lib/classes/cache/DbCache.php
new file mode 100644
index 0000000..c7abef2
--- /dev/null
+++ b/lib/classes/cache/DbCache.php
@@ -0,0 +1,143 @@
+<?php
+
+namespace Studip\Cache;
+
+use DBManager;
+use Psr\Cache\CacheItemInterface;
+
+/**
+ * StudipCache implementation using database table
+ *
+ * @author Elmar Ludwig <elmar.ludwig@uos.de>
+ */
+class DbCache extends Cache
+{
+ /**
+ * @return string A display name (that can be translated) for this cache class.
+ */
+ public static function getDisplayName(): string
+ {
+ return _('Datenbank');
+ }
+
+ /**
+ * Expire item from the cache.
+ *
+ * @param string $arg a single key
+ */
+ public function expire($arg)
+ {
+ $db = DBManager::get();
+
+ $stmt = $db->prepare('DELETE FROM cache WHERE cache_key = ?');
+ $stmt->execute([$arg]);
+ }
+
+ /**
+ * Expire all items from the cache.
+ */
+ public function flush()
+ {
+ $db = DBManager::get();
+
+ $db->exec('TRUNCATE TABLE cache');
+ }
+
+ /**
+ * Delete all expired items from the cache.
+ */
+ public function purge()
+ {
+ $db = DBManager::get();
+
+ $stmt = $db->prepare('DELETE FROM cache WHERE expires < ?');
+ $stmt->execute([time()]);
+ }
+
+ /**
+ * Return statistics.
+ *
+ * @return array|array[]
+ *@see Cache::getStats()
+ *
+ */
+ public function getStats(): array
+ {
+ return [
+ __CLASS__ => [
+ 'name' => _('Anzahl Einträge'),
+ 'value' => DBManager::get()->fetchColumn("SELECT COUNT(*) FROM `cache`")
+ ]
+ ];
+ }
+
+ /**
+ * Return the Vue component name and props that handle configuration.
+ *
+ * @return array
+ *@see Cache::getConfig()
+ *
+ */
+ public static function getConfig(): array
+ {
+ return [
+ 'component' => null,
+ 'props' => []
+ ];
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public function getItem(string $key): CacheItemInterface
+ {
+ $query = "SELECT `content`, `expires`
+ FROM `cache`
+ WHERE `cache_key` = :key
+ AND `expires` > UNIX_TIMESTAMP()";
+ $result = DBManager::get()->fetchOne($query, [':key' => $key]);
+
+ $item = new Item($key);
+ if (!empty($result)) {
+ $item->setHit();
+ if ($result['content']) {
+ $item->set(unserialize($result['content']));
+ }
+ if ($result['expires']) {
+ $expiration = new \DateTime();
+ $expiration->setTimestamp($result['expires']);
+ $item->expiresAt($expiration);
+ }
+ }
+ return $item;
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public function hasItem(string $key): bool
+ {
+ $query = "SELECT 1
+ FROM `cache`
+ WHERE `cache_key` = :key
+ AND `expires` > UNIX_TIMESTAMP()";
+ return (bool) DBManager::get()->fetchColumn($query, [':key' => $key]);
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public function save(CacheItemInterface $item): bool
+ {
+ $expiration = $this->getExpiration($item);
+ if ($expiration < 1) {
+ // The item would expire immediately.
+ return false;
+ }
+
+ return DBManager::get()->execute(
+ 'REPLACE INTO `cache` VALUES (?, ?, ?)',
+ [$item->getKey(), serialize($item->get()), $expiration]
+ );
+ }
+}