CacheCollector
abstract class CacheCollector implements CacheCollectorInterface, DestructableInterface (View source)
Default implementation for CacheCollectorInterface.
By default, the class accounts for caches where calling functions might request keys that won't exist even after a cache rebuild. This prevents situations where a cache rebuild would be triggered over and over due to a 'missing' item. These cases are stored internally as a value of NULL. This means that the CacheCollector::get() method must be overridden if caching data where the values can legitimately be NULL, and where CacheCollector->has() needs to correctly return (equivalent to array_key_exists() vs. isset()). This should not be necessary in the majority of cases.
Properties
| protected string | $cid | The cache id that is used for the cache entry. |
|
| protected array | $tags | A list of tags that are used for the cache entry. |
|
| protected CacheBackendInterface | $cache | The cache backend that should be used. |
|
| protected LockBackendInterface | $lock | The lock backend that should be used. |
|
| protected array | $keysToPersist | An array of keys to add to the cache on service termination. |
|
| protected array | $keysToRemove | An array of keys to remove from the cache on service termination. |
|
| protected array | $storage | Storage for the data itself. |
|
| protected int | $cacheCreated | Stores the cache creation time. |
|
| protected bool | $cacheInvalidated | Flag that indicates of the cache has been invalidated. |
|
| protected bool | $cacheLoaded | Indicates if the collected cache was already loaded. |
Methods
Constructs a CacheCollector object.
Gets the cache ID.
Returns whether data exists for this key.
Gets value from the cache.
Implements \Drupal\Core\Cache\CacheCollectorInterface::set().
Deletes the element.
Flags an offset value to be written to the persistent cache.
Resolves a cache miss.
Writes a value to the persistent cache immediately.
Normalizes a cache ID in order to comply with database limitations.
Resets the local cache.
Clears the collected cache entry.
Performs destruct operations.
Loads the cache if not already done.
Invalidate the cache.
Details
__construct(string $cid, CacheBackendInterface $cache, LockBackendInterface $lock, array $tags = [])
Constructs a CacheCollector object.
protected string
getCid()
Gets the cache ID.
has(string $key)
Returns whether data exists for this key.
mixed
get(string $key)
Gets value from the cache.
set(string $key, mixed $value)
Implements \Drupal\Core\Cache\CacheCollectorInterface::set().
This is not persisted by default. In practice this means that setting a value will only apply while the object is in scope and will not be written back to the persistent cache. This follows a similar pattern to static vs. persistent caching in procedural code. Extending classes may wish to alter this behavior, for example by adding a call to persist().
delete(string $key)
Deletes the element.
It depends on the specific case and implementation whether this has a permanent effect or if it just affects the current request.
protected
persist(string $key, bool $persist = TRUE)
Flags an offset value to be written to the persistent cache.
abstract protected mixed
resolveCacheMiss(string $key)
Resolves a cache miss.
When an offset is not found in the object, this is treated as a cache miss. This method allows classes using this implementation to look up the actual value and allow it to be cached.
protected
updateCache(bool $lock = TRUE)
Writes a value to the persistent cache immediately.
protected string
normalizeLockName(string $cid)
Normalizes a cache ID in order to comply with database limitations.
reset()
Resets the local cache.
Does not clear the persistent cache.
clear()
Clears the collected cache entry.
destruct()
Performs destruct operations.
protected
lazyLoadCache()
Loads the cache if not already done.
protected
invalidateCache()
Invalidate the cache.