Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5119005
  • 博文数量: 921
  • 博客积分: 16037
  • 博客等级: 上将
  • 技术积分: 8469
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-05 02:08
文章分类

全部博文(921)

文章存档

2020年(1)

2019年(3)

2018年(3)

2017年(6)

2016年(47)

2015年(72)

2014年(25)

2013年(72)

2012年(125)

2011年(182)

2010年(42)

2009年(14)

2008年(85)

2007年(89)

2006年(155)

分类:

2010-02-24 13:21:01

Last week i had the chance to work with memcache. Something i hadn’t done before. For caching i mostly rely on APC for bytecode. And file based caching for content. Using memecache extension for PHP and de memcached service gives a whole range of features for caching data. And an easy way to manage all this cached data.

The project i am working on is based on Zend Framework. And there for this post will reflect working with memcache in ZF only. With ZF and memcache it’s possible to do frontend and backend caching. I will focuse on the backend side for now. But will setup the frontend as well.

If you don’t have memcache installed do it now. It’s not that hard. On my debian box it went somethign like this:

$ aptitude install memcached
$ aptitude install php5-memcache

Now a days evrything seems to go automatic. So no need to edit the php.ini or anyhting. To check if PHP loaded the memcache extension you can do:

$ php -m | grep memcache
memcache

Memached is automatically started after install. So let’s check if everything went ok.

$ ps -aux | grep memcached
nobody 20965 0.0 0.5 42900 23072 ? S Jan28 0:01 /usr/bin/memcached -m 64 -p 11211 -u nobody -l 127.0.0.1
root 22393 0.0 0.0 87940 776 pts/0 R+ 12:06 0:00 grep memcached

$ netstat -an | grep 11211
tcp 0 0 127.0.0.1:11211 0.0.0.0:* LISTEN

Looking good. Now it’s time to do some configuration in ZF.

First of the frontend settings. With ‘type’ we set the cache method for the frontend. We chose the standard Core class for this. With ‘lifetime’ we set the time for which a cached entity is valid. The ‘cache_id_prefix’ is a nice way to group for instnace cache data by controller. So just give it a name.

cache.frontend.type = Core
cache.frontend.options.lifetime = 600
cache.frontend.options.automatic_serialization = true
cache.frontend.options.cache_id_prefix = [some string]
cache.frontend.options.cache = true

Now it’s the backends turn to get configured. For ‘type’ we choose Memcached. More types are available. For more info read the ZF manual. The rest of the settings are basic memcached settings like host, port and persitance.

cache.backend.type = Memcached
cache.backend.options.servers.1.host = 127.0.0.1
cache.backend.options.servers.1.port = 11211
cache.backend.options.servers.1.persistent = true
cache.backend.options.servers.1.weight = 1
cache.backend.options.servers.1.timeout = 5
cache.backend.options.servers.1.retry_interval = 15

Finally we can do some PHP code. To get the cache instance setup we add a _initCache() method to the bootstrap class. Inside this method we retrieve the config options by calling getOptions(). And setup the cache object. Which is then stored in the Registry for later use.

 
  1. protected function _initCache()   
  2. {   
  3.         $options = $this->getOptions();   
  4.   
  5.         if (isset($options['cache'])) {   
  6.             $cache = Zend_Cache::factory(   
  7.                 $options['cache']['frontend']['type'],   
  8.                 $options['cache']['backend']['type'],   
  9.                 $options['cache']['frontend']['options'],   
  10.                 $options['cache']['backend']['options']   
  11.             );   
  12.   
  13.             Zend_Registry::set('cache'$cache);   
  14.             return $cache;   
  15.         }   
  16. }  

That’s all set. I used the caching for some REST service responses. So i will stick to that in this post as well. The class has a method for adding a cache object.

 
  1. public function setCache(Zend_Cache_Core $cache) {   
  2.     $this->cache = $cache;   
  3. }  

So fetching the cache instance and passing it to the REST client will look something like this:

 
  1. $cache = Zend_Registry::get('cache');   
  2. $cache->setLifetime(86400);   
  3.   
  4. $client = new Client();   
  5. $client->setCache($cache);  

The first thing we do inside the calling method is set a unique key for the cache entry.

 
  1. $cache_id = 'getPage_' . $this->getDomain()   
  2.             . $this->getPageName()   
  3.             . $this->getCountry();  

Then we check if the cache object is set. If that’s the case we check to see if the unique key is already available in the cache.

 
  1. if (!isset($this->cache) || !($ret = $this->cache->load($cache_id))) {  

If a value is returned from the cahce we will return it. If not then we will do the webservice call and store the return data in cache.

 
  1. $ret = $this->restClient->call('getPage'array());   
  2.   
  3. f (isset($this->cache)) {   
  4.    $this->cache->save($ret$cache_id);   

The complete method looks something like this.

 
  1. protected function getPageFromWebservice()   
  2. {   
  3.         $cache_id = 'getPage_' . $this->getDomain()   
  4.             . $this->getPageName()   
  5.             . $this->getCountry();   
  6.   
  7.         if (!isset($this->cache) || !($ret = $this->cache->load($cache_id))) {   
  8.             $ret = $this->restClient->call('getPage'array());   
  9.   
  10.             if (isset($this->cache)) {   
  11.                 $this->cache->save($ret$cache_id);   
  12.             }   
  13.         }   
  14.         return $ret;   
  15. }  

That’s all. It’s pretty basic stuff. But so cool!

阅读(1982) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~