Memcached.class.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2013 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 何辉 <runphp@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace Think\Cache\Driver;
  12. use Memcached as MemcachedResource;
  13. use Think\Cache;
  14. /**
  15. * Memcached缓存驱动
  16. */
  17. class Memcached extends Cache {
  18. /**
  19. *
  20. * @param array $options
  21. */
  22. public function __construct($options = array()) {
  23. if ( !extension_loaded('memcached') ) {
  24. E(L('_NOT_SUPPORT_').':memcached');
  25. }
  26. $options = array_merge(array(
  27. 'servers' => C('MEMCACHED_SERVER') ? : null,
  28. 'lib_options' => C('MEMCACHED_LIB') ? : null
  29. ), $options);
  30. $this->options = $options;
  31. $this->options['expire'] = isset($options['expire'])? $options['expire'] : C('DATA_CACHE_TIME');
  32. $this->options['prefix'] = isset($options['prefix'])? $options['prefix'] : C('DATA_CACHE_PREFIX');
  33. $this->options['length'] = isset($options['length'])? $options['length'] : 0;
  34. $this->handler = new MemcachedResource;
  35. $options['servers'] && $this->handler->addServers($options['servers']);
  36. $options['lib_options'] && $this->handler->setOptions($options['lib_options']);
  37. }
  38. /**
  39. * 读取缓存
  40. * @access public
  41. * @param string $name 缓存变量名
  42. * @return mixed
  43. */
  44. public function get($name) {
  45. N('cache_read',1);
  46. return $this->handler->get($this->options['prefix'].$name);
  47. }
  48. /**
  49. * 写入缓存
  50. * @access public
  51. * @param string $name 缓存变量名
  52. * @param mixed $value 存储数据
  53. * @param integer $expire 有效时间(秒)
  54. * @return boolean
  55. */
  56. /*
  57. public function set($name, $value, $expire = null) {
  58. N('cache_write',1);
  59. if(is_null($expire)) {
  60. $expire = $this->options['expire'];
  61. }
  62. $name = $this->options['prefix'].$name;
  63. if($this->handler->set($name, $value, time() + $expire)) {
  64. if($this->options['length']>0) {
  65. // 记录缓存队列
  66. $this->queue($name);
  67. }
  68. return true;
  69. }
  70. return false;
  71. }
  72. */
  73. public function set($name, $value, $expire = null) {
  74. N('cache_write',1);
  75. if(is_null($expire)) {
  76. $expire = $this->options['expire'];
  77. }
  78. $name = $this->options['prefix'].$name;
  79. if (empty($expire))
  80. $time = 0;
  81. else
  82. $time = time() + $expire;
  83. if($this->handler->set($name, $value, $time)) {
  84. if($this->options['length']>0) {
  85. // 记录缓存队列
  86. $this->queue($name);
  87. }
  88. return true;
  89. }
  90. return false;
  91. }
  92. /**
  93. * 删除缓存
  94. * @access public
  95. * @param string $name 缓存变量名
  96. * @return boolean
  97. */
  98. public function rm($name, $ttl = false) {
  99. $name = $this->options['prefix'].$name;
  100. return $ttl === false ?
  101. $this->handler->delete($name) :
  102. $this->handler->delete($name, $ttl);
  103. }
  104. /**
  105. * 清除缓存
  106. * @access public
  107. * @return boolean
  108. */
  109. public function clear() {
  110. return $this->handler->flush();
  111. }
  112. }