Cache.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2016 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\sae;
  12. use think\Exception;
  13. /**
  14. * SAE Memcache缓存驱动
  15. * @author liu21st <liu21st@gmail.com>
  16. */
  17. class Cache
  18. {
  19. protected $handler = null;
  20. protected $options = [
  21. 'host' => '127.0.0.1',
  22. 'port' => 11211,
  23. 'expire' => 0,
  24. 'timeout' => false,
  25. 'persistent' => false,
  26. 'prefix' => '',
  27. ];
  28. /**
  29. * 架构函数
  30. * @param array $options 缓存参数
  31. * @access public
  32. */
  33. public function __construct($options = [])
  34. {
  35. if (!function_exists('sae_debug')) {
  36. throw new \BadFunctionCallException('must run at sae');
  37. }
  38. $this->handler = new \Memcached();
  39. if (!$this->handler) {
  40. throw new Exception('memcache init error');
  41. }
  42. if (!empty($options)) {
  43. $this->options = array_merge($this->options, $options);
  44. }
  45. }
  46. /**
  47. * 读取缓存
  48. * @access public
  49. * @param string $name 缓存变量名
  50. * @return mixed
  51. */
  52. public function get($name)
  53. {
  54. return $this->handler->get($_SERVER['HTTP_APPVERSION'] . '/' . $this->options['prefix'] . $name);
  55. }
  56. /**
  57. * 写入缓存
  58. * @access public
  59. * @param string $name 缓存变量名
  60. * @param mixed $value 存储数据
  61. * @param integer $expire 有效时间(秒)
  62. * @return bool
  63. */
  64. public function set($name, $value, $expire = null)
  65. {
  66. if (is_null($expire)) {
  67. $expire = $this->options['expire'];
  68. }
  69. $name = $this->options['prefix'] . $name;
  70. if ($this->handler->set($_SERVER['HTTP_APPVERSION'] . '/' . $name, $value, $expire)) {
  71. return true;
  72. }
  73. return false;
  74. }
  75. /**
  76. * 删除缓存
  77. * @param string $name 缓存变量名
  78. * @param bool|false $ttl
  79. * @return bool
  80. */
  81. public function rm($name, $ttl = false)
  82. {
  83. $name = $_SERVER['HTTP_APPVERSION'] . '/' . $this->options['prefix'] . $name;
  84. return false === $ttl ?
  85. $this->handler->delete($name) :
  86. $this->handler->delete($name, $ttl);
  87. }
  88. /**
  89. * 清除缓存
  90. * @access public
  91. * @return bool
  92. */
  93. public function clear()
  94. {
  95. return $this->handler->flush();
  96. }
  97. /**
  98. * 获得SaeKv对象
  99. */
  100. private function getKv()
  101. {
  102. static $kv;
  103. if (!$kv) {
  104. $kv = new \SaeKV();
  105. if (!$kv->init()) {
  106. throw new Exception('KVDB init error');
  107. }
  108. }
  109. return $kv;
  110. }
  111. }