Memcached.class.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. *
  21. * @param array $options
  22. */
  23. public function __construct($options = array())
  24. {
  25. if ( !extension_loaded('memcached') ) {
  26. E(L('_NOT_SUPPORT_').':memcached');
  27. }
  28. $options = array_merge(array(
  29. 'servers' => C('MEMCACHED_SERVER') ? C('MEMCACHED_SERVER') : null,
  30. 'lib_options' => C('MEMCACHED_LIB') ? C('MEMCACHED_LIB') : null
  31. ), $options);
  32. $this->options = $options;
  33. $this->options['expire'] = isset($options['expire'])? $options['expire'] : C('DATA_CACHE_TIME');
  34. $this->options['prefix'] = isset($options['prefix'])? $options['prefix'] : C('DATA_CACHE_PREFIX');
  35. $this->options['length'] = isset($options['length'])? $options['length'] : 0;
  36. $this->handler = new MemcachedResource;
  37. $options['servers'] && $this->handler->addServers($options['servers']);
  38. $options['lib_options'] && $this->handler->setOptions($options['lib_options']);
  39. }
  40. /**
  41. * 读取缓存
  42. * @access public
  43. * @param string $name 缓存变量名
  44. * @return mixed
  45. */
  46. public function get($name)
  47. {
  48. N('cache_read',1);
  49. $this->handler->get($this->options['prefix'].$name);
  50. return $this->handler->get($this->options['prefix'].$name);
  51. }
  52. /**
  53. * 写入缓存
  54. * @access public
  55. * @param string $name 缓存变量名
  56. * @param mixed $value 存储数据
  57. * @param integer $expire 有效时间(秒)
  58. * @return boolean
  59. */
  60. public function set($name, $value, $expire = null)
  61. {
  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, time() + $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. }