Services

CacheServiceInterface

v1.0.0

TTL-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.

Methods

public function get(string $key, mixed $default = null): mixed

Reads a cached value or returns the fallback.

public function set(string $key, mixed $value, int $ttl = 3600): void

Stores a value for a given TTL in seconds.

public function delete(string $key): void

Deletes a single cached key from the module namespace.

public function flush(): void

Clears 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;
}