Memcache.class.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace Think\Cache\Driver;
  12. use Think\Cache;
  13. defined('THINK_PATH') or exit();
  14. /**
  15. * Memcache缓存驱动
  16. */
  17. class Memcache extends Cache {
  18. /**
  19. * 架构函数
  20. * @param array $options 缓存参数
  21. * @access public
  22. */
  23. function __construct($options=array()) {
  24. if ( !extension_loaded('memcache') ) {
  25. E(L('_NOT_SUPPORT_').':memcache');
  26. }
  27. $options = array_merge(array (
  28. 'host' => C('MEMCACHE_HOST') ? C('MEMCACHE_HOST') : '127.0.0.1',
  29. 'port' => C('MEMCACHE_PORT') ? C('MEMCACHE_PORT') : 11211,
  30. 'timeout' => C('DATA_CACHE_TIMEOUT') ? C('DATA_CACHE_TIMEOUT') : false,
  31. 'persistent' => false,
  32. ),$options);
  33. $this->options = $options;
  34. $this->options['expire'] = isset($options['expire'])? $options['expire'] : C('DATA_CACHE_TIME');
  35. $this->options['prefix'] = isset($options['prefix'])? $options['prefix'] : C('DATA_CACHE_PREFIX');
  36. $this->options['length'] = isset($options['length'])? $options['length'] : 0;
  37. $func = $options['persistent'] ? 'pconnect' : 'connect';
  38. $this->handler = new \Memcache;
  39. $options['timeout'] === false ?
  40. $this->handler->$func($options['host'], $options['port']) :
  41. $this->handler->$func($options['host'], $options['port'], $options['timeout']);
  42. }
  43. /**
  44. * 读取缓存
  45. * @access public
  46. * @param string $name 缓存变量名
  47. * @return mixed
  48. */
  49. public function get($name) {
  50. N('cache_read',1);
  51. return $this->handler->get($this->options['prefix'].$name);
  52. }
  53. /**
  54. * 写入缓存
  55. * @access public
  56. * @param string $name 缓存变量名
  57. * @param mixed $value 存储数据
  58. * @param integer $expire 有效时间(秒)
  59. * @return boolean
  60. */
  61. public function set($name, $value, $expire = null) {
  62. N('cache_write',1);
  63. if(is_null($expire)) {
  64. $expire = $this->options['expire'];
  65. }
  66. $name = $this->options['prefix'].$name;
  67. if($this->handler->set($name, $value, 0, $expire)) {
  68. if($this->options['length']>0) {
  69. // 记录缓存队列
  70. $this->queue($name);
  71. }
  72. return true;
  73. }
  74. return false;
  75. }
  76. /**
  77. * 删除缓存
  78. * @access public
  79. * @param string $name 缓存变量名
  80. * @return boolean
  81. */
  82. public function rm($name, $ttl = false) {
  83. $name = $this->options['prefix'].$name;
  84. return $ttl === false ?
  85. $this->handler->delete($name) :
  86. $this->handler->delete($name, $ttl);
  87. }
  88. /**
  89. * 清除缓存
  90. * @access public
  91. * @return boolean
  92. */
  93. public function clear() {
  94. return $this->handler->flush();
  95. }
  96. }