在Win7 中为php扩展配置Xcache
内容摘要
XCache 工程由 mOo 领队, 他也是 Lighttpd 的开发成员之一. Lighttpd 是最快的 Web 服务器应用程序之一, 并且超越 Apache 以及许多其他 Web 服务器. XCache 努力达到类似的
文章正文
XCache 工程由 mOo 领队, 他也是 Lighttpd 的开发成员之一. Lighttpd 是最快的 Web 服务器应用程序之一, 并且超越 Apache 以及许多其他 Web 服务器. XCache 努力达到类似的效果.
目前最新的版本为XCache 3.2.0,它是php5全系列支持的,官方网站: http://xcache.lighttpd.net/
如果英文不好的朋友,可以点右边切换语言为中文。
首先下载最新的版本: http://xcache.lighttpd.net/pub/Releases/3.2.0/ 记得选择正确的版本。
下载解压后放到php下的ext目录下,然后打开php.ini 添加extension = php_xcache.dll
压缩包内还有一个中文版Xcache的php.ini的示范,还有就是一个查看Xcache及信息的程序.
注意xcache.admin.pass 用md5加密后存放
xcache.count可以根据你CPU的数设置,默认为1
xcache.slots缓存的文件/变量hash参考值,根据自己的实际情况可以设置
完成后,重新启动Apache服务。
http://127.0.0.1/xcache/htdocs/, 会弹出一个登陆的账号密码对话框,输入进去后,你就可以看到xcache的环境及配置,变量等等。。
$c =new Cache_Xcache();
$c->set('key', 'aaaa123');
echo $c->get('key');
Cache_Xcache::getInstance()->set('key1', '999999999999999');
echo Cache_Xcache::getInstance()->get('key1');
/**------------------------------代码开始----------------------------------**/
class Cache_Xcache {
/**
* 单例模式实例化本类
*
* @var object
*/
protected static $_instance = null;
/**
* 默认的缓存策略
*
* @var array
*/
protected $_defaultOptions = array('expire' => 900);
/**
* 构造方法
*
* @access public
* @return boolean
*/
public function __construct() {
//分析xcache扩展模块 if (!extension_loaded('xcache')) {
die('The xcache extension to be loaded before use!');
}
return true;
}
/**
* 写入缓存
*
* @access public
*
* @param string $key 缓存key
* @param mixted $value 缓存值
* @param integer $expire 生存周期
*
* @return boolean
*/
public function set($key, $value, $expire = null) {
//参数分析 if (!$key) {
return false;
}
$expire = is_null($expire) ? $this->_defaultOptions['expire'] : $expire;
return xcache_set($key, $value, $expire);
}
/**
* 读取缓存,失败或缓存撒失效时返回 false
*
* @access public
*
* @param string $key 缓存key
*
* @return mixted
*/
public function get($key) {
//参数分析 if (!$key) {
return false;
}
return xcache_isset($key) ? xcache_get($key) : false;
}
/**
* 缓存一个变量到数据存储
*
* @access public
*
* @param string $key 数据key
* @param mixed $value 数据值
* @param int $expire 缓存时间(秒)
*
* @return boolean
*/
public function add($key, $value, $expire = null) {
//参数分析 if (!$key) {
return false;
}
$expire = is_null($expire) ? $this->_defaultOptions['expire'] : $expire;
return !xcache_isset($key) ? $this->set($key,$value,$expire) : false;
}
/**
* 删除指定的缓存
*
* @access public
*
* @param string $key 缓存Key
*
* @return boolean
*/
public function delete($key) {
//参数分析 if (!$key) {
return false;
}
return xcache_unset($key);
}
/**
* 清空全部缓存变量
*
* @access public
* @return boolean
*/
public function clear() {
return xcache_clear_cache(XC_TYPE_VAR, 0);
}
/**
* 单例模式
*
* 用于本类的单例模式(singleton)实例化
*
* @access public
* @return object
*/
public static function getInstance() {
if (!self::$_instance) {
self::$_instance = new self();
}
return self::$_instance;
}
}
但实际上Xcache不但能缓存变量,而且能缓存php文件,如果你的php环境中配置了Xcache扩展后,它会自动将每次给你访问的php文件都自动缓存。无需再额外的修改代码,十分的方便快捷,如下图的我只访问了phpmyadmin,Xcache官方的程序包就可以检测到phpmyadmin的cache列表。
代码很简单,带单例模式,可以直接在应用环境中使用,代码在php5.5.12中完美测试通过。
复制代码 代码如下:
$c =new Cache_Xcache();
$c->set('key', 'aaaa123');
echo $c->get('key');
Cache_Xcache::getInstance()->set('key1', '999999999999999');
echo Cache_Xcache::getInstance()->get('key1');
/**------------------------------代码开始----------------------------------**/
class Cache_Xcache {
/**
* 单例模式实例化本类
*
* @var object
*/
protected static $_instance = null;
/**
* 默认的缓存策略
*
* @var array
*/
protected $_defaultOptions = array('expire' => 900);
/**
* 构造方法
*
* @access public
* @return boolean
*/
public function __construct() {
//分析xcache扩展模块 if (!extension_loaded('xcache')) {
die('The xcache extension to be loaded before use!');
}
return true;
}
/**
* 写入缓存
*
* @access public
*
* @param string $key 缓存key
* @param mixted $value 缓存值
* @param integer $expire 生存周期
*
* @return boolean
*/
public function set($key, $value, $expire = null) {
//参数分析 if (!$key) {
return false;
}
$expire = is_null($expire) ? $this->_defaultOptions['expire'] : $expire;
return xcache_set($key, $value, $expire);
}
/**
* 读取缓存,失败或缓存撒失效时返回 false
*
* @access public
*
* @param string $key 缓存key
*
* @return mixted
*/
public function get($key) {
//参数分析 if (!$key) {
return false;
}
return xcache_isset($key) ? xcache_get($key) : false;
}
/**
* 缓存一个变量到数据存储
*
* @access public
*
* @param string $key 数据key
* @param mixed $value 数据值
* @param int $expire 缓存时间(秒)
*
* @return boolean
*/
public function add($key, $value, $expire = null) {
//参数分析 if (!$key) {
return false;
}
$expire = is_null($expire) ? $this->_defaultOptions['expire'] : $expire;
return !xcache_isset($key) ? $this->set($key,$value,$expire) : false;
}
/**
* 删除指定的缓存
*
* @access public
*
* @param string $key 缓存Key
*
* @return boolean
*/
public function delete($key) {
//参数分析 if (!$key) {
return false;
}
return xcache_unset($key);
}
/**
* 清空全部缓存变量
*
* @access public
* @return boolean
*/
public function clear() {
return xcache_clear_cache(XC_TYPE_VAR, 0);
}
/**
* 单例模式
*
* 用于本类的单例模式(singleton)实例化
*
* @access public
* @return object
*/
public static function getInstance() {
if (!self::$_instance) {
self::$_instance = new self();
}
return self::$_instance;
}
}
代码注释