|
本帖最后由 chinalijie 于 2011-8-2 11:28 编辑
CodeIgniter 缓存适配器
包含文件:
./system/libraries/Cache.php
./system/libraries/drivers/Cache_apc.php
./system/libraries/drivers/Cache_dummy.php
./system/libraries/drivers/Cache_file.php
./system/libraries/drivers/Cache_memcached.php 有错误 见贴:http://codeigniter.org.cn/forums/thread-10071-1-1.html
php有俩个扩展 来访问memcached 缓存服务器,memcache和memcached
memcache:Memcache模块提供了于memcached方便的面向过程及面向对象的接口,memcached是为了降低动态web应用 从数据库加载数据而产生的一种常驻进程缓存产品。
memcached:此扩展使用了libmemcached库提供的api与memcached服务端进行交互。它同样提供了一个session处理器(memcached)。 它同时提供了一个session处理器(memcached)。
从俩个扩展的介绍可以看出,memcache扩展安装容易,windows下和linux都可以用,而memcached扩展就不容易安装了,而且linux下才可以用。
本人鉴于此,特开发了,Cache_memcache.php 来访问memcached缓存服务器
代码如下:
PHP复制代码 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter
*
* An open source application development framework for PHP 4.3.2 or newer
*
* @package CodeIgniter
* @author ExpressionEngine Dev Team
* @copyright Copyright (c) 2006 - 2011 EllisLab, Inc.
* @license http://codeigniter.com/user_guide/license.html
* @link http://codeigniter.com
* @since Version 2.0
* @filesource
*/
// ------------------------------------------------------------------------
/**
* CodeIgniter Memcache Caching Class
*
* @package CodeIgniter
* @subpackage Libraries
* @category Core
* @author lijie
* @link
*/
class CI_Cache_memcache extends CI_Driver {
private $_memcache; // Holds the memcache object
protected $_memcache_conf = array(
'default' => array(
'default_host' => '127.0.0.1',
'default_port' => 11211,
'default_weight' => 1
)
);
// ------------------------------------------------------------------------
/**
* Fetch from cache
*
* @param mixed unique key id
* @return mixed data on success/false on failure
*/
public function get ($id)
{
$data = $this->_memcache ->get($id);
return (is_array($data)) ? $data[0] : FALSE;
}
// ------------------------------------------------------------------------
/**
* Save
*
* @param string unique identifier
* @param mixed data being cached
* @param int time to live
* @return boolean true on success, false on failure
*/
public function save ($id, $data, $ttl = 60)
{
return $this->_memcache ->add($id, array($data, time(), $ttl), $ttl);
}
// ------------------------------------------------------------------------
/**
* Delete from Cache
*
* @param mixed key to be deleted.
* @return boolean true on success, false on failure
*/
public function delete ($id)
{
return $this->_memcache ->delete($id);
}
// ------------------------------------------------------------------------
/**
* Clean the Cache
*
* @return boolean false on failure/true on success
*/
public function clean ()
{
return $this->_memcache ->flush();
}
// ------------------------------------------------------------------------
/**
* Cache Info
*
* @param null type not supported in memcached
* @return mixed array on success, false on failure
*/
public function cache_info ($type = NULL)
{
return $this->_memcache ->getStats();
}
// ------------------------------------------------------------------------
/**
* Get Cache Metadata
*
* @param mixed key to get cache metadata on
* @return mixed FALSE on failure, array on success.
*/
public function get_metadata ($id)
{
$stored = $this->_memcache ->get($id);
if (count($stored) !== 3)
{
return FALSE;
}
list($data, $time, $ttl) = $stored;
return array(
'expire' => $time + $ttl,
'mtime' => $time,
'data' => $data
);
}
// ------------------------------------------------------------------------
/**
* Setup memcached.
*/
private function _setup_memcache ()
{
// Try to load memcached server info from the config file.
$CI =& get_instance ();
if ($CI->config->load('memcache', TRUE, TRUE))
{
if (is_array($CI->config->config['memcache']))
{
$this->_memcache_conf = NULL;
foreach ($CI->config->config['memcache'] as $name => $conf)
{
$this->_memcache_conf [$name] = $conf;
}
}
}
$this->_memcache = new Memcache ();
foreach ($this->_memcache_conf as $name => $cache_server)
{ $cache_server = array_pop($cache_server);
if ( ! array_key_exists('hostname', $cache_server))
{
$cache_server['hostname'] = $this->_memcache_conf ['default']['default_host'];
}
if ( ! array_key_exists('port', $cache_server))
{
$cache_server['port'] = $this->_memcache_conf ['default']['default_port'];
}
if ( ! array_key_exists('weight', $cache_server))
{
$cache_server['weight'] = $this->_memcache_conf ['default']['default_weight'];
}
$this->_memcache ->addServer(
$cache_server['hostname'], $cache_server['port'], true, $cache_server['weight']
);
}
}
// ------------------------------------------------------------------------
/**
* Is supported
*
* Returns FALSE if memcache is not supported on the system.
* If it is, we setup the memcache object & return TRUE
*/
public function is_supported ()
{
if ( ! extension_loaded('memcache'))
{
log_message ('error', 'The Memcache Extension must be loaded to use Memcached Cache.');
return FALSE;
}
$this->_setup_memcache ();
return TRUE;
}
// ------------------------------------------------------------------------
}
// End Class
/* End of file Cache_memcache.php */
/* Location: ./system/libraries/Cache/drivers/Cache_memcache.php */
复制代码
请将此文件命名为 Cache_memcache.php 拷到 ./system/libraries/Cache/drivers/下
修改 ./system/libraries/Cache.php 中的 $valid_drivers 数组增加 cache_memcache
PHP复制代码 protected $valid_drivers = array(
'cache_apc', 'cache_file', 'cache_memcached', 'cache_dummy', 'cache_memcache'
);
复制代码
这样就可以使用 memcache 扩展来访问memcached缓存服务器了
示例:
PHP复制代码 $this->load->driver('cache', array('adapter' => 'memcache', 'backup' => 'file'));
$this->cache->save('foo', 'bar123', 10);
var_dump($this->cache->get('foo')); 复制代码
你也可以增加一个配置文件,这样可以使用多个memcached缓存服务器
示例:
PHP复制代码 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
|--------------------------------------------------------------------------
| memcache
|--------------------------------------------------------------------------
|
*/
$config['memcache']=array(
'memcache1'=>array('hostname'=>'192.168.1.1', 'port'=>'11211', 'weight'=>1),
'memcache1'=>array('hostname'=>'192.168.1.2', 'port'=>'11211', 'weight'=>1)
); 复制代码
将配置文件 命名为 memcache.php 保存于 application/config/下
|
评分
-
查看全部评分
|