cache_get

5 cache.inc cache_get($key, $table = 'cache')
6 cache-install.inc cache_get($key, $table = 'cache')
6 cache.inc cache_get($cid, $table = 'cache')
7 cache.inc cache_get($cid, $bin = 'cache')

Returns data from the persistent cache.

Data may be stored as either plain text or as serialized data. cache_get will automatically return unserialized objects and arrays.

Parameters

$cid: The cache ID of the data to retrieve.

$bin: The cache bin to store the data in. Valid core values are 'cache_block', 'cache_bootstrap', 'cache_field', 'cache_filter', 'cache_form', 'cache_menu', 'cache_page', 'cache_path', 'cache_update' or 'cache' for the default cache.

Return value

The cache or FALSE on failure.

See also

cache_set()

46 calls to cache_get()

File

includes/cache.inc, line 55
Functions and interfaces for cache handling.

Code

function cache_get($cid, $bin = 'cache') {
  return _cache_get_object($bin)->get($cid);
}

Comments

Return Value

The return value is an object representing the row in the cache table. To get the actual value out, access the data property. The data property is already unserialized.

Example:

<?php
cache_set
($cid, $myObject);
// ...
$cache = cache_get($cid);
$myObject = $cache->data;
?>

$cid Convention

Note that $cid is a string. The convention seems to be to use colons to create a sort of namespacing. So your module may want to do something like $cid = 'mymodule:mything:50';

This function will return

This function will return expired items in Drupal 7, here is the core issue for fixing in D8:

http://drupal.org/node/534092

This is not obvious from these API docs which is why I'm adding this note here.

To me it looks like they just

To me it looks like they just completely removed it for D8 and kept the cache only for backward compatibility. Did this get moved/rewritten to somewhere else in D8 or was it just removed?

Here's an equivalent line in

Here's an equivalent line in D7 and D8 (dev):

In D7 (taken from menu.module):

<?php
$cache
= cache_get($cid, 'cache_menu');
?>

And in D8, which uses cache():

<?php
$cache
= cache('menu')->get($cid);
?>

Cheers,

Albert.

Login or register to post comments