PHP 相关 |
PHP 相关 |
2011-09-16 14:19:53, Fri
Post
#1
|
|
猫猫猫 Group: Power Cat Posts: 626 Joined: 2006-12-8 Member No.: 2 |
一、CodeIgniter 下 memcached 使用问题
1、CodeIgniter 的 memcached 相关接口,用的是 Memcached 名称,而非 Pecl memcache 库的 Memcache 名称,所以无法加载,解决方法: 修改 System/libraries/Cache/drivers/Cache_memcached.php 文件,将 代码 $this->_memcached = new Memcached(); 改成 代码 $this->_memcached = new Memcache(); 代码 if ( ! extension_loaded('memcached')) 改成 代码 if ( ! extension_loaded('memcache')) 2、CodeIgniter 的 Memcache::add 封装有错(依然是 Memcached 的封装),没有正确定义 flag 参数位置,解决方法: 代码 /** * Save * * @param string unique identifier * @param mixed data being cached + * @param int flags * @param int time to live * @return boolean true on success, false on failure */ public function save($id, $data, $ttl = 60) { - return $this->_memcached->add($id, array($data, time(), $ttl), $ttl); + return $this->_memcached->add($id, array($data, time(), $ttl), false, $ttl); } 3、CodeIgniter 没有设置 set 和 replace 入口,解决方法: 代码 + /** + // ------------------------------------------------------------------------ + + /** + * Set + * + * @param string unique identifier + * @param mixed data being cached + * @param int flags + * @param int time to live + * @return boolean true on success, false on failure + */ + public function set($id, $data, $ttl = 60) + { + return $this->_memcached->set($id, array($data, time(), $ttl), false, $ttl); + } + + // ------------------------------------------------------------------------ + + /** + * Replace + * + * @param string unique identifier + * @param mixed data being cached + * @param int flags + * @param int time to live + * @return boolean true on success, false on failure + */ + public function replace($id, $data, $ttl = 60) + { + return $this->_memcached->replace($id, array($data, time(), $ttl), false, $ttl); + } + + // ------------------------------------------------------------------------ + /** * Delete from Cache * * @param mixed key to be deleted. * @return boolean true on success, false on failure */ public function delete($id) { return $this->_memcached->delete($id); } 4、CodeIgniter 没有正确解析永久连接,解决方法: 代码 if ( ! array_key_exists('port', $cache_server)) { $cache_server['port'] = $this->_default_options['default_port']; } + if ( ! array_key_exists('persistent', $cache_server)) + { + $cache_server['persistent'] = TRUE; + } + if ( ! array_key_exists('weight', $cache_server)) { $cache_server['weight'] = $this->_default_options['default_weight']; } $this->_memcached->addServer( - $cache_server['hostname'], $cache_server['port'],$cache_server['persistent'], $cache_server['weight'] + $cache_server['hostname'], $cache_server['port'], + $cache_server['persistent'], $cache_server['weight'] ); 最后,是 CodeIgniter 的 memcache 的配置文件格式: 代码 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['default']['hostname'] = 'unix:///var/run/memcached.sock'; $config['default']['port'] = 0; $config['default']['persistent'] = TRUE; $config['default']['weight'] = 1; |
|
|
Lo-Fi Version | Time is now: 2024-11-1 05:33 |