Memcache.class.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. /**
  14. * Memcache缓存驱动
  15. */
  16. class Memcache extends Cache
  17. {
  18. /**
  19. * 架构函数
  20. * @param array $options 缓存参数
  21. * @access public
  22. */
  23. public function __construct($options = array())
  24. {
  25. if (!extension_loaded('memcache')) {
  26. E(L('_NOT_SUPPORT_') . ':memcache');
  27. }
  28. $options = array_merge(array(
  29. 'host' => C('MEMCACHE_HOST') ?: '127.0.0.1',
  30. 'port' => C('MEMCACHE_PORT') ?: 11211,
  31. 'timeout' => C('DATA_CACHE_TIMEOUT') ?: false,
  32. 'persistent' => false,
  33. ), $options);
  34. $this->options = $options;
  35. $this->options['expire'] = isset($options['expire']) ? $options['expire'] : C('DATA_CACHE_TIME');
  36. $this->options['prefix'] = isset($options['prefix']) ? $options['prefix'] : C('DATA_CACHE_PREFIX');
  37. $this->options['length'] = isset($options['length']) ? $options['length'] : 0;
  38. $func = $options['persistent'] ? 'pconnect' : 'connect';
  39. $this->handler = new \Memcache;
  40. false === $options['timeout'] ?
  41. $this->handler->$func($options['host'], $options['port']) :
  42. $this->handler->$func($options['host'], $options['port'], $options['timeout']);
  43. }
  44. /**
  45. * 读取缓存
  46. * @access public
  47. * @param string $name 缓存变量名
  48. * @return mixed
  49. */
  50. public function get($name)
  51. {
  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 boolean
  62. */
  63. public function set($name, $value, $expire = null)
  64. {
  65. N('cache_write', 1);
  66. if (is_null($expire)) {
  67. $expire = $this->options['expire'];
  68. }
  69. $name = $this->options['prefix'] . $name;
  70. if ($this->handler->set($name, $value, 0, $expire)) {
  71. if ($this->options['length'] > 0) {
  72. // 记录缓存队列
  73. $this->queue($name);
  74. }
  75. return true;
  76. }
  77. return false;
  78. }
  79. /**
  80. * 删除缓存
  81. * @access public
  82. * @param string $name 缓存变量名
  83. * @return boolean
  84. */
  85. public function rm($name, $ttl = false)
  86. {
  87. $name = $this->options['prefix'] . $name;
  88. return false === $ttl ?
  89. $this->handler->delete($name) :
  90. $this->handler->delete($name, $ttl);
  91. }
  92. /**
  93. * 清除缓存
  94. * @access public
  95. * @return boolean
  96. */
  97. public function clear()
  98. {
  99. return $this->handler->flush();
  100. }
  101. }