Memcache 操作类

Jackey PHP 3,507 次浏览 , 没有评论
  1. <?php
  2.  
  3. /**
  4.   +------------------------------------------------------------------------------
  5.  * Run Framework Memcache操作类
  6.   +------------------------------------------------------------------------------
  7.  * @date 17-06
  8.  * @version 1.0
  9.   +------------------------------------------------------------------------------
  10.  */
  11. class MmCache {
  12.  
  13. public $mem = null; //Memcache对象
  14. public $expire = 300; //过期时间(5分钟)
  15. public $connected = false; //连接标识
  16. public $compressed = false; //是否启用数据压缩-暂时停止使用
  17. public $compressed_new = true; //是否启用数据压缩
  18. public $prefix = 'run_'; //缓存键前缀
  19.  
  20. /**
  21.   +----------------------------------------------------------
  22.   * 类的构造子
  23.   +----------------------------------------------------------
  24.   * @access public
  25.   +----------------------------------------------------------
  26.   */
  27.  
  28. public function __construct($host = '127.0.0.1', $port = '11211') {
  29. if (!class_exists('Memcache')) {
  30. die('Not Support : Memcache');
  31. }
  32. $this->mem = new Memcache();
  33. $this->host = $host;
  34. $this->port = $port;
  35. }
  36.  
  37. /**
  38.   +----------------------------------------------------------
  39.   * 类的析构方法(负责资源的清理工作)
  40.   +----------------------------------------------------------
  41.   * @access public
  42.   +----------------------------------------------------------
  43.   */
  44. public function __destruct() {
  45. $this->close();
  46. $this->mem = null;
  47. $this->expire = null;
  48. $this->connected = null;
  49. $this->compressed = null;
  50. $this->compressed_new = null;
  51. $this->prefix = null;
  52. }
  53.  
  54. /**
  55.   +----------------------------------------------------------
  56.   * 打开Memcache连接
  57.   +----------------------------------------------------------
  58.   * @access private
  59.   +----------------------------------------------------------
  60.   */
  61. private function connect() {
  62. if (!$this->connected) {
  63. $this->connected = $this->mem->pconnect($this->host, $this->port);
  64. if (!$this->connected){
  65. die("连接Memcache失败");
  66. }
  67. $host = $port = null;
  68. }
  69. }
  70.  
  71. /**
  72.   +----------------------------------------------------------
  73.   * 关闭Memcache连接
  74.   +----------------------------------------------------------
  75.   * @access private
  76.   +----------------------------------------------------------
  77.   */
  78. private function close() {
  79. if ($this->connected) {
  80. $this->mem->close();
  81. $this->connected = null;
  82. }
  83. }
  84.  
  85. /**
  86.   +----------------------------------------------------------
  87.   * 写入缓存
  88.   +----------------------------------------------------------
  89.   * @access public
  90.   +----------------------------------------------------------
  91.   * @param string $key 缓存键值
  92.   * @param mixed $value 被缓存的数据
  93.   * @param mixed $expire 缓存时间
  94.   +----------------------------------------------------------
  95.   * @return boolean
  96.   +----------------------------------------------------------
  97.   */
  98. public function set($key, $value, $expire = 0) {
  99. $data = serialize($value);
  100.  
  101. if ($this->compressed_new && function_exists('gzcompress')) {
  102. if (!empty($data)){
  103. $data = gzcompress($data, 3);
  104. }
  105. }
  106. $expire = $expire > 0 ? $expire : $this->expire;
  107. if (!$this->connected || !isset($this->connected)){
  108. $this->connect();
  109. }
  110. return $this->mem->set(md5($this->prefix . $key), $data, 0, $expire);
  111. }
  112.  
  113. /**
  114.   +----------------------------------------------------------
  115.   * 读取缓存
  116.   +----------------------------------------------------------
  117.   * @access public
  118.   +----------------------------------------------------------
  119.   * @param string $key 缓存键值
  120.   +----------------------------------------------------------
  121.   * @return mixed
  122.   +----------------------------------------------------------
  123.   */
  124. public function get($key) {
  125. if (!$this->connected || !isset($this->connected)){
  126. $this->connect();
  127. }
  128. $data = $this->mem->get(md5($this->prefix . $key));
  129.  
  130. if (empty($data)){
  131. return '';
  132. }
  133. if ($this->compressed_new && function_exists('gzcompress')) {
  134. $data = gzuncompress($data);
  135. }
  136. return unserialize($data);
  137. }
  138.  
  139. /**
  140.   +----------------------------------------------------------
  141.   * 删除缓存
  142.   +----------------------------------------------------------
  143.   * @access public
  144.   +----------------------------------------------------------
  145.   * @param string $key 缓存键值
  146.   +----------------------------------------------------------
  147.   * @return boolean
  148.   +----------------------------------------------------------
  149.   */
  150. public function remove($key) {
  151. if ($this->connected == null || !isset($this->connected)){
  152. $this->connect();
  153. }
  154. return $this->mem->delete(md5($this->prefix . $key));
  155. }
  156.  
  157. /**
  158.   +----------------------------------------------------------
  159.   * 清除缓存(删除所有缓存数据)
  160.   +----------------------------------------------------------
  161.   * @access public
  162.   +----------------------------------------------------------
  163.   * @return boolean
  164.   +----------------------------------------------------------
  165.   */
  166. public function clear() {
  167. if ($this->connected == null || !isset($this->connected)){
  168. $this->connect();
  169. }
  170. return $this->mem->flush();
  171. }
  172.  
  173. }
  174.  
  175. ?>

 

使用方法:

  1. require './RunDbPdo.php';
  2. require './MmCache.php';
  3. $model = new RunDbPdo();
  4. $cache = new MmCache('127.0.0.1', '11211');
  5.  
  6. extract($_GET);
  7. $user_id = isset($user_id)?$user_id:0;
  8. $sql = "select * from mm_user where user_id='{$user_id}'";
  9.  
  10. $key = "{user_id:$user_id}";
  11. $data = $cache->get($key);
  12. if(empty($data)){
  13. $data = $model->getRow($sql);
  14. $cache->set($key, $data, 3600);
  15. }
  16. var_dump($data);

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

Go