CacheWrapper
in package
implements
CacheItemPoolInterface, CacheInterface
CacheWrapper
- storage for underlying Symfony Cache (PSR-6) instance.
- Wrapper methods for common PSR-6 operations + useful helpers (get/set/remember/getMultiple/setMultiple/tag).
- If you later change cache implementation (another PSR-6 adapter), replace adapter via setAdapter().
Table of Contents
Interfaces
- CacheItemPoolInterface
- CacheInterface
Properties
- $logger : LoggerInterface|null
- Optional logger for debug (not required).
- $pool : CacheItemPoolInterface|null
- PSR-6 cache pool instance (shared across process).
- $tagAware : bool
- Cache adapter supports TagAwareAdapter.
Methods
- clear() : bool
- Clear all cache.
- commit() : bool
- Commit deferred saves.
- debugState() : array<string|int, mixed>
- Return an array summary of wrapper state (for debugging).
- delete() : bool
- deleteItem() : bool
- Delete a single item.
- deleteItems() : bool
- Delete multiple items.
- deleteMultiple() : bool
- Delete multiple keys.
- get() : mixed
- Simple get - similar to PSR-16 get:
- getAdapter() : CacheItemPoolInterface|null
- Return currently configured adapter or null.
- getItem() : CacheItemInterface
- Get a CacheItem by key.
- getItems() : array<string|int, CacheItemInterface>
- Get multiple items by keys.
- getMultiple() : iterable<string, mixed>
- Get multiple values by keys. Returns associative array key => value (not CacheItem).
- has() : bool
- hasItem() : bool
- Check item existence.
- initFilesystemAdapter() : void
- Create and set a Symfony FilesystemAdapter as the pool.
- invalidateTags() : bool
- Invalidate by tags - if adapter supports TagAwareAdapter->invalidateTags()
- isTagAware() : bool
- remember() : mixed
- Get or compute value and save it (atomic-ish for single process).
- reset() : void
- Clear adapter (for tests).
- save() : bool
- Save item.
- saveDeferred() : bool
- Save deferred.
- set() : bool
- Simple set helper - TTL in seconds or DateInterval; returns bool
- setAdapter() : void
- Set the PSR-6 cache adapter instance (static).
- setLogger() : void
- Set optional PSR-3 logger for internal debug.
- setMultiple() : bool
- Set multiple values. $values is associative array key => value.
- supportsTags() : bool
- Check whether underlying adapter supports tagging.
- tagItem() : bool
- Tag an item (if supported). Returns true on success.
- ensureAdapter() : CacheItemPoolInterface
- Ensure adapter present; throw if not.
Properties
$logger
Optional logger for debug (not required).
private
static LoggerInterface|null
$logger
= null
$pool
PSR-6 cache pool instance (shared across process).
private
static CacheItemPoolInterface|null
$pool
= null
Set via setAdapter() or initFilesystemAdapter().
$tagAware
Cache adapter supports TagAwareAdapter.
private
static bool
$tagAware
= false
Methods
clear()
Clear all cache.
public
clear() : bool
Return values
boolcommit()
Commit deferred saves.
public
commit() : bool
Return values
booldebugState()
Return an array summary of wrapper state (for debugging).
public
debugState() : array<string|int, mixed>
Return values
array<string|int, mixed>delete()
public
delete(string $key) : bool
Parameters
- $key : string
Return values
booldeleteItem()
Delete a single item.
public
deleteItem(string $key) : bool
Parameters
- $key : string
Return values
booldeleteItems()
Delete multiple items.
public
deleteItems(array<string|int, mixed> $keys) : bool
Parameters
- $keys : array<string|int, mixed>
Return values
booldeleteMultiple()
Delete multiple keys.
public
deleteMultiple(array<string|int, string> $keys) : bool
Parameters
- $keys : array<string|int, string>
Return values
boolget()
Simple get - similar to PSR-16 get:
public
get(string $key[, mixed $default = null ]) : mixed
Parameters
- $key : string
- $default : mixed = null
getAdapter()
Return currently configured adapter or null.
public
static getAdapter() : CacheItemPoolInterface|null
Return values
CacheItemPoolInterface|nullgetItem()
Get a CacheItem by key.
public
getItem(string $key) : CacheItemInterface
Parameters
- $key : string
Return values
CacheItemInterfacegetItems()
Get multiple items by keys.
public
getItems([array<string|int, mixed> $keys = [] ]) : array<string|int, CacheItemInterface>
Parameters
- $keys : array<string|int, mixed> = []
Return values
array<string|int, CacheItemInterface>getMultiple()
Get multiple values by keys. Returns associative array key => value (not CacheItem).
public
getMultiple(array<string|int, string> $keys[, mixed $default = null ]) : iterable<string, mixed>
Missing keys will have $default value.
Parameters
- $keys : array<string|int, string>
- $default : mixed = null
Return values
iterable<string, mixed>has()
public
has(string $key) : bool
Parameters
- $key : string
Return values
boolhasItem()
Check item existence.
public
hasItem(string $key) : bool
Parameters
- $key : string
Return values
boolinitFilesystemAdapter()
Create and set a Symfony FilesystemAdapter as the pool.
public
initFilesystemAdapter([string $namespace = '' ][, int $defaultLifetime = 0 ][, string|null $directory = null ][, bool $tagAware = true ]) : void
Usage: CacheWrapper::initFilesystemAdapter('app_cache', 3600, '/tmp/my_cache');
Parameters
- $namespace : string = ''
- $defaultLifetime : int = 0
-
seconds
- $directory : string|null = null
-
cache directory; if null, sys_get_temp_dir() is used
- $tagAware : bool = true
invalidateTags()
Invalidate by tags - if adapter supports TagAwareAdapter->invalidateTags()
public
invalidateTags(array<string|int, string> $tags) : bool
Parameters
- $tags : array<string|int, string>
Return values
boolisTagAware()
public
isTagAware() : bool
Return values
boolremember()
Get or compute value and save it (atomic-ish for single process).
public
remember(string $key, callable(CacheItemInterface): mixed $callback[, int|DateInterval|null $ttl = null ][, array<string|int, mixed> $tags = [] ]) : mixed
$callback receives CacheItemInterface and should return the value to store.
Parameters
- $key : string
- $callback : callable(CacheItemInterface): mixed
- $ttl : int|DateInterval|null = null
- $tags : array<string|int, mixed> = []
reset()
Clear adapter (for tests).
public
reset() : void
save()
Save item.
public
save(CacheItemInterface $item) : bool
Parameters
- $item : CacheItemInterface
Return values
boolsaveDeferred()
Save deferred.
public
saveDeferred(CacheItemInterface $item) : bool
Parameters
- $item : CacheItemInterface
Return values
boolset()
Simple set helper - TTL in seconds or DateInterval; returns bool
public
set(string $key, mixed $value[, int|DateInterval|null $ttl = null ]) : bool
Parameters
- $key : string
- $value : mixed
- $ttl : int|DateInterval|null = null
Return values
boolsetAdapter()
Set the PSR-6 cache adapter instance (static).
public
static setAdapter(CacheItemPoolInterface $adapter) : void
Parameters
- $adapter : CacheItemPoolInterface
setLogger()
Set optional PSR-3 logger for internal debug.
public
static setLogger(LoggerInterface|null $logger) : void
Parameters
- $logger : LoggerInterface|null
setMultiple()
Set multiple values. $values is associative array key => value.
public
setMultiple(iterable<string, mixed> $values[, int|DateInterval|null $ttl = null ]) : bool
Returns true on success.
Parameters
- $values : iterable<string, mixed>
- $ttl : int|DateInterval|null = null
Return values
boolsupportsTags()
Check whether underlying adapter supports tagging.
public
supportsTags() : bool
Return values
booltagItem()
Tag an item (if supported). Returns true on success.
public
tagItem(string $key, array<string|int, string> $tags) : bool
Parameters
- $key : string
- $tags : array<string|int, string>
Return values
boolensureAdapter()
Ensure adapter present; throw if not.
private
ensureAdapter() : CacheItemPoolInterface