aboutsummaryrefslogtreecommitdiff
path: root/lib/classes
diff options
context:
space:
mode:
authorRami Jasim <rami.jasim1@uni-oldenburg.de>2025-01-27 13:01:54 +0100
committerRami Jasim <rami.jasim1@uni-oldenburg.de>2025-01-27 13:01:54 +0100
commit27e141b98eb4395736ef605275647b4dda2123ef (patch)
tree0f8b16e57d081d7c1706e1ede8b5f93ec16a47d8 /lib/classes
parente5a8b35103c4079b8339e8a60b9a1d105c507e6f (diff)
add general caching structureissue-5183
Diffstat (limited to 'lib/classes')
-rw-r--r--lib/classes/MyRealmModel.php21
-rw-r--r--lib/classes/cache/DbCache.php29
2 files changed, 47 insertions, 3 deletions
diff --git a/lib/classes/MyRealmModel.php b/lib/classes/MyRealmModel.php
index b1317b4..4b1147b 100644
--- a/lib/classes/MyRealmModel.php
+++ b/lib/classes/MyRealmModel.php
@@ -521,6 +521,7 @@ class MyRealmModel
// -- 2. Fetch the Navigation per StudipModule
$all_course_ids = array_keys($all_courses);
$visits = get_objects_visits($all_course_ids, 0, null, null, array_keys($activated_tools));
+ $cache = \Studip\Cache\Factory::getCache();
foreach ($activated_tools as $plugin_id => $plugin_data) {
if (!Config::get()->VOTE_ENABLE && $plugin_id === -1) {
continue;
@@ -534,9 +535,23 @@ class MyRealmModel
$navigation[$c_id][$plugin_id] = self::checkVote($all_courses[$c_id], $user_id, $c_id);
}
} elseif ($plugin_data['studip_module'] instanceof StudipModuleExtended) {
- $fetched_navs = $plugin_data['studip_module']->getManyIconNavigation($c_ids, $user_id);
- foreach ($fetched_navs as $fetched_c_id => $fetched_nav) {
- $navigation[$fetched_c_id][$plugin_id] = $fetched_nav;
+ $cache_locs = array_map(fn ($c_id) => StudipModuleExtended::ICON_NAV_CACHE_PATH . "$user_id/$plugin_id/$c_id", $c_ids);
+ $cached_navs = $cache->getItems($cache_locs);
+ $to_fetch = [];
+ foreach ($cached_navs as $key => $cached_item) {
+ $c_id = explode('/', $key)[3];
+ if ($cached_item->isHit()) {
+ $navigation[$c_id][$plugin_id] = unserialize($cached_item->get());
+ } else {
+ $to_fetch[] = $c_id;
+ }
+ }
+ if ($to_fetch) {
+ $fetched_navs = $plugin_data['studip_module']->getManyIconNavigation($to_fetch, $user_id);
+ foreach ($fetched_navs as $fetched_c_id => $fetched_nav) {
+ $cache->write(StudipModuleExtended::ICON_NAV_CACHE_PATH . "$user_id/$plugin_id/$fetched_c_id", serialize($fetched_nav));
+ $navigation[$fetched_c_id][$plugin_id] = $fetched_nav;
+ }
}
} else {
foreach ($c_ids as $c_id) {
diff --git a/lib/classes/cache/DbCache.php b/lib/classes/cache/DbCache.php
index 5714ce9..783ab10 100644
--- a/lib/classes/cache/DbCache.php
+++ b/lib/classes/cache/DbCache.php
@@ -112,6 +112,35 @@ class DbCache extends Cache
return $item;
}
+ public function getItems(array $keys = []): array
+ {
+ $query = "SELECT `cache_key`, `content`, `expires`
+ FROM `cache`
+ WHERE `cache_key` IN (:keys)
+ AND `expires` > UNIX_TIMESTAMP()";
+ $results = DBManager::get()->fetchAll($query, [':keys' => $keys]);
+ $items = [];
+ foreach ($results as $result) {
+ $item = new Item($result['cache_key']);
+ $item->setHit();
+ if ($result['content']) {
+ $item->set(unserialize($result['content']));
+ }
+ if ($result['expires']) {
+ $expiration = new \DateTime();
+ $expiration->setTimestamp($result['expires']);
+ $item->expiresAt($expiration);
+ }
+ $items[$result['cache_key']] = $item;
+ }
+ // Don't return null, fill remaining keys
+ foreach (array_diff($keys, array_keys($items)) as $remaining_key) {
+ $items[$remaining_key] = new Item($remaining_key);
+ }
+ return $items;
+ }
+
+
/**
* @inheritDoc
*/