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

在实际网站项目中须要把一些配置文件放到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文件或者内存缓存项是否存在,若是存在则验证成功而且删除信息;不然验证失败。相比使用数据库保存验证信息,这种方式更加灵活和高效。

 


收藏到: Del.icio.us


相关文章
相关标签/搜索