CacheMemcache.class.php 3.5 KB

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