ILIAS  trunk Revision v12.0_alpha-413-g215742c0453
ILIAS\News\Persistence\NewsCache Class Reference

Multi-Level News Cache Implementation: More...

+ Collaboration diagram for ILIAS\News\Persistence\NewsCache:

Public Member Functions

 __construct ()
 
 getAggregatedContexts (array $contexts)
 Level-1 Cache stores a collection of the aggregated contexts for the provided base context. More...
 
 storeAggregatedContexts (array $contexts, array $aggregated)
 
 invalidateAggregatedContexts (array $contexts)
 
 getUserContextAccess (int $user_id, NewsCriteria $criteria)
 Level-2 Cache stores a collection of the base news contexts for a specific user. More...
 
 storeUserContextAccess (int $user_id, NewsCriteria $criteria, array $contexts)
 
 invalidateUserContextAccess (int $user_id)
 
 getNewsForUser (int $user_id, NewsCriteria $criteria)
 Level-3 Cache stores a collection of the news items for a specific user. More...
 
 storeNewsForUser (int $user_id, NewsCriteria $criteria, NewsCollection $news)
 
 invalidateNewsForUser (int $user_id, NewsCriteria $criteria)
 
 flush ()
 

Protected Member Functions

 generateL1Key (string|array $contexts)
 
 generateL3Key (int $user_id, NewsCriteria $criteria)
 

Protected Attributes

readonly bool $enabled
 
readonly int $cache_ttl
 
readonly ilCache $il_cache
 

Detailed Description

Multi-Level News Cache Implementation:

Definition at line 35 of file NewsCache.php.

Constructor & Destructor Documentation

◆ __construct()

ILIAS\News\Persistence\NewsCache::__construct ( )

Definition at line 42 of file NewsCache.php.

43 {
44 $settings = new \ilSetting('news');
45
46 $this->cache_ttl = (int) $settings->get('acc_cache_mins');
47 $this->enabled = $this->cache_ttl !== 0;
48
49 $this->il_cache = new \ilCache('ServicesNews', 'NewsMultiLevel', true);
50 $this->il_cache->setExpiresAfter($this->cache_ttl * 60);
51 }

References ILIAS\Repository\int().

+ Here is the call graph for this function:

Member Function Documentation

◆ flush()

ILIAS\News\Persistence\NewsCache::flush ( )

Definition at line 224 of file NewsCache.php.

224 : void
225 {
226 $this->il_cache->deleteAllEntries();
227 }

◆ generateL1Key()

ILIAS\News\Persistence\NewsCache::generateL1Key ( string|array  $contexts)
protected

Definition at line 78 of file NewsCache.php.

78 : string
79 {
80 return 'agg:' . md5(is_array($contexts) ? implode(',', $contexts) : $contexts);
81 }

Referenced by ILIAS\News\Persistence\NewsCache\invalidateAggregatedContexts(), and ILIAS\News\Persistence\NewsCache\storeAggregatedContexts().

+ Here is the caller graph for this function:

◆ generateL3Key()

ILIAS\News\Persistence\NewsCache::generateL3Key ( int  $user_id,
NewsCriteria  $criteria 
)
protected

Definition at line 205 of file NewsCache.php.

205 : string
206 {
207 $payload = [
208 'start_date' => $criteria->getStartDate(),
209 'min_priority' => $criteria->getMinPriority(),
210 'max_priority' => $criteria->getMaxPriority(),
211 'no_auto_generated' => $criteria->isNoAutoGenerated(),
212 ];
213
214 // The Period of entries only needs to be considered if cache entries are stored for longer periods
215 $period_minutes = ($criteria->getPeriod() ?? 0) * 1440;
216 if ($period_minutes <= $this->cache_ttl) {
217 $payload['period'] = $criteria->getPeriod();
218 }
219
220 return "user:{$user_id}:" . md5(serialize($payload));
221 }
if(count($parts) !=3) $payload
Definition: ltitoken.php:67

References $payload, ILIAS\News\Data\NewsCriteria\getMaxPriority(), ILIAS\News\Data\NewsCriteria\getMinPriority(), ILIAS\News\Data\NewsCriteria\getPeriod(), ILIAS\News\Data\NewsCriteria\getStartDate(), and ILIAS\News\Data\NewsCriteria\isNoAutoGenerated().

