DatabaseBackend
class DatabaseBackend implements CacheBackendInterface (View source)
Defines a default cache implementation.
This is Drupal's default cache implementation. It uses the database to store cached data. Each cache bin corresponds to a database table by the same name.
Constants
| DEFAULT_MAX_ROWS |
The default maximum number of rows that this cache bin table can store. This maximum is introduced to ensure that the database is not filled with hundred of thousand of cache entries with gigabytes in size. Read about how to change it in the @link cache Cache API topic. @endlink |
| MAXIMUM_NONE |
-1 means infinite allows numbers of rows for the cache backend. |
Properties
| protected int | $maxRows | The maximum number of rows that this cache bin table is allowed to store. |
|
| protected string | $bin | ||
| protected Connection | $connection | The database connection. |
|
| protected CacheTagsChecksumInterface | $checksumProvider | The cache tags checksum provider. |
Methods
Constructs a DatabaseBackend object.
Returns data from the persistent cache.
Returns data from the persistent cache when given an array of cache IDs.
Prepares a cached item.
Stores data in the persistent cache.
Store multiple items in the persistent cache.
Stores 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 all cache items as invalid.
Performs garbage collection on a cache bin.
Remove a cache bin.
Check if the cache bin exists and create it if not.
Act on an exception when cache might be stale.
Normalizes a cache ID in order to comply with database limitations.
Defines the schema for the {cache_*} bin tables.
The maximum number of rows that this cache bin table is allowed to store.
Details
__construct(Connection $connection, CacheTagsChecksumInterface $checksum_provider, string $bin, int $max_rows = NULL)
Constructs a DatabaseBackend 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.
protected mixed|false
prepareItem(object $cache, bool $allow_invalid)
Prepares a cached item.
Checks that items are either permanent or did not expire, and unserializes data as appropriate.
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.
protected
doSetMultiple(array $items)
Stores 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.
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.
protected
ensureBinExists()
Check if the cache bin exists and create it if not.
protected
catchException(Exception $e, string|null $table_name = NULL)
Act on an exception when cache might be stale.
If the table does not yet exist, that's fine, but if the table exists and yet the query failed, then the cache is stale and the exception needs to propagate.
protected string
normalizeCid(string $cid)
Normalizes a cache ID in order to comply with database limitations.
schemaDefinition()
| internal |
Defines the schema for the {cache_*} bin tables.
int
getMaxRows()
The maximum number of rows that this cache bin table is allowed to store.