Drupal 8 Cache API examples cheat sheet
Here are some random useful snippets for dealing with caches in Drupal 8, just because I keep having to dig them up from the API.
I'll try to add more here as I go.
Set an expiring cache item
\<span class="token package">Drupal</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token function">cache</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">set</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'cache_key'</span><span class="token punctuation">,</span> <span class="token single-quoted-string string">'cache_data'</span><span class="token punctuation">,</span> <span class="token variable">$expiration_timestamp</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
Set a permanent cache item
\<span class="token package">Drupal</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token function">cache</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">set</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'cache_key'</span><span class="token punctuation">,</span> <span class="token single-quoted-string string">'cache_data'</span><span class="token punctuation">,</span> CacheBackendInterface<span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token constant">CACHE_PERMANENT</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
Set a permanent cache item with tags
\<span class="token package">Drupal</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token function">cache</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">set</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'cache_key'</span><span class="token punctuation">,</span> <span class="token single-quoted-string string">'cache_data'</span><span class="token punctuation">,</span> CacheBackendInterface<span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token constant">CACHE_PERMANENT</span><span class="token punctuation">,</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'tag_one'</span><span class="token punctuation">,</span> <span class="token single-quoted-string string">'second_tag'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
Fetch an item from the cache
<span class="token variable">$cache</span> <span class="token operator">=</span> \<span class="token package">Drupal</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token function">cache</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'cache_key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token function">empty</span><span class="token punctuation">(</span><span class="token variable">$cache</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">data</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// Do something with $cache->data here.</span><span class="token punctuation">}</span>
(Note that in Drupal 8 you don't have to manually check to make sure the cache isn't expired, thanks to this issue)
Invalidate a cache item
\<span class="token package">Drupal</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token function">cache</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">invalidate</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'cache_key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
Invalidate multiple cache items
\<span class="token package">Drupal</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token function">cache</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">invalidateMultiple</span><span class="token punctuation">(</span><span class="token variable">$array_of_cache_ids</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
Invalidate specific cache tags
This one allows you to pass in an array of cache tags to invalidate manually.
<span class="token keyword">use</span> <span class="token package">Drupal<span class="token punctuation">\</span>Core<span class="token punctuation">\</span>Cache<span class="token punctuation">\</span>Cache</span><span class="token punctuation">;</span>Cache<span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token function">invalidateTags</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token single-quoted-string string">'config:block.block.YOURBLOCKID'</span><span class="token punctuation">,</span> <span class="token single-quoted-string string">'config:YOURMODULE.YOURCONFIG'</span><span class="token punctuation">,</span> <span class="token single-quoted-string string">'node:YOURNID'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
Note that the invalidation functions also exist for deleting caches, by just replacing invalidate
with delete
.
Flush the entire site cache
This one is still the same as Drupal 7.
<span class="token function">drupal_flush_all_caches</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
The end!