diff --git a/includes/cache.inc b/includes/cache.inc index 9b60a7e..9c87d11 100644 --- a/includes/cache.inc +++ b/includes/cache.inc @@ -4,10 +4,28 @@ * Factory for instantiating and statically caching the correct class for a cache bin. * * By default, this returns an instance of the DrupalDatabaseCache class. - * Classes implementing DrupalCacheInterface can register themselves both as a - * default implementation and for specific bins. + * Classes implementing DrupalCacheInterface can be used instead, either + * as a default for all cache requests, or for specific bins. * - * @see DrupalCacheInterface + * Configuration of cache bins should be done in settings.php + * + * For example to use 'MyCustomCache' for the''page' bin. Set the following + * variables: + * + * @code + * $conf['cache_backends'] = array( + * 'DrupalDatabaseCache' => 'includes/cache.inc', + * 'MyCustomCache' => 'sites/all/modules/mycustomcache/MyCustomCache.inc', + * ); + * $conf['cache_class_page'] = 'MyCustomCache', + * @endcode + * + * Additionally, you can register your cache implementation to be used by + * default for all cache bins by setting the variable 'cache_default_class' to + * the name of your implementation of the DrupalCacheInterface, e.g. + * @code + * $conf['cache_default_class'] = 'MyCustomCache'; + * @endcode * * @param $bin * The cache bin for which the cache object should be returned, defaults to @@ -16,10 +34,6 @@ * The cache object associated with the specified bin. */ function cache($bin = 'cache') { - // Temporary backwards compatibiltiy layer, allow old style prefixed cache - // bin names to be passed as arguments. - $bin = str_replace('cache_', '', $bin); - // We do not use drupal_static() here because we do not want to change the // storage of a cache bin mid-request. static $cache_objects; @@ -34,195 +48,12 @@ function cache($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. - * - * @param $cid - * The cache ID of the data to retrieve. - * @param $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 - * The cache or FALSE on failure. - */ -function cache_get($cid, $bin = 'cache') { - return cache($bin)->get($cid); -} - -/** - * Return data from the persistent cache when given an array of cache IDs. - * - * @param $cids - * An array of cache IDs for the data to retrieve. This is passed by - * reference, and will have the IDs successfully returned from cache removed. - * @param $bin - * The cache bin where the data is stored. - * @return - * An array of the items successfully returned from cache indexed by cid. - */ -function cache_get_multiple(array &$cids, $bin = 'cache') { - return cache($bin)->getMultiple($cids); -} - -/** - * Store data in the persistent cache. - * - * The persistent cache is split up into several cache bins. In the default - * cache implementation, each cache bin corresponds to a database table by the - * same name. Other implementations might want to store several bins in data - * structures that get flushed together. While it is not a problem for most - * cache bins if the entries in them are flushed before their expire time, some - * might break functionality or are extremely expensive to recalculate. These - * will be marked with a (*). The other bins expired automatically by core. - * Contributed modules can add additional bins and get them expired - * automatically by implementing hook_flush_caches(). - * - * - cache: Generic cache storage bin (used for variables, theme registry, - * locale date, list of simpletest tests etc). - * - * - cache_block: Stores the content of various blocks. - * - * - cache field: Stores the field data belonging to a given object. - * - * - cache_filter: Stores filtered pieces of content. - * - * - cache_form(*): Stores multistep forms. Flushing this bin means that some - * forms displayed to users lose their state and the data already submitted - * to them. - * - * - cache_menu: Stores the structure of visible navigation menus per page. - * - * - cache_page: Stores generated pages for anonymous users. It is flushed - * very often, whenever a page changes, at least for every ode and comment - * submission. This is the only bin affected by the page cache setting on - * the administrator panel. - * - * - cache path: Stores the system paths that have an alias. - * - * - cache update(*): Stores available releases. The update server (for - * example, drupal.org) needs to produce the relevant XML for every project - * installed on the current site. As this is different for (almost) every - * site, it's very expensive to recalculate for the update server. - * - * The reasons for having several bins are as follows: - * - * - smaller bins mean smaller database tables and allow for faster selects and - * inserts - * - we try to put fast changing cache items and rather static ones into - * different bins. The effect is that only the fast changing bins will need a - * lot of writes to disk. The more static bins will also be better cacheable - * with MySQL's query cache. - * - * @param $cid - * The cache ID of the data to store. - * @param $data - * The data to store in the cache. Complex data types will be automatically - * serialized before insertion. - * Strings will be stored as plain text and not serialized. - * @param $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_update' or 'cache' for the default - * cache. - * @param $expire - * One of the following values: - * - CACHE_PERMANENT: Indicates that the item should never be removed unless - * explicitly told to using cache_clear_all() with a cache ID. - * - CACHE_TEMPORARY: Indicates that the item should be removed at the next - * general cache wipe. - * - A Unix timestamp: Indicates that the item should be kept at least until - * the given time, after which it behaves like CACHE_TEMPORARY. - */ -function cache_set($cid, $data, $bin = 'cache', $expire = CACHE_PERMANENT) { - return cache($bin)->set($cid, $data, $expire); -} - -/** - * Expire data from the cache. - * - * If called without arguments, expirable entries will be cleared from the - * cache_page and cache_block bins. - * - * @param $cid - * If set, the cache ID to delete. Otherwise, all cache entries that can - * expire are deleted. - * - * @param $bin - * If set, the bin $bin to delete from. Mandatory - * argument if $cid is set. - * - * @param $wildcard - * If $wildcard is TRUE, cache IDs starting with $cid are deleted in - * addition to the exact cache ID specified by $cid. If $wildcard is - * TRUE and $cid is '*' then the entire bin $bin is emptied. - */ -function cache_clear_all($cid = NULL, $bin = NULL, $wildcard = FALSE) { - if (!isset($cid) && !isset($bin)) { - // Clear the block cache first, so stale data will - // not end up in the page cache. - if (module_exists('block')) { - cache('block')->expire(); - } - cache('page')->expire(); - return; - } - return cache($bin)->clear($cid, $wildcard); -} - -/** - * Check if a cache bin is empty. - * - * A cache bin is considered empty if it does not contain any valid data for any - * cache ID. - * - * @param $bin - * The cache bin to check. - * @return - * TRUE if the cache bin specified is empty. - */ -function cache_is_empty($bin) { - return cache($bin)->isEmpty(); -} - -/** * Interface for cache implementations. * * All cache implementations have to implement this interface. * DrupalDatabaseCache provides the default implementation, which can be * consulted as an example. * - * To make Drupal use your implementation for a certain cache bin, you have to - * set a variable with the name of the cache bin as its key and the name of - * your class as its value. For example, if your implementation of - * DrupalCacheInterface was called MyCustomCache, the following line would make - * Drupal use it for the 'cache_page' bin: - * @code - * variable_set('cache_class_cache_page', 'MyCustomCache'); - * @endcode - * - * Additionally, you can register your cache implementation to be used by - * default for all cache bins by setting the variable 'cache_default_class' to - * the name of your implementation of the DrupalCacheInterface, e.g. - * @code - * variable_set('cache_default_class', 'MyCustomCache'); - * @endcode - * - * To implement a completely custom cache bin, use the same variable format: - * @code - * variable_set('cache_class_custom_bin', 'MyCustomCache'); - * @endcode - * To access your custom cache bin, specify the name of the bin when storing - * or retrieving cached data: - * @code - * cache_set($cid, $data, 'custom_bin', $expire); - * cache_get($cid, 'custom_bin'); - * @endcode - * * @see cache() * @see DrupalDatabaseCache */ @@ -319,23 +150,6 @@ interface DrupalCacheInterface { function garbageCollection(); /** - * Expire data from the cache. If called without arguments, expirable - * entries will be cleared from the cache_page and cache_block bins. - * - * @param $cid - * If set, the cache ID to delete. Otherwise, all cache entries that can - * expire are deleted. - * @param $wildcard - * If set to TRUE, the $cid is treated as a substring - * to match rather than a complete ID. The match is a right hand - * match. If '*' is given as $cid, the bin $bin will be emptied. - * - * @todo: this method is deprecated, as it's functionality is covered by - * more targetted methods in the interface. - */ - function clear($cid = NULL, $wildcard = FALSE); - - /** * Check if a cache bin is empty. * * A cache bin is considered empty if it does not contain any valid data for @@ -538,30 +352,6 @@ class DrupalDatabaseCache implements DrupalCacheInterface { } } - function clear($cid = NULL, $wildcard = FALSE) { - global $user; - - if (empty($cid)) { - $this->expire(); - } - else { - if ($wildcard) { - if ($cid == '*') { - $this->flush(); - } - else { - $this->deletePrefix($cid); - } - } - elseif (is_array($cid)) { - $this->deleteMultiple($cid); - } - else { - $this->delete($cid); - } - } - } - function isEmpty() { $this->garbageCollection(); $query = db_select($this->bin);