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;
}
- Kiran's blog
- 266 reads













Post new comment