| 1 |
<?php
|
| 2 |
// $Id: memcache.inc,v 1.27 2008/01/29 23:43:34 slantview Exp $
|
| 3 |
|
| 4 |
require_once 'dmemcache.inc';
|
| 5 |
|
| 6 |
/** Implementation of cache.inc with memcache logic included **/
|
| 7 |
|
| 8 |
/**
|
| 9 |
* Return data from the persistent cache.
|
| 10 |
*
|
| 11 |
* @param $key
|
| 12 |
* The cache ID of the data to retrieve.
|
| 13 |
* @param $table
|
| 14 |
* The table $table to store the data in. Valid core values are 'cache_filter',
|
| 15 |
* 'cache_menu', 'cache_page', or 'cache' for the default cache.
|
| 16 |
*/
|
| 17 |
function cache_get($key, $table = 'cache') {
|
| 18 |
return dmemcache_get($key, $table);
|
| 19 |
}
|
| 20 |
|
| 21 |
/**
|
| 22 |
* Store data in memcache.
|
| 23 |
*
|
| 24 |
* @param $cid
|
| 25 |
* The cache ID of the data to store.
|
| 26 |
* @param $table
|
| 27 |
* The table $table to store the data in. Valid core values are 'cache_filter',
|
| 28 |
* 'cache_menu', 'cache_page', or 'cache'.
|
| 29 |
* @param $data
|
| 30 |
* The data to store in the cache. Complex data types must be serialized first.
|
| 31 |
* @param $expire
|
| 32 |
* One of the following values:
|
| 33 |
* - CACHE_PERMANENT: Indicates that the item should never be removed unless
|
| 34 |
* explicitly told to using cache_clear_all() with a cache ID.
|
| 35 |
* - CACHE_TEMPORARY: Indicates that the item should be removed at the next
|
| 36 |
* general cache wipe.
|
| 37 |
* - A Unix timestamp: Indicates that the item should be kept at least until
|
| 38 |
* the given time, after which it behaves like CACHE_TEMPORARY.
|
| 39 |
* @param $headers
|
| 40 |
* A string containing HTTP header information for cached pages.
|
| 41 |
* @param $db_storage
|
| 42 |
* This boolean is unique to the memcache.inc implementation of cache set.
|
| 43 |
* It allows us to do a cache_set and not write to the database, but only
|
| 44 |
* to memcache.
|
| 45 |
*/
|
| 46 |
function cache_set($cid, $table = 'cache', $data, $expire = CACHE_PERMANENT, $headers = NULL) {
|
| 47 |
$time = time();
|
| 48 |
|
| 49 |
// Create new cache object.
|
| 50 |
$cache = new stdClass;
|
| 51 |
$cache->cid = $cid;
|
| 52 |
$cache->data = is_object($data) ? memcache_clone($data) : $data;
|
| 53 |
$cache->created = $time;
|
| 54 |
$cache->expire = $expire;
|
| 55 |
$cache->headers = $headers;
|
| 56 |
|
| 57 |
// Save to memcache
|
| 58 |
if ($expire == CACHE_TEMPORARY) {
|
| 59 |
$expire = variable_get('cache_lifetime', 2591999);
|
| 60 |
}
|
| 61 |
dmemcache_set($cid, $cache, $expire, $table);
|
| 62 |
}
|
| 63 |
|
| 64 |
/**
|
| 65 |
*
|
| 66 |
* Expire data from the cache. If called without arguments, expirable
|
| 67 |
* entries will be cleared from the cache_page table.
|
| 68 |
*
|
| 69 |
* Memcache logic is simpler than the core cache because memcache doesn't have
|
| 70 |
* a minimum cache lifetime consideration (it handles it internally), and
|
| 71 |
* doesn't support wildcards. Wildcard flushes result in the entire table
|
| 72 |
* being flushed.
|
| 73 |
*
|
| 74 |
* @param $cid
|
| 75 |
* If set, the cache ID to delete. Otherwise, all cache entries that can
|
| 76 |
* expire are deleted from the specified table.
|
| 77 |
*
|
| 78 |
* @param $table
|
| 79 |
* If set, the table to delete from.
|
| 80 |
*
|
| 81 |
* @param $wildcard
|
| 82 |
* If set to TRUE, the $cid is treated as a substring
|
| 83 |
* to match rather than a complete ID. The match is a right hand
|
| 84 |
* match. If '*' is given as $cid, the table $table will be emptied.
|
| 85 |
*/
|
| 86 |
function cache_clear_all($cid = NULL, $table = NULL, $wildcard = FALSE) {
|
| 87 |
// If cid and table are not set, we should flush the cache_page table.
|
| 88 |
if (!isset($cid) && !isset($table)) {
|
| 89 |
$cid = '*';
|
| 90 |
$wildcard = TRUE;
|
| 91 |
$table = 'cache_page';
|
| 92 |
}
|
| 93 |
if (empty($cid) || ($cid == '*' && $wildcard !== TRUE)) {
|
| 94 |
// don't do anything if cid is unset. this matches the default drupal behavior...
|
| 95 |
if ($wildcard && $cid != '*') {
|
| 96 |
if (variable_get('memcache_debug', FALSE)) {
|
| 97 |
// call watchdog, since you probably didn't want to flush the entire bin.
|
| 98 |
watchdog('memcache', "illegal wildcard in cache_clear_all - not flushing entire bin. table: $table. cid: $cid", WATCHDOG_WARNING);
|
| 99 |
}
|
| 100 |
}
|
| 101 |
}
|
| 102 |
else if ($cid == '*' || $wildcard === TRUE) {
|
| 103 |
dmemcache_flush($table);
|
| 104 |
}
|
| 105 |
else {
|
| 106 |
dmemcache_delete($cid, $table);
|
| 107 |
}
|
| 108 |
}
|
| 109 |
|
| 110 |
/**
|
| 111 |
* Provide a substitute clone() function for PHP4. This is a copy of drupal_clone
|
| 112 |
* because common.inc isn't included early enough in the bootstrap process to
|
| 113 |
* be able to depend on drupal_clone.
|
| 114 |
*/
|
| 115 |
function memcache_clone($object) {
|
| 116 |
return version_compare(phpversion(), '5.0') < 0 ? $object : clone($object);
|
| 117 |
}
|