Referenced by ILIAS\News\Persistence\NewsCache\getNewsForUser(), ILIAS\News\Persistence\NewsCache\invalidateNewsForUser(), and ILIAS\News\Persistence\NewsCache\storeNewsForUser().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getAggregatedContexts()

ILIAS\News\Persistence\NewsCache::getAggregatedContexts ( array  $contexts)

Level-1 Cache stores a collection of the aggregated contexts for the provided base context.

This method uses a greedy algorithm to collect subset matches in the cache and return both cache hits (as complete NewsContexts objects) and missing contexts.

Parameters
NewsContext[]$contexts
Returns
array{hit: NewsContext[], missing: NewsContext[]}

Definition at line 61 of file NewsCache.php.

61 : array
62 {
63 if (!$this->enabled || empty($contexts)) {
64 return ['hit' => [], 'missing' => $contexts];
65 }
66
67 $context_ids = array_map(fn($context) => $context->getRefId(), $contexts);
68 sort($context_ids, SORT_NUMERIC);
69
70 if ($entry = $this->il_cache->getEntry($this->generateL1Key($context_ids))) {
71 $contexts = array_map(fn($raw) => NewsContext::denormalize($raw), unserialize($entry));
72 return ['hit' => $contexts, 'missing' => []];
73 }
74
75 return ['hit' => [], 'missing' => $contexts];
76 }
static denormalize(array $raw)
Create new object from reduced array representation.
$context
Definition: webdav.php:31

References $context, ILIAS\News\Data\NewsContext\denormalize(), and ILIAS\UI\examples\Symbol\Glyph\Sort\sort().

+ Here is the call graph for this function:

◆ getNewsForUser()

ILIAS\News\Persistence\NewsCache::getNewsForUser ( int  $user_id,
NewsCriteria  $criteria 
)

Level-3 Cache stores a collection of the news items for a specific user.

It returns a LazyNewsCollection or null on cache miss.

Definition at line 172 of file NewsCache.php.

172 : ?LazyNewsCollection
173 {
174 if (!$this->enabled) {
175 return null;
176 }
177
178 $entry = $this->il_cache->getEntry($this->generateL3Key($user_id, $criteria));
179 if (!$entry) {
180 return null;
181 }
182
183 $payload = unserialize($entry);
184 return (new LazyNewsCollection(array_keys($payload)))
185 ->setUserReadStatus($user_id, $payload);
186 }
generateL3Key(int $user_id, NewsCriteria $criteria)
Definition: NewsCache.php:205

References $payload, $user_id, and ILIAS\News\Persistence\NewsCache\generateL3Key().

+ Here is the call graph for this function:

◆ getUserContextAccess()

ILIAS\News\Persistence\NewsCache::getUserContextAccess ( int  $user_id,
NewsCriteria  $criteria 
)

Level-2 Cache stores a collection of the base news contexts for a specific user.

It returns a list of the NewsContexts (ref_id only) or null on cache miss.

Returns
NewsContext[]|null

Definition at line 125 of file NewsCache.php.

125 : ?array
126 {
127 if (!$this->enabled) {
128 return null;
129 }
130
131 $entry = $this->il_cache->getEntry("access:{$user_id}");
132 if (!$entry) {
133 return null;
134 }
135
136 // Check if the stored payload matches the criteria
137 $payload = unserialize($entry);
138 if ($payload['only_public'] !== $criteria->isOnlyPublic()) {
140 return null;
141 }
142
143 return array_map(fn($ref_id) => new NewsContext($ref_id), $payload['contexts']);
144 }
invalidateUserContextAccess(int $user_id)
Definition: NewsCache.php:160
$ref_id
Definition: ltiauth.php:66

References $payload, $ref_id, ILIAS\News\Persistence\NewsCache\invalidateUserContextAccess(), and ILIAS\News\Data\NewsCriteria\isOnlyPublic().

+ Here is the call graph for this function:

◆ invalidateAggregatedContexts()

ILIAS\News\Persistence\NewsCache::invalidateAggregatedContexts ( array  $contexts)
Parameters
NewsContext[]$contexts

Definition at line 104 of file NewsCache.php.

