1 cache.inc cache_set($cid, $data, $bin = 'cache', $expire = CACHE_PERMANENT)

Stores 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. The other bins are expired automatically by core. Contributed modules can add additional bins and get them expired automatically by implementing hook_flush_caches().

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.


string $cid: The cache ID of the data to store.

mixed $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 are not serialized.

string $bin: The cache bin in which data should be stored. Valid core values are "cache", "bootstrap", "field", "filter", "form", "menu", "page", "path", "update", "views", and "views_data". Bin names may include the prefix of "cache_", but it is stripped out before execution

int $expire: (optional) Controls the maximum lifetime of this cache entry. Note that caches might be subject to clearing at any time, so this setting does not guarantee a minimum lifetime. With this in mind, the cache should not be used for data that must be kept during a cache clear, like sessions.

Use 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.

See also



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


function cache_set($cid, $data, $bin = 'cache', $expire = CACHE_PERMANENT) {
  return cache($bin)->set($cid, $data, $expire);