Services
CacheServiceInterface
v1.0.0TTL-based cache interface for expensive computations and remote API responses.
Access
$ctx->cache()?->set('dashboard_stats', $stats, 300);How To Use It
- Use it for module-local caches that avoid repeated remote calls or heavy data reshaping on every admin request.
- Keep cache keys narrow and TTLs explicit so invalidation remains predictable during support and debugging.
Works Well With
Methods
public function get(string $key, mixed $default = null): mixedReads a cached value or returns the fallback.
public function set(string $key, mixed $value, int $ttl = 3600): voidStores a value for a given TTL in seconds.
public function delete(string $key): voidDeletes a single cached key from the module namespace.
public function flush(): voidClears the whole module cache namespace.
Example
bootstrap.php
public function refreshStats(SdkContext $ctx): array
{
$cached = $ctx->cache()?->get('dashboard_stats');
if (is_array($cached)) {
return $cached;
}
$stats = $ctx->http()?->get('https://api.example.com/stats') ?? [];
$ctx->cache()?->set('dashboard_stats', $stats, 300);
return $stats;
}