Cache API in DRUPAL

0
Your rating: None

There are two major function in DRUPAL for Cache API.

1. cache_set().
2. cache_get().

Function signature for cache_set() is:

cache_set($cid, $table = 'cache', $data, $expire = CACHE_PERMANENT, $headers = NULL)

The function parameters are:

• $cid: A unique cache ID string that acts as a key to the data.
• $table: The name of the table to store the data in. You can create your own table or use cache, cache_filter, cache_menu, or cache_page. The cache table is used by default.
• $data: The data to store in the cache. Remember that complex PHP data types must be serialized first.
• $expire: The length of time for which the cached data is valid. Possible values are CACHE_PERMANENT, CACHE_TEMPORARY, or a Unix timestamp.
• $headers: For cached pages, a string of HTTP headers to pass along to the browser.

Here is an example for cache_set() which can be seen in filter.module.

// Store in cache with a minimum expiration time of 1 day.
if ($cache) {
  cache_set($cid, 'cache_filter', $text, time() + (60 * 60 * 24));
}

Function signature for cache_get() is:

cache_get($cid, $table = 'cache')

The function parameters are:

• $cid: The cache ID of the data to retrieve.
• $table: The name of the table from which to retrieve the data. This might be a table you created or one of the tables provided by Drupal: cache, cache_filter, cache_menu, or cache_page. The cache table is used by default.

Here is an example for cache_get() which can be seen in filter.module.

// Check for a cached version of this piece of text.
if ($cached = cache_get($cid, 'cache_filter')) {
  return $cached->data;
}

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.

Cumulus Tag Cloud

Kiran Says

I love work environment which is:

Informal yet professional
Demanding yet rewarding
Challenging yet inspiring
Mediocrity is not an option
Having fun is serious business
Making mistakes is human
Forgiveness is Company Policy

Travelling Sucks