interface DrupalCacheInterface

You are here

Defines an 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:

 variable_set('cache_class_cache_page', 'MyCustomCache');

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.

 variable_set('cache_default_class', 'MyCustomCache');

To implement a completely custom cache bin, use the same variable format:

 variable_set('cache_class_custom_bin', 'MyCustomCache');

To access your custom cache bin, specify the name of the bin when storing or retrieving cached data:

 cache_set($cid, $data, 'custom_bin', $expire);
 cache_get($cid, 'custom_bin');

See also

_cache_get_object()

DrupalDatabaseCache

Implemented by

Hierarchy

Expanded class hierarchy of DrupalCacheInterface

Members

Contains filters are case sensitive
Namesort descending Modifiers Type Description
DrupalCacheInterface::clear function Expires data from the cache.
DrupalCacheInterface::get function Returns data from the persistent cache.
DrupalCacheInterface::getMultiple function Returns data from the persistent cache when given an array of cache IDs.
DrupalCacheInterface::isEmpty function Checks if a cache bin is empty.
DrupalCacheInterface::set function Stores data in the persistent cache.

File

includes/cache.inc, line 221
Functions and interfaces for cache handling.

View source
interface DrupalCacheInterface {

  /**
   * Returns 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.
   *
   * @return
   *   The cache or FALSE on failure.
   */
  function get($cid);

  /**
   * Returns 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.
   *
   * @return
   *   An array of the items successfully returned from cache indexed by cid.
   */
  function getMultiple(&$cids);

  /**
   * Stores data in the persistent 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 $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 set($cid, $data, $expire = CACHE_PERMANENT);


  /**
   * Expires 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 or an array of cache IDs. Otherwise, all cache
   *   entries that can expire are deleted. The $wildcard argument will be
   *   ignored if set to NULL.
   * @param $wildcard
   *   If TRUE, the $cid argument must contain a string value and cache IDs
   *   starting with $cid are deleted in addition to the exact cache ID
   *   specified by $cid. If $wildcard is TRUE and $cid is '*', the entire
   *   cache is emptied.
   */
  function clear($cid = NULL, $wildcard = FALSE);

  /**
   * Checks if a cache bin is empty.
   *
   * A cache bin is considered empty if it does not contain any valid data for
   * any cache ID.
   *
   * @return
   *   TRUE if the cache bin specified is empty.
   */
  function isEmpty();
}

Comments

i want to override drupal default caching for this i implemented DrupalCacheInterface in MyCustomCacheClass in mycustommodule.inc and in mycustommodule.install i added this code

function mycustommodule_install() {
  variable_set('cache_class_cache_page', 'MyCustomCacheClass' );
}

function mycustommodule_uninstall() {
    variable_del('cache_class_cache_page');
 
}

but install and uninstall hook are not called so i added cache_class_cache_page variable manually in variable table whose's value is MyCustomCacheClass and checked "MyCustomCacheClass" exists or not using class_exists() function. Its output 1 means that class exists and loaded by drupal. Everything work for admin but for anonymouse user giving http 330 error in chrome while in firfox its showing http 200 code in header but showing blank page. Where did i made mistake. I just copied DrupalDatabaseCache class from cache.inc and renamed it nothing else changed inside class.

its Content Encoding Error when i implement DrupalCacheInterface How to resolve this issue