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.

Invalidation

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.

<?php
// Explicit invalidation
cache()->set('foo', 123);
cache()->get('foo'); // Returns a cache item
cache()->invalidate('foo');
cache()->get('foo'); // Returns FALSE
cache()->get('foo', TRUE); // Returns a cache item
cache()->garbageCollection();
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.

Comparison:

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.

Cache API

The Cache API provides a generic mechanism for modules to store objects in bins for performance gains. Cached data can be simple permanent information like your site name, or more complex dynamic objects like entity data or block data.

The Cache API implementations differ between versions of Drupal.

Pages

Subscribe with RSS Subscribe to RSS - Drupal 8.x