在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文件或者内存缓存项是否存在,如果存在则验证成功并且删除信息;否则验证失败。相比使用数据库保存验证信息,这种方式更加灵活和高效。
安装在cakephp中Xcache 2010-01-02
Cakephp的魔法函数解析,findBy… 2009-12-23
Cakephp中使用Captcha实现更加安全的验证码 2009-12-23
Consistent hashing原理和模拟 2009-12-23
收藏到:
Del.icio.us
色界频道——这里有顶尖的摄影大师,也有摄影爱好者,他们用相机收纳大千世界。
当然单纯的读取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
色界频道——这里有顶尖的摄影大师,也有摄影爱好者,他们用相机收纳大千世界。
相关文章推荐
- Android学习(22) -- 数据存储之使用xml保存数据
- 使用dom4j将数据库中的数据保存到xml文件中
- CakePHP中使用saveAll无法正常保存数据的解决办法
- 【android, 3】3.操作数据保存到rom,sd卡上,sharedpreference的使用,pul解析xml
- Android中使用xml保存数据(xml文件序列化)及使用pull解析xml文件
- 数据库操作_连接SQL Server数据库示例;连接ACCESS数据库;连接到 Oracle 数据库示例;SqlCommand 执行SQL命令示例;SqlDataReader 读取数据示例;使用DataAdapter填充数据到DataSet;使用DataTable存储数据库表;将数据库数据填充到 XML 文件;10 使用带输入参数的存储过程;11 使用带输入、输出参数的存储过程示;12 获得数据库中表的数目和名称;13 保存图片到SQL Server数据库示例;14 获得插入记录标识号;Exce
- 使用JDBC4.0操作XML类型的字段(保存获取xml数据)的方法
- 使用XML保存数据
- [drp 4] 使用dom4j,读取XML数据,保存至数据库
- [drp 4] 使用dom4j,读取XML数据,保存至数据库
- iPhoneSDK开发136技系列:第23技使用NSCoding保存数据
- 使用localStrorage保存客户端数据
- 使用OUTPUT保存更新记录前后数据
- 2017-05-23 DBA日记,使用python巧妙分析listener的log.xml,为连接泄漏提供数据支持
- mybatis 处理数组类型及使用Json格式保存数据 JsonTypeHandler and ArrayTypeHandler
- Arduino - 使用EEPROM保存数据
- [原创]使用DataList数据修改,保存,取消
- 使用NSKeyedArchiver保存数据
- C#使用XmlDocument操作XML进行查询、增加、修改、删除、保存应用的实例(转载)
- Java中使用OpenSSL RSA公私钥进行数据加解密保存