DrupalCacheInterface

  1. drupal
    1. 7 includes/cache.inc

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

Functions & methods

NameDescription
DrupalCacheInterface::clearExpires data from the cache.
DrupalCacheInterface::getReturns data from the persistent cache.
DrupalCacheInterface::getMultipleReturns data from the persistent cache when given an array of cache IDs.
DrupalCacheInterface::isEmptyChecks if a cache bin is empty.
DrupalCacheInterface::setStores data in the persistent cache.
DrupalCacheInterface::__constructConstructs a new cache interface.

File

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

View source
interface DrupalCacheInterface {
  /**
   * Constructs a new cache interface.
   *
   * @param $bin
   *   The cache bin for which the object is created.
   */
  function __construct($bin);

  /**
   * 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 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.
   */
  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();
}
Login or register to post comments