|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Opencast\Caching; |
| 4 | + |
| 5 | +use Opencast\Models\Videos; |
| 6 | + |
| 7 | +class VideosCaching |
| 8 | +{ |
| 9 | + private $cache_factory; |
| 10 | + private $cache_name; |
| 11 | + const OC_CACHE_KEY_DOMAIN_USERS = 'OpencastV3/videos/users/'; |
| 12 | + const OC_CACHE_KEY_DOMAIN_COURSES = 'OpencastV3/videos/courses/'; |
| 13 | + const OC_CACHE_KEY_DOMAIN_PLAYLIST = 'OpencastV3/videos/playlist/'; |
| 14 | + |
| 15 | + public function __construct() { |
| 16 | + $this->cache_factory = \StudipCacheFactory::getCache(); |
| 17 | + } |
| 18 | + |
| 19 | + public function userVideos(string $user_id) |
| 20 | + { |
| 21 | + $this->cache_name = self::OC_CACHE_KEY_DOMAIN_USERS . $user_id; |
| 22 | + return $this; |
| 23 | + } |
| 24 | + |
| 25 | + public function courseVideos(string $course_id) |
| 26 | + { |
| 27 | + $this->cache_name = self::OC_CACHE_KEY_DOMAIN_COURSES . $course_id; |
| 28 | + return $this; |
| 29 | + } |
| 30 | + |
| 31 | + public function playlistVideos(int $playlist_id) |
| 32 | + { |
| 33 | + $this->cache_name = self::OC_CACHE_KEY_DOMAIN_PLAYLIST . $playlist_id; |
| 34 | + return $this; |
| 35 | + } |
| 36 | + |
| 37 | + public function readAll() |
| 38 | + { |
| 39 | + if (empty($this->cache_name)) { |
| 40 | + throw new \Error('Unable to read the cache due to missing cache name!'); |
| 41 | + } |
| 42 | + |
| 43 | + $content = $this->cache_factory->read($this->cache_name); |
| 44 | + return $content ? unserialize($content) : []; |
| 45 | + } |
| 46 | + |
| 47 | + public function read(string $unique_query_id) |
| 48 | + { |
| 49 | + if (empty($unique_query_id)) { |
| 50 | + throw new \Error('Unable to read the cache due to missing cache name!'); |
| 51 | + } |
| 52 | + |
| 53 | + $all = $this->readAll(); |
| 54 | + |
| 55 | + if (!isset($all[$unique_query_id])) { |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + return $all[$unique_query_id]; |
| 60 | + } |
| 61 | + |
| 62 | + public function write($unique_query_id, $content) |
| 63 | + { |
| 64 | + if (empty($unique_query_id)) { |
| 65 | + throw new \Error('Unable to write the cache data due to missing cache name!'); |
| 66 | + } |
| 67 | + |
| 68 | + $all = $this->readAll(); |
| 69 | + |
| 70 | + $all[$unique_query_id] = $content; |
| 71 | + |
| 72 | + $serialized_records = serialize($all); |
| 73 | + |
| 74 | + return $this->cache_factory->write($this->cache_name, $serialized_records); |
| 75 | + } |
| 76 | + |
| 77 | + public function delete($unique_query_id) |
| 78 | + { |
| 79 | + if (empty($unique_query_id)) { |
| 80 | + throw new \Error('Unable to expire the cache data due to missing cache name!'); |
| 81 | + } |
| 82 | + |
| 83 | + $all = $this->readAll(); |
| 84 | + |
| 85 | + if (empty($all)) { |
| 86 | + return true; |
| 87 | + } |
| 88 | + |
| 89 | + if (isset($all[$unique_query_id])) { |
| 90 | + unset($all[$unique_query_id]); |
| 91 | + } |
| 92 | + |
| 93 | + if (empty($all)) { |
| 94 | + $this->expire(); |
| 95 | + return true; |
| 96 | + } |
| 97 | + |
| 98 | + return $this->cache_factory->write($this->cache_name, serialize($all)); |
| 99 | + } |
| 100 | + |
| 101 | + public function expire() |
| 102 | + { |
| 103 | + $this->cache_factory->expire($this->cache_name); |
| 104 | + } |
| 105 | + |
| 106 | + public static function expireAllVideoCaches(Videos $video) |
| 107 | + { |
| 108 | + $cache = \StudipCacheFactory::getCache(); |
| 109 | + if (!empty($video->perms)) { |
| 110 | + foreach ($video->perms->pluck('user_id') as $user_id) { |
| 111 | + $cache->expire(self::OC_CACHE_KEY_DOMAIN_USERS . $user_id); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + if (!empty($video->playlists)) { |
| 116 | + foreach ($video->playlists as $playlist) { |
| 117 | + $cache->expire(self::OC_CACHE_KEY_DOMAIN_PLAYLIST . $playlist->id); |
| 118 | + |
| 119 | + if (!empty($playlist->courses)) { |
| 120 | + foreach ($playlist->courses as $course) { |
| 121 | + $cache->expire(self::OC_CACHE_KEY_DOMAIN_COURSES . $course->id); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + // We need to also look for root accounts! |
| 128 | + $stmt = \DBManager::get()->prepare($q = "SELECT `user_id` FROM `auth_user_md5` WHERE `perms` = :perm"); |
| 129 | + $stmt->execute([ |
| 130 | + ':perm' => 'root', |
| 131 | + ]); |
| 132 | + $root_user_ids = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
| 133 | + foreach ($root_user_ids as $root_user_id) { |
| 134 | + $cache->expire(self::OC_CACHE_KEY_DOMAIN_USERS . $root_user_id); |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments