Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

147 Zeilen
2.9 KiB

  1. <?php
  2. /**
  3. * @Author: 杰少Pakey
  4. * @Email : Pakey@qq.com
  5. * @File : cache.php
  6. */
  7. namespace Kuxin;
  8. /**
  9. * 缓存
  10. * Class Cache
  11. *
  12. * @package Kuxin
  13. * @author Pakey <pakey@qq.com>
  14. */
  15. class Cache
  16. {
  17. /**
  18. * @var \Kuxin\Cache\Memcache
  19. */
  20. protected $handler = null;
  21. /**
  22. * Cache constructor.
  23. *
  24. * @param array $config
  25. */
  26. public function __construct(array $config)
  27. {
  28. $class = '\\Kuxin\\Cache\\' . $config['driver'];
  29. $this->handler = Loader::instance($class, [$config['option']]);
  30. }
  31. /**
  32. * 设置缓存
  33. *
  34. * @param $key
  35. * @param $value
  36. * @param int $time
  37. * @return bool
  38. */
  39. public function set(string $key, $value, $time = 0):bool
  40. {
  41. Registry::setInc('_cacheWrite');
  42. return $this->handler->set($key, $value, (int)$time);
  43. }
  44. /**
  45. * 获取缓存
  46. * @param string $key
  47. * @param mixed|null $default
  48. * @return mixed
  49. */
  50. public function get(string $key, $default = null)
  51. {
  52. Registry::setInc('_cacheRead');
  53. $result = $this->handler->get($key);
  54. if ($result === null) {
  55. $result = (is_callable($default) ? $default($key) : $default);
  56. } else {
  57. Registry::setInc('_cacheHit');
  58. }
  59. return $result;
  60. }
  61. /**
  62. * debug模式来获取缓存,debug模式不取缓存
  63. *
  64. * @param $key
  65. * @param mixed $default
  66. * @return mixed
  67. */
  68. public function debugGet(string $key, $default = null)
  69. {
  70. Registry::setInc('_cacheRead');
  71. $result = Config::get('app.debug') ? null : $this->handler->get($key);
  72. if ($result === null) {
  73. $result = (is_callable($default) ? $default($key) : $default);
  74. } else {
  75. Registry::setInc('_cacheHit');
  76. }
  77. return $result;
  78. }
  79. /**
  80. * 删除缓存
  81. *
  82. * @param $key
  83. * @return bool
  84. */
  85. public function remove(string $key): bool
  86. {
  87. return $this->handler->remove($key);
  88. }
  89. /**
  90. * 缓存计数 增加
  91. *
  92. * @param $key
  93. * @param int $len
  94. * @return mixed|bool|int
  95. */
  96. public function inc(string $key, int $len = 1)
  97. {
  98. return $this->handler->inc($key, $len);
  99. }
  100. /**
  101. * 缓存计数 减少
  102. *
  103. * @param $key
  104. * @param int $len
  105. * @return mixed|bool|int
  106. */
  107. public function dec(string $key, int $len = 1)
  108. {
  109. return $this->handler->dec($key, $len);
  110. }
  111. /**
  112. * 清空缓存
  113. */
  114. public function clear(): void
  115. {
  116. $this->handler->clear();
  117. }
  118. /**
  119. * @param $method
  120. * @param $args
  121. */
  122. public function __call($method, $args)
  123. {
  124. if (is_callable([$this->handler, $method])) {
  125. call_user_func_array([$this->handler, $method], $args);
  126. } else {
  127. trigger_error('Cache中不存在的方法');
  128. }
  129. }
  130. }