Cache chain

BackendChain defines a chained cache implementation for combining multiple cache backends.

Multiple Backends

All cache implementations have to implement CacheBackendInterface. DatabaseBackend 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.


The new cache API introduces a new way to “remove” a cache item. In addition to the existing delete methods where cache entries are permanently deleted, cache entries can now also be invalidated.

Delete methods
delete(), deleteMultiple(), deleteTags(), deleteAll(), deleteExpired()
Invalidate methods
invalidate(), invalidateMultiple(), invalidateTags(), invalidateAll()

Invalidation is like a “soft delete” where items are not deleted but only marked as invalid, meaning “not fresh” or “not fresh enough”. An item automatically becomes invalid when the expire time specified in the set() call is reached, or it may explicitly be marked as invalid using the functions listed above. The invalid items are not returned by a normal get() call, so in most ways they act as they have been deleted. However, by passing TRUE as the second argument for get(), it is possible to fetch the invalid value.

// Explicit invalidation
cache()->set('foo', 123);
cache()->get('foo'); // Returns a cache item
cache()->get('foo'); // Returns FALSE
cache()->get('foo', TRUE); // Returns a cache item
cache()->get('bar', TRUE); // Returns FALSE

// Expired items
cache()->set('bar', 123, REQUEST_TIME + 60);
cache()->get('bar'); // Returns a cache item

Cache tags

A previous, temporary implementation that implemented prefix/wildcard cache clears in Drupal 8 (deletePrefix()) has since been removed in favor of the new tag invalidation functionality.


Cleaner API

The new API removes cache_clear_all(), and its confusing, overloaded arguments. Deleting items from cache, expiring them or flushing the entire cache bin are now separate methods opposed to the previous, all-in-one clear() method.

Cache API in Drupal 8

The Cache API is much improved in Drupal 8. The following sections go into more detail on each feature.

For a brief run-down, see also the Cache API page from the API documentation.


Subscribe with RSS Subscribe to RSS - Drupal 8.x