http://blog.belvg.com/using-default-magento-cache.html
Magento uses the following cache model: Mage::getModel(‘core/cache’) and 4 main methods:
save($value, $key, $tags = array(), $lifeTime=null);
load($key);
remove($key);
clean($tags = array();
Saving to cache: $key, $tags = array(), $lifeTime=null):
Example: Mage::app()->getCache()->save(json_encode($this->results), ‘mycache_info_’ . $query, array(‘MYCACHE_CACHE’), $lifetime);
Where: $value – the data you need to cache
$key – a unique key, which can provide the cached data. As a rule it consists of a dynamic and static parts.
For example: ‘mycache_info_’ is a statics part; As a dynamic part it is possible to use the search query $query
$tags – tags that are applied for clearing cache
$lifeTime – life time in seconds(刚开始代码里给设置了10, 老是无法取到值, 很郁闷)
Loading from cache: load( $key)
Example: Mage::app()->getCache()->load(‘mycache_info_’ . $query);
Where: $query: a dynamic parameter ( for example search query)
Cleaning cache by key: remove( $key)
Example: Mage::app()->getCache()->remove(‘mycache_info_’ . $query);
Deleting from cache only the information defined by $key
Clearing cache by tags: clean($tags = array())
Example: Mage::app()->getCache()->clean(array(‘MYCACHE_CACHE’));
Removing all the data which is defined by a specific tag: for example ‘MYCACHE_CACHE’
<?php
class Alanstormdotcom_Helloworld_IndexController extends Mage_Core_Controller_Front_Action {
public function indexAction() {
$cache = Mage::app()->getCache();
echo $cache->save('cccccccccccccccc', "nick_date", array("nicks_cache"), null);
// $this->loadLayout();
// $this->renderLayout();
}
public function getCacheAction(){
$cache = Mage::app()->getCache();
$v = $cache->load("nick_date");
$t = Mage::app()->getCacheInstance()->getTypes();
var_dump($t);
}
}