Tsugi

Cache
in package

A very simple Cache - currently implemented in session

Someday we will build a better cache but for now this simply saves re-retrieving or re-constructing a few bits of user data later in the same request or in a later request. Everything is saved in $_SESSION.

Since the cache is in $_SESSION, it is already per user, per link_id. And within that we have "cache locations". Each location, can have one key and one value. We have this limitation because we don't want our simple session based cache to grow forever. Each location stores only one entry.

For example in the loadUserInfoByPass() routine, we are looking at users other than our own (usually as instructor). To make sure we only make the SQL query once, we make the following call:

Cache::set('lti_user', $user_id, $row);

This will overwrite any other user_id value in the 'lti_user' slot. In effect there is one 'lti_user' slot and we store a a key (which might change) from call to call and a value (the row of retrieved data).

This strictly bounds overall cache data in the session to the number of cache locations and avoids any need for a garbage collection or an independent expire process.

Context-scoped entries (setContext / getContext) use the same one-slot-per-location rule: each $cacheloc holds at most one bundle { context_id, ts, ttl, value }. A get for a different context removes the slot and returns null. Expiry uses ts + ttl (seconds); ttl 0 means no time-based expiry (context mismatch still invalidates).

Table of Contents

Methods

check()  : mixed
Check and return a value from the cache.
clear()  : mixed
Delete an entry from the cache.
clearAllSessionCaches()  : mixed
Remove every Tsugi session cache entry (session keys starting with "cache_").
clearContext()  : mixed
Remove the context cache entry for this location (any stored context).
expires()  : mixed
Check when value in the cache expires
getContext()  : mixed|null
Return the cached value for this location if context matches and entry is not expired.
invalidateContext()  : mixed
Remove the entry only if it is currently cached for the given context_id.
set()  : mixed
Place an entry in the cache.
setContext()  : mixed
Store a value for one cache location, tied to a context id and optional TTL (seconds).

Methods

check()

Check and return a value from the cache.

public static check(mixed $cacheloc, mixed $cachekey) : mixed

Returns false if there is no entry.

Parameters
$cacheloc : mixed
$cachekey : mixed

clear()

Delete an entry from the cache.

public static clear(mixed $cacheloc) : mixed
Parameters
$cacheloc : mixed

clearAllSessionCaches()

Remove every Tsugi session cache entry (session keys starting with "cache_").

public static clearAllSessionCaches() : mixed

Use after login or other events where cached user/context data must not leak across identities.

clearContext()

Remove the context cache entry for this location (any stored context).

public static clearContext(mixed $cacheloc) : mixed
Parameters
$cacheloc : mixed

expires()

Check when value in the cache expires

public static expires(mixed $cacheloc, mixed $cachekey) : mixed

Returns false if there is no entry.

Parameters
$cacheloc : mixed
$cachekey : mixed

getContext()

Return the cached value for this location if context matches and entry is not expired.

public static getContext(string $cacheloc, int $context_id) : mixed|null

If the slot exists but context_id does not match, the entry is removed and null is returned. If the entry is time-expired (ttl > 0 and age >= ttl), it is removed and null is returned.

Parameters
$cacheloc : string

Location name.

$context_id : int

Requesting context; must match stored context_id.

Return values
mixed|null

invalidateContext()

Remove the entry only if it is currently cached for the given context_id.

public static invalidateContext(mixed $cacheloc, mixed $context_id) : mixed
Parameters
$cacheloc : mixed
$context_id : mixed

set()

Place an entry in the cache.

public static set(mixed $cacheloc, mixed $cachekey, mixed $cacheval[, mixed $expiresec = false ]) : mixed

We don't cache null or false if that was our value.

Parameters
$cacheloc : mixed
$cachekey : mixed
$cacheval : mixed
$expiresec : mixed = false

setContext()

Store a value for one cache location, tied to a context id and optional TTL (seconds).

public static setContext(string $cacheloc, int $context_id, mixed $value[, int|false $expiresec = false ]) : mixed

Same session slot as getContext: overwrites any previous entry for this $cacheloc.

Parameters
$cacheloc : string

Location name (becomes cache_ prefix in session).

$context_id : int

Context this value belongs to.

$value : mixed

Data to cache; null or false removes the entry.

$expiresec : int|false = false

TTL in seconds from stored ts, or false/0 for no time expiry.


        
On this page

Search results