CacheMiddleware
extends AbstractMiddleware
in package
Middleware for caching Canvas API responses.
Caches successful GET requests to reduce API calls and improve performance. Supports multiple cache backends and configurable TTL strategies.
Table of Contents
Properties
- $config : array<string, mixed>
- $adapter : CacheAdapterInterface
- $keyGenerator : CacheKeyGenerator
- $serializer : ResponseSerializer
- $ttlStrategy : TtlStrategy
Methods
- __construct() : mixed
- Constructor.
- __invoke() : callable
- Get the Guzzle middleware callable
- clearCache() : void
- Clear all cached entries.
- configure() : void
- Set configuration for the middleware
- getAdapter() : CacheAdapterInterface
- Get the cache adapter.
- getName() : string
- Get the name of the middleware
- getStatistics() : array{hits: int, misses: int, size: int, entries: int}
- Get cache statistics.
- setAdapter() : void
- Set the cache adapter.
- getConfig() : mixed
- Get a configuration value
- getDefaultConfig() : array<string, mixed>
- Get default configuration for the middleware
- executeAndCache() : PromiseInterface
- Execute the request and cache the response.
- getInvalidationPatterns() : array<int, string>
- Get cache invalidation patterns for a mutation.
- invalidateOnMutation() : void
- Invalidate cache entries on mutation.
- isCachingEnabled() : bool
- Check if caching is enabled.
- shouldCacheResponse() : bool
- Check if response should be cached.
- shouldRefreshCache() : bool
- Check if cache should be refreshed.
Properties
$config
protected
array<string, mixed>
$config
= []
$adapter
private
CacheAdapterInterface
$adapter
The cache adapter
$keyGenerator
private
CacheKeyGenerator
$keyGenerator
The cache key generator
$serializer
private
ResponseSerializer
$serializer
The response serializer
$ttlStrategy
private
TtlStrategy
$ttlStrategy
The TTL strategy
Methods
__construct()
Constructor.
public
__construct([array<string, mixed> $config = [] ]) : mixed
Parameters
- $config : array<string, mixed> = []
-
Configuration options
__invoke()
Get the Guzzle middleware callable
public
__invoke() : callable
Return values
callableclearCache()
Clear all cached entries.
public
clearCache() : void
configure()
Set configuration for the middleware
public
configure(array<string|int, mixed> $config) : void
Parameters
- $config : array<string|int, mixed>
Tags
getAdapter()
Get the cache adapter.
public
getAdapter() : CacheAdapterInterface
Return values
CacheAdapterInterface —The cache adapter
getName()
Get the name of the middleware
public
getName() : string
Return values
stringgetStatistics()
Get cache statistics.
public
getStatistics() : array{hits: int, misses: int, size: int, entries: int}
Return values
array{hits: int, misses: int, size: int, entries: int}setAdapter()
Set the cache adapter.
public
setAdapter(CacheAdapterInterface $adapter) : void
Parameters
- $adapter : CacheAdapterInterface
-
The cache adapter
getConfig()
Get a configuration value
protected
getConfig(string $key[, mixed $default = null ]) : mixed
Parameters
- $key : string
- $default : mixed = null
getDefaultConfig()
Get default configuration for the middleware
protected
getDefaultConfig() : array<string, mixed>
Return values
array<string, mixed>executeAndCache()
Execute the request and cache the response.
private
executeAndCache(RequestInterface $request, callable $handler, array<string, mixed> $options[, string|null $cacheKey = null ]) : PromiseInterface
Parameters
- $request : RequestInterface
-
The request
- $handler : callable
-
The handler
- $options : array<string, mixed>
-
The options
- $cacheKey : string|null = null
-
The cache key (will be generated if not provided)
Return values
PromiseInterface —The response promise
getInvalidationPatterns()
Get cache invalidation patterns for a mutation.
private
getInvalidationPatterns(string $method, string $path) : array<int, string>
Parameters
- $method : string
-
The HTTP method
- $path : string
-
The request path
Return values
array<int, string> —The invalidation patterns
invalidateOnMutation()
Invalidate cache entries on mutation.
private
invalidateOnMutation(RequestInterface $request) : void
Parameters
- $request : RequestInterface
-
The mutation request
isCachingEnabled()
Check if caching is enabled.
private
isCachingEnabled(array<string, mixed> $options) : bool
Parameters
- $options : array<string, mixed>
-
Request options
Return values
bool —True if caching is enabled
shouldCacheResponse()
Check if response should be cached.
private
shouldCacheResponse(ResponseInterface $response) : bool
Parameters
- $response : ResponseInterface
-
The response
Return values
bool —True if response should be cached
shouldRefreshCache()
Check if cache should be refreshed.
private
shouldRefreshCache(array<string, mixed> $options) : bool
Parameters
- $options : array<string, mixed>
-
Request options
Return values
bool —True if cache should be refreshed