在實際網站項目中須要把一些配置文件放到XML文件中,有時也須要把一些非關鍵的用戶信息或者應用信息放到xml中保存。這麼有幾個好處,一方面xml文件能夠靈活的配置,減小對數據庫的改動;另外一方面,能夠下降數據庫服務器的壓力。php
固然單純的讀取XML來獲取配置信息並不划算,咱們使用XML同時結合Memcache內存緩存保存數據,能夠提升應用程序的執行效率。在Cakephp框架中配置Memcache以下:html
Cache::config('memcache', array(數據庫
'engine' => 'Memcache',緩存
'duration'=> '+1 day', php框架
'probability'=> 100,安全
'prefix' => 'whatever_', 服務器
'servers' => array(框架
'127.0.0.1:11211' 函數
), post
'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文件或者內存緩存項是否存在,若是存在則驗證成功而且刪除信息;不然驗證失敗。相比使用數據庫保存驗證信息,這種方式更加靈活和高效。