104 : void
105 {
106 if (!$this->enabled || empty($contexts)) {
107 return;
108 }
109
110 $context_ids = array_map(fn($context) => $context->getRefId(), $contexts);
111 sort($context_ids, SORT_NUMERIC);
112 $key = implode(',', $context_ids);
113
114 // Delete cache entry
115 $this->il_cache->deleteEntry($this->generateL1Key($key));
116 }
generateL1Key(string|array $contexts)
Definition: NewsCache.php:78

References $context, ILIAS\News\Persistence\NewsCache\generateL1Key(), and ILIAS\UI\examples\Symbol\Glyph\Sort\sort().

+ Here is the call graph for this function:

◆ invalidateNewsForUser()

ILIAS\News\Persistence\NewsCache::invalidateNewsForUser ( int  $user_id,
NewsCriteria  $criteria 
)

Definition at line 200 of file NewsCache.php.

200 : void
201 {
202 $this->il_cache->deleteEntry($this->generateL3Key($user_id, $criteria));
203 }

References ILIAS\News\Persistence\NewsCache\generateL3Key().

+ Here is the call graph for this function:

◆ invalidateUserContextAccess()

ILIAS\News\Persistence\NewsCache::invalidateUserContextAccess ( int  $user_id)

Definition at line 160 of file NewsCache.php.

160 : void
161 {
162 if ($this->enabled) {
163 $this->il_cache->deleteEntry("access:{$user_id}");
164 }
165 }

Referenced by ILIAS\News\Persistence\NewsCache\getUserContextAccess().

+ Here is the caller graph for this function:

◆ storeAggregatedContexts()

ILIAS\News\Persistence\NewsCache::storeAggregatedContexts ( array  $contexts,
array  $aggregated 
)
Parameters
NewsContext[]$contexts
NewsContext[]$aggregated

Definition at line 87 of file NewsCache.php.

87 : void
88 {
89 if (!$this->enabled || empty($contexts)) {
90 return;
91 }
92
93 $context_ids = array_map(fn($context) => $context->getRefId(), $contexts);
94 sort($context_ids, SORT_NUMERIC);
95 $key = implode(',', $context_ids);
96
97 $payload = array_map(fn($context) => $context->normalize(), $aggregated);
98 $this->il_cache->storeEntry($this->generateL1Key($key), serialize($payload));
99 }

References $context, $payload, ILIAS\News\Persistence\NewsCache\generateL1Key(), and ILIAS\UI\examples\Symbol\Glyph\Sort\sort().

+ Here is the call graph for this function:

◆ storeNewsForUser()

ILIAS\News\Persistence\NewsCache::storeNewsForUser ( int  $user_id,
NewsCriteria  $criteria,
NewsCollection  $news 
)

Definition at line 188 of file NewsCache.php.

188 : void
189 {
190 if (!$this->enabled) {
191 return;
192 }
193
194 $this->il_cache->storeEntry(
195 $this->generateL3Key($user_id, $criteria),
196 serialize($news->getUserReadStatus($user_id))
197 );
198 }

References $user_id, ILIAS\News\Persistence\NewsCache\generateL3Key(), and ILIAS\News\Data\NewsCollection\getUserReadStatus().

+ Here is the call graph for this function:

◆ storeUserContextAccess()

ILIAS\News\Persistence\NewsCache::storeUserContextAccess ( int  $user_id,
NewsCriteria  $criteria,
array  $contexts 
)
Parameters
NewsContext[]$contexts

Definition at line 149 of file NewsCache.php.

149 : void
150 {
151 if (!$this->enabled) {
152 return;
153 }
154
155 $contexts = array_map(fn($context) => $context->getRefId(), $contexts);
156 $payload = ['contexts' => $contexts, 'only_public' => $criteria->isOnlyPublic()];
157 $this->il_cache->storeEntry("access:{$user_id}", serialize($payload));
158 }

References $context, $payload, and ILIAS\News\Data\NewsCriteria\isOnlyPublic().

+ Here is the call graph for this function:

Field Documentation

◆ $cache_ttl

readonly int ILIAS\News\Persistence\NewsCache::$cache_ttl
protected

Definition at line 39 of file NewsCache.php.

◆ $enabled

readonly bool ILIAS\News\Persistence\NewsCache::$enabled
protected

Definition at line 37 of file NewsCache.php.

◆ $il_cache

readonly ilCache ILIAS\News\Persistence\NewsCache::$il_cache
protected

Definition at line 40 of file NewsCache.php.


The documentation for this class was generated from the following file: