Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Drupal 8 Cache API

By editor , 25 October, 2016
Question

How to use the Drupal 8 Cache API ?

Get the default cache bin

$cache = \Drupal::cache();

To get a particular cache bin (here 'render'):

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

Save / Cache itmes
Example:
$my_object = 'A stdClass';
Drupal::cache()->set('my_cache_key', $my_object, CacheBackendInterface::CACHE_PERMANENT, array('my_first_tag', 'my_second_tag'));


Read a cached item

$cache = \Drupal::cache()->get('my_cache_key');

Cache remove and invalidation

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

And also using cache tags

Example: To invalidate cach with tags 'node:5' and/or 'my_first_tag'
\Drupal\Core\Cache\Cache::invalidateTags(array('node:5', 'my_first_tag'));

Delete Cache (by CID)

$cids = ['cid1', 'cid2',];
\Drupal::cache()->delete($cids);

 

Cache contex

https://www.drupal.org/docs/8/api/cache-api/cache-contexts

Tags

  • Drupal 8
  • Code
  • Cache
Cache Drupal 8 Render Arrays
Cache Clearing and rebuilding methods for Drupal 8
Drupal 8 Tags based cache system
Drupal 8 Cache contexts
Disable cache for a specific page
Memcache - Install memcached on linux and Drupal.
Redis and Php-Redis with drupal 9 on Linux

Comments

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer