lxylxy888666 发表于 2012-12-5 22:02:18

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

本帖最后由 lxylxy888666 于 2012-12-5 22:04 编辑

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

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

db的用法

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

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



创建libraries下类:
Memcache

<?phpif ( ! 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方法:
    /**

   * 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

<?phpif ( ! defined('BASEPATH')) exit('No direct script access allowed');

$config['memcache']['host'] = 'localhost';
$config['memcache']['port'] = '11211';





最后,你就可以在controller或model里调用了
    /**

   * memcache调用

   */
    public function memcache()
    {
      $this->load->memcache();
      
      $key = 'admin_memcache';
      $this->mc->set($key, time());
      echo $this->mc->get( $key );
    }


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

jeongee 发表于 2012-12-5 22:54:41

额,不是有cache驱动了么

sa0811 发表于 2012-12-9 22:51:42

本帖最后由 sa0811 于 2012-12-9 23:17 编辑

路过。学习一下。

相知犹按剑 发表于 2013-2-24 21:21:26

   $memObj ->connect($this->host, $this->port, $this->timeout);
最好把connect改成addServer,并且不要仅仅部署一台memcached server,否则,当这台Server死机,网站会死的很难看,如果用2台或者更多就没有这个问题。

lxylxy888666 发表于 2013-2-25 16:03:20

相知犹按剑 发表于 2013-2-24 21:21 static/image/common/back.gif
最好把connect改成addServer,并且不要仅仅部署一台memcached server,否则,当这台Server死机,网站会死 ...

说得对,这个有考虑,暂时只用一台。
页: [1]
查看完整版本: 为CI增加类似DB的驱动的memcache模块