cache_get

includes/cache.inc, line 13

Versions
4.6 – 4.7
cache_get($key)
5
cache_get($key, $table = 'cache')
6
cache_get($cid, $table = 'cache')
7
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.

Parameters

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

▾ 11 functions call cache_get()

block_list in modules/block/block.module
Return all blocks in the specified region for the current user.
book_menu_subtree_data in modules/book/book.module
Get the data representing a subtree of the book hierarchy.
check_markup in modules/filter/filter.module
Run all the enabled filters on a piece of text.
drupal_get_schema in includes/common.inc
Get the schema definition of a table, or the whole database schema.
form_get_cache in includes/form.inc
Fetch a form from cache.
locale in modules/locale/locale.module
Provides interface translation services.
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.
variable_init in includes/bootstrap.inc
Load the persistent variable table.
_theme_load_registry in includes/theme.inc
Get the theme_registry cache from the database; if it doesn't exist, build it.

Code

<?php
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);
      }
    }
    // If enforcing a minimum cache lifetime, validate that the data is
    // currently valid for this user before we return it by making sure the
    // cache entry was created before the timestamp in the current session's
    // cache timer. The cache variable is loaded into the $user object by
    // sess_read() in session.inc.
    else {
      if ($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;
}
?>
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.