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

Return 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.


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

$table: The table $table to store the data in. Valid core values are 'cache_filter', 'cache_menu', 'cache_page', or 'cache' for the default cache.

@see cache_set()

5 calls to cache_get()
book_menu_subtree_data in modules/book/book.module
Get the data representing a subtree of the book hierarchy.
menu_tree_all_data in includes/menu.inc
Get the data structure representing a named menu tree.
menu_tree_page_data in includes/menu.inc
Get the data structure representing a named menu tree, based on the current page.
page_get_cache in includes/bootstrap.inc
Retrieve the current page from the cache.
_theme_load_registry in includes/theme.inc
Get the theme_registry cache from the database; if it doesn't exist, build it.


includes/cache.inc, line 15


function cache_get($cid, $table = 'cache') {
  global $user;

  // Garbage collection necessary when enforcing a minimum cache lifetime
  $cache_flush = variable_get('cache_flush_' . $table, 0);
  if ($cache_flush && $cache_flush + variable_get('cache_lifetime', 0) <= time()) {

    // Reset the variable immediately to prevent a meltdown in heavy load situations.
    variable_set('cache_flush_' . $table, 0);

    // Time to flush old cache data
    db_query("DELETE FROM {" . $table . "} WHERE expire != %d AND expire <= %d", CACHE_PERMANENT, $cache_flush);
  $cache = db_fetch_object(db_query("SELECT data, created, headers, expire, serialized FROM {" . $table . "} WHERE cid = '%s'", $cid));
  if (isset($cache->data)) {

    // If the data is permanent or we're not enforcing a minimum cache lifetime
    // always return the cached data.
    if ($cache->expire == CACHE_PERMANENT || !variable_get('cache_lifetime', 0)) {
      $cache->data = db_decode_blob($cache->data);
      if ($cache->serialized) {
        $cache->data = unserialize($cache->data);
    else {
      if (isset($user->cache) && $user->cache > $cache->created) {

        // This cache data is too old and thus not valid for us, ignore it.
        return 0;
      else {
        $cache->data = db_decode_blob($cache->data);
        if ($cache->serialized) {
          $cache->data = unserialize($cache->data);
    return $cache;
  return 0;


colemanw’s picture

It should be noted that the return value is an object.

Garrett Albright’s picture

…unless the cached item is not found; in which case the return value is an integer (0).

EvanDonovan’s picture

If you just want the data, here's a little helper function I wrote:

/* Wraps cache_get to return just the data. */
function cache_get_data($key, $table) {
  $cache_obj = cache_get($key, $table);
  if(is_object($cache_obj) && isset($cache_obj->data)) {
    return $cache_obj->data;
  else {
    return 0;

Note that this function be extended to check expiration manually, as per the below comments, if needed.

uibi’s picture

cache_get() returns expired cache items so you have to manually check wether the returned item is still valid:

if (($cache = cache_get('my_module:my_function', 'cache'))
      && !empty($cache->data)
      && (time() < $cache->expire)) //check expiration manually
    //use $cache->data
    //do expensive processing and store result in the cache

Some links for further references:

armanschwarz’s picture

Newbie here. uibi: it was my understanding that once a cache object has expired, it will be cleared from the cache automatically along with all other temporary cache objects, ie. whenever drupal feels like it. Is this correct?

Fabianx’s picture

That is correct, but the cache is only cleared on cron runs or manual cache clearing. This is normally called garbage collection and garbage collection most often is an asynchronous process to allow for good performance.

So it can happen that the object is still in the cache, even though it is expired already.

The above code helps to workaround that problem by checking the expire timestamp.

Hope that helps.

Best Wishes,