您的位置:首页 > 数据库 > Memcache

在Cakephp中使用XML和Memcache持久化保存数据

2010-04-20 19:05 330 查看
在实际网站项目中需要把一些配置文件放到XML文件中,有时也需要把一些非关键的用户信息或者应用信息放到xml中保存。这么有几个好处,一方面xml文件可以灵活的配置,减少对数据库的改动;另一方面,可以降低数据库服务器的压力。

当然单纯的读取XML来获取配置信息并不划算,我们使用XML同时结合Memcache内存缓存保存数据,可以提高应用程序的执行效率。在Cakephp框架中配置Memcache如下:

Cache::config('memcache', array(

'engine' => 'Memcache',

'duration'=> '+1 day',

'probability'=> 100,

'prefix' => 'whatever_',

'servers' => array(

'127.0.0.1:11211'

),

'compress' => false,

));

那么就使用Cache::write,Cache::read和Cache::del来操作Memcache变量。另外,通过引入Cakephp核心类库Xml

App::import('Core', 'Xml')

可以很方便的读取XML文件、toArray和toString。结合以上两点,可以设计一个Cakephp组件SiteConfig来完成一下功能:

1)读取站点配置getConfig

2)新建配置文件newConfig

3)更新配置文件updateConfig

4)删除配置文件delConfig

class SiteConfigComponent extends Object{

var $config_root = 'site/';

var $cache_prefix = 'site_config_';

var $config_suffix = '.xml';

var $config_engine = 'memcache';

//get config

function getConfig($str){

$cache_config = false;

//first try to get from memcache

$cache_config = Cache::read($this->cache_prefix.$str, $this->config_engine);

//attention! here we use ===

if($cache_config === false){

//if no found in memcache, try to get config from xml file

$file_path = $this->config_root.$str.$this->config_suffix;

if(file_exists($file_path)){

//and put config array to memcache

$xml = new Xml($file_path);

$cache_config = $xml->toArray();

Cache::write($this->cache_prefix.$str, $cache_config, $this->config_engine);

}

}

return $cache_config;

}

//create config file

function newConfig($str, $value = null){

$file_path = $this->config_root.$str.$this->config_suffix;

//if already existed, then do nothing

if(file_exists($file_path)){

return false;

}

//create dir if necessary

if(!file_exists(dirname($file_path))){

mkdir(dirname($file_path), 0777, true);

}

//create a new file

$handle = fopen($file_path, 'w');

//if $value is not empty, then put it into memcache and xml file

if(!empty($value)){

Cache::write($this->cache_prefix.$str, $value, $this->config_engine);

$xml = new Xml($value);

fwrite($handle, $xml->toString());

}

fclose($handle);

return true;

}

//update config content

function updateConfig($str, $value, $to_disk = true){

$file_path = $this->config_root.$str.$this->config_suffix;

//only do when file exists and $value is not empty

if(file_exists($file_path) && !empty($value)){

//first update memcache

Cache::write($this->cache_prefix.$str, $value, $this->config_engine);

//and if $to_disk is true, then update xml file

if($to_disk){

$handle = fopen($file_path, 'w');

$xml = new Xml($value);

fwrite($handle, $xml->toString());

fclose($handle);

}

return true;

}

return false;

}

//delete config file from disk and memcache

function delConfig($str){

Cache::delete($this->cache_prefix.$str, $this->config_engine);

$file_path = $this->config_root.$str.$this->config_suffix;

if(file_exists($file_path)){

return unlink($file_path);

}

return false;

}

}

使用SiteConfig的一个例子,用户注册后需要通过邮箱验证才能登陆。验证的过程相当于向用户发送激活码,那么用户信息和激活码信息可以通过SiteConfig保存到XML和Memcache中。用户验证的时候,只需要查找对应的XML文件或者内存缓存项是否存在,如果存在则验证成功并且删除信息;否则验证失败。相比使用数据库保存验证信息,这种方式更加灵活和高效。

随机文章:

有关管理 2010-03-05

安装在cakephp中Xcache 2010-01-02

Cakephp的魔法函数解析,findBy… 2009-12-23

Cakephp中使用Captcha实现更加安全的验证码 2009-12-23

Consistent hashing原理和模拟 2009-12-23

收藏到:
Del.icio.us

色界频道——这里有顶尖的摄影大师,也有摄影爱好者,他们用相机收纳大千世界。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