n How to use Cache API in Drupal 8 | CodimTh

Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
Category:

In this tuto, I'll show you how to use Cache API  in your custom module in Drupal 8.

  • Get the default cache bin

Cache storage is separated into "bins", each containing various cache items.

$cache = \Drupal::cache();
  • Get a particular cache bin
$render_cache = \Drupal::cache('BIN_NAME');

// Example

$render_cache = \Drupal::cache('render');

List of the cache bins are the following:

  • bootstrap: Data needed from the beginning to the end of most requests, that has a very strict limit on variations and is invalidated rarely.
  • render: Contains cached HTML strings like cached pages and blocks, can grow to large size.
  • data: Contains data that can vary by path or similar context.
  • discovery: Contains cached discovery data for things such as plugins, views_data, or YAML discovered data such as library info.

 

  •  Storing cache items

Example:

$tags = array(
  'my_custom_tag',
  'node:1',
  'node:3',
  'user:7',
);
\Drupal::cache()->set($cacheID, $data, CacheBackendInterface::CACHE_PERMANENT, $tags);

set() method contains 4 arguments:

  • $cache ID: string by which we can later reference the item.
  • $data: value such as a string, array or object you want to save it.
  • the expiration time: a timestamp in the future when this cache item will automatically become invalid or -1 which basically means this item never expires. It is best practice to use the Drupal\Core\Cache\CacheBackendInterface::CACHE_PERMANENT constant to represent this value.
  • $tags: array of cache tags this item can be later identified by.

 

  • Retrieving cached items
$cache = \Drupal::cache()->get('cacheID');
$data = $cache->data;
  • Cache invalidation and removal

You can remove cache using following methods:
invalidate(), invalidateMultiple(), invalidateAll(), delete(), deleteMultiple(), invalidateTags() or deleteAll();

Example: To invalidate cach with tags.

$tags = array(
  'my_custom_tag',
  'node:1',
  'node:3',
  'user:7',
);

\Drupal\Core\Cache\Cache::invalidateTags($tags);

Example:  Delete Cache by cacheID.

  $cacheIDs = ['cid1', 'cid2',];
\Drupal::cache()->delete($cacheIDs);
  • Get Cache Tags

If you want to associate the cache tags of entities, or entity listings. You won't have to manually construct cache tags for them — just get their cache tags via:

Example How to use Cache API to store data:

$cid = 'mymodule_example:' . \Drupal::languageManager()->getCurrentLanguage()->getId();
$data = NULL;
if ($cache = \Drupal::cache()->get($cid)) {
  $data = $cache->data;
}
else {
  $data = my_module_complicated_calculation();
  \Drupal::cache()->set($cid, $data);
}

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook