ChainedFastBackend
class ChainedFastBackend implements CacheBackendInterface, CacheTagsInvalidatorInterface (View source)
Defines a backend with a fast and a consistent backend chain.
In order to mitigate a network roundtrip for each cache get operation, this cache allows a fast backend to be put in front of a slow(er) backend. Typically the fast backend will be something like APCu, and be bound to a single web node, and will not require a network round trip to fetch a cache item. The fast backend will also typically be inconsistent (will only see changes from one web node). The slower backend will be something like Mysql, Memcached or Redis, and will be used by all web nodes, thus making it consistent, but also require a network round trip for each cache get.
In addition to being useful for sites running on multiple web nodes, this backend can also be useful for sites running on a single web node where the fast backend (e.g., APCu) isn't shareable between the web and CLI processes. Single-node configurations that don't have that limitation can just use the fast cache backend directly.
We always use the fast backend when reading (get()) entries from cache, but check whether they were created before the last write (set()) to this (chained) cache backend. Those cache entries that were created before the last write are discarded, but we use their cache IDs to then read them from the consistent (slower) cache backend instead; at the same time we update the fast cache backend so that the next read will hit the faster backend again. Hence we can guarantee that the cache entries we return are all up-to-date, and maximally exploit the faster cache backend. This cache backend uses and maintains a "last write timestamp" to determine which cache entries should be discarded.
Because this backend will mark all the cache entries in a bin as out-dated for each write to a bin, it is best suited to bins with fewer changes.
Note that this is designed specifically for combining a fast inconsistent cache backend with a slower consistent cache back-end. To still function correctly, it needs to do a consistency check (see the "last write timestamp" logic). This contrasts with \Drupal\Core\Cache\BackendChain, which assumes both chained cache backends are consistent, thus a consistency check being pointless.
Constants
| LAST_WRITE_TIMESTAMP_PREFIX |
Cache key prefix for the bin-specific entry to track the last write. |
Properties
| protected string | $bin | ||
| protected CacheBackendInterface | $consistentBackend | The consistent cache backend. |
|
| protected CacheBackendInterface | $fastBackend | The fast cache backend. |
|
| protected float | $lastWriteTimestamp | The time at which the last write to this cache bin happened. |
Methods
Constructs a ChainedFastBackend object.
Returns data from the persistent cache.
Returns data from the persistent cache when given an array of cache IDs.
Stores data in the persistent cache.
Store multiple items in the persistent cache.
Deletes an item from the cache.
Deletes multiple items from the cache.
Deletes all cache items in a bin.
Marks a cache item as invalid.
Marks cache items as invalid.
Marks cache items with any of the specified tags as invalid.
Marks all cache items as invalid.
Performs garbage collection on a cache bin.
Remove a cache bin.
No description
Gets the last write timestamp.
Marks the fast cache bin as outdated because of a write.
Details
__construct(CacheBackendInterface $consistent_backend, CacheBackendInterface $fast_backend, string $bin)
Constructs a ChainedFastBackend object.
object|false
get(string $cid, bool $allow_invalid = FALSE)
Returns data from the persistent cache.
array
getMultiple(array $cids, bool $allow_invalid = FALSE)
Returns data from the persistent cache when given an array of cache IDs.
set(string $cid, mixed $data, int $expire = Cache::PERMANENT, array $tags = [])
Stores data in the persistent cache.
Core cache implementations set the created time on cache item with microtime(TRUE) rather than REQUEST_TIME_FLOAT, because the created time of cache items should match when they are created, not when the request started. Apart from being more accurate, this increases the chance an item will legitimately be considered valid.
setMultiple(array $items)
Store multiple items in the persistent cache.
delete(string $cid)
Deletes an item from the cache.
If the cache item is being deleted because it is no longer "fresh", you may consider using invalidate() instead. This allows callers to retrieve the invalid item by calling get() with $allow_invalid set to TRUE. In some cases an invalid item may be acceptable rather than having to rebuild the cache.
deleteMultiple(array $cids)
Deletes multiple items from the cache.
If the cache items are being deleted because they are no longer "fresh", you may consider using invalidateMultiple() instead. This allows callers to retrieve the invalid items by calling get() with $allow_invalid set to TRUE. In some cases an invalid item may be acceptable rather than having to rebuild the cache.
deleteAll()
Deletes all cache items in a bin.
invalidate(string $cid)
Marks a cache item as invalid.
Invalid items may be returned in later calls to get(), if the $allow_invalid argument is TRUE.
invalidateMultiple(array $cids)
Marks cache items as invalid.
Invalid items may be returned in later calls to get(), if the $allow_invalid argument is TRUE.
invalidateTags(array $tags)
Marks cache items with any of the specified tags as invalid.
invalidateAll()
Marks all cache items as invalid.
Invalid items may be returned in later calls to get(), if the $allow_invalid argument is TRUE.
garbageCollection()
Performs garbage collection on a cache bin.
The backend may choose to delete expired or invalidated items.
removeBin()
Remove a cache bin.
reset()
No description
protected
getLastWriteTimestamp()
Gets the last write timestamp.
protected
markAsOutdated()
Marks the fast cache bin as outdated because of a write.