You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
3.1 KiB

4 years ago
  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. public function set($name, $value, $expire = null) {
  57. N('cache_write',1);
  58. if(is_null($expire)) {
  59. $expire = $this->options['expire'];
  60. }
  61. $name = $this->options['prefix'].$name;
  62. if($this->handler->set($name, $value, time() + $expire)) {
  63. if($this->options['length']>0) {
  64. // 记录缓存队列
  65. $this->queue($name);
  66. }
  67. return true;
  68. }
  69. return false;
  70. }
  71. /**
  72. * 删除缓存
  73. * @access public
  74. * @param string $name 缓存变量名
  75. * @return boolean
  76. */
  77. public function rm($name, $ttl = false) {
  78. $name = $this->options['prefix'].$name;
  79. return $ttl === false ?
  80. $this->handler->delete($name) :
  81. $this->handler->delete($name, $ttl);
  82. }
  83. /**
  84. * 清除缓存
  85. * @access public
  86. * @return boolean
  87. */
  88. public function clear() {
  89. return $this->handler->flush();
  90. }
  91. }