用户
 找回密码
 入住 CI 中国社区
搜索
查看: 7416|回复: 4
收起左侧

[Web] 为CI增加类似DB的驱动的memcache模块

[复制链接]
发表于 2012-12-5 22:02:18 | 显示全部楼层 |阅读模式
本帖最后由 lxylxy888666 于 2012-12-5 22:04 编辑

memcache相信都知道熟悉了
写个类似DB的驱动,加入核心里

比较简单,写个文章凑个数

db的用法

$this->load->database();
然后就可以直接db对象进行数据库操作了

mem也要做到类似这样
$this->load->memcache();
然后就可以用mc对象进行缓存操作了



创建libraries下类:
Memcache
PHP复制代码
 
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
 
/**
 * Memcache Class
 *
 * @package        CodeIgniter
 * @subpackage    Libraries
 * @category    Memcaches
 * @author        ExpressionEngine Dev Team
 * @link        http://codeigniter.com/user_guide/libraries/Memcaches.html
 */

class CI_Memcache {
   
    var $host;
   
    var $port;
   
    var $timeout;
   
    var $mc;
   
   
    public function __construct( $memcache_config = array())
    {
        log_message('debug', "Memcache Class Initialized");
       
        $this->CI =& get_instance();
       
        $r = $this->CI->config->load('memcache');
       
        if( !$memcache_config ) {
            $memcache_config = $this->CI->config->item('memcache');
        }
 
        if( $memcache_config ) {
           
            $this->host = isset( $memcache_config['host'] ) ? $memcache_config['host'] : 'localhost';
           
            $this->port = isset( $memcache_config['port'] ) ? $memcache_config['port'] : '11211';
           
            $this->timeout = isset( $memcache_config['timeout'] ) ? $memcache_config['timeout'] : '60';
           
            return true;
        }
       
        return false;
       
        log_message('debug', "Memcache successfully run");
    }
   
   
   
    /**
     * init memcache
     * @return Memcache
     */

    public function init()
    {
        $key = $this->host . '-' . $this->port;
        if( !$this->mc[$key] ) {
            $memObj   =   new Memcache();
            $memObj ->connect($this->host, $this->port, $this->timeout);
            return $this->mc[$key] = $memObj;
        } else {
            return $this->mc[$key];
        }
    }
   
}
 
复制代码





然后在Loader.php
里加memcache方法:
   
PHP复制代码
/**
 
     * memcache Loader
 
     *
 
     * @param    string    the DB credentials
 
     * @param    bool    whether to return the DB object
 
     * @param    bool    whether to enable active record (this allows us to override the config setting)
 
     * @return    object
 
     */

    public function memcache($params = '', $return = FALSE)
    {
        $CI =& get_instance();
       
        $CI->load->library('memcache');
        $this->memcache = new CI_Memcache();
       
        $CI->mc = $this->memcache->init();
    }
复制代码




当然,要增加配置文件
application/config/memcache.php
PHP复制代码
 
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
$config['memcache']['host'] = 'localhost';
$config['memcache']['port'] = '11211';
复制代码






最后,你就可以在controller或model里调用了
  
PHP复制代码
 /**
 
     * memcache调用
 
     */

    public function memcache()
    {
        $this->load->memcache();
       
        $key = 'admin_memcache';
        $this->mc->set($key, time());
        echo $this->mc->get( $key );
    }
复制代码



简单吧,ci易用好改,5年如一日,支持~~~

发表于 2012-12-5 22:54:41 | 显示全部楼层
额,不是有cache驱动了么
发表于 2012-12-9 22:51:42 CI中国手机版 | 显示全部楼层
本帖最后由 sa0811 于 2012-12-9 23:17 编辑

路过。学习一下。来自: Android客户端
发表于 2013-2-24 21:21:26 | 显示全部楼层
PHP复制代码
     $memObj ->connect($this->host, $this->port, $this->timeout);
复制代码

最好把connect改成addServer,并且不要仅仅部署一台memcached server,否则,当这台Server死机,网站会死的很难看,如果用2台或者更多就没有这个问题。
 楼主| 发表于 2013-2-25 16:03:20 | 显示全部楼层
相知犹按剑 发表于 2013-2-24 21:21
最好把connect改成addServer,并且不要仅仅部署一台memcached server,否则,当这台Server死机,网站会死 ...

说得对,这个有考虑,暂时只用一台。

本版积分规则