- <?php
-
- /**
- +------------------------------------------------------------------------------
- * Run Framework Memcache操作类
- +------------------------------------------------------------------------------
- * @date 17-06
- * @version 1.0
- +------------------------------------------------------------------------------
- */
- class MmCache {
-
- public $mem = null; //Memcache对象
- public $expire = 300; //过期时间(5分钟)
- public $connected = false; //连接标识
- public $compressed = false; //是否启用数据压缩-暂时停止使用
- public $compressed_new = true; //是否启用数据压缩
- public $prefix = 'run_'; //缓存键前缀
-
- /**
- +----------------------------------------------------------
- * 类的构造子
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- */
-
- public function __construct($host = '127.0.0.1', $port = '11211') {
- }
- $this->mem = new Memcache();
- $this->host = $host;
- $this->port = $port;
- }
-
- /**
- +----------------------------------------------------------
- * 类的析构方法(负责资源的清理工作)
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- */
- public function __destruct() {
- $this->close();
- $this->mem = null;
- $this->expire = null;
- $this->connected = null;
- $this->compressed = null;
- $this->compressed_new = null;
- $this->prefix = null;
- }
-
- /**
- +----------------------------------------------------------
- * 打开Memcache连接
- +----------------------------------------------------------
- * @access private
- +----------------------------------------------------------
- */
- private function connect() {
- if (!$this->connected) {
- $this->connected = $this->mem->pconnect($this->host, $this->port);
- if (!$this->connected){
- }
- $host = $port = null;
- }
- }
-
- /**
- +----------------------------------------------------------
- * 关闭Memcache连接
- +----------------------------------------------------------
- * @access private
- +----------------------------------------------------------
- */
- private function close() {
- if ($this->connected) {
- $this->mem->close();
- $this->connected = null;
- }
- }
-
- /**
- +----------------------------------------------------------
- * 写入缓存
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @param string $key 缓存键值
- * @param mixed $value 被缓存的数据
- * @param mixed $expire 缓存时间
- +----------------------------------------------------------
- * @return boolean
- +----------------------------------------------------------
- */
- public function set($key, $value, $expire = 0) {
-
- }
- }
- $expire = $expire > 0 ? $expire : $this->expire;
- $this->connect();
- }
- }
-
- /**
- +----------------------------------------------------------
- * 读取缓存
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @param string $key 缓存键值
- +----------------------------------------------------------
- * @return mixed
- +----------------------------------------------------------
- */
- public function get($key) {
- $this->connect();
- }
-
- return '';
- }
- }
- }
-
- /**
- +----------------------------------------------------------
- * 删除缓存
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @param string $key 缓存键值
- +----------------------------------------------------------
- * @return boolean
- +----------------------------------------------------------
- */
- public function remove($key) {
- $this->connect();
- }
- }
-
- /**
- +----------------------------------------------------------
- * 清除缓存(删除所有缓存数据)
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @return boolean
- +----------------------------------------------------------
- */
- public function clear() {
- $this->connect();
- }
- return $this->mem->flush();
- }
-
- }
-
- ?>
使用方法:
- require './RunDbPdo.php';
- require './MmCache.php';
- $model = new RunDbPdo();
- $cache = new MmCache('127.0.0.1', '11211');
-
- $sql = "select * from mm_user where user_id='{$user_id}'";
-
- $key = "{user_id:$user_id}";
- $data = $cache->get($key);
- $data = $model->getRow($sql);
- $cache->set($key, $data, 3600);
- }