function cache_get in Memcache API and Integration 5.2
Same name in this branch
- 5.2 memcache.db.inc \cache_get()
- 5.2 memcache.inc \cache_get()
Same name and namespace in other branches
- 5 memcache.db.inc \cache_get()
- 5 memcache.inc \cache_get()
- 6 memcache.db.inc \cache_get()
- 6 memcache.inc \cache_get()
Return data from the persistent cache.
Parameters
$key: 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.
File
- ./
memcache.db.inc, line 16
Code
function cache_get($key, $table = 'cache') {
global $user;
// Garbage collection necessary when enforcing a minimum cache lifetime
$cache_flush = variable_get('cache_flush', 0);
if ($cache_flush && $cache_flush + variable_get('cache_lifetime', 0) <= time()) {
// Time to flush old cache data
db_query("DELETE FROM {%s} WHERE expire != %d AND expire <= %d", $table, CACHE_PERMANENT, $cache_flush);
variable_set('cache_flush', 0);
}
// If we have a memcache hit for this, return it.
if ($cache = dmemcache_get($key, $table)) {
return $cache;
}
// Look for a database cache hit.
if ($cache = db_fetch_object(db_query("SELECT data, created, headers, expire, serialized FROM {%s} WHERE cid = '%s'", $table, $key))) {
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 ($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);
}
}
}
}
// By calling cache_set with an extra paramater to signify no db storage,
// we can lazy instantiate memcache that just comes online.
cache_set($key, $table, $cache->data, $cache->expire, $cache->headers, FALSE);
return $cache;
}
return 0;
}