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.
 
 
 
 

79 lines
1.5 KiB

  1. <?php
  2. namespace Kuxin\Cache;
  3. use Kuxin\Config;
  4. use Kuxin\Helper\Serialize;
  5. class Yac
  6. {
  7. /**
  8. * @var \Yac
  9. */
  10. protected $handler;
  11. /**
  12. * @var string 缓存前缀
  13. */
  14. protected $prefix = '';
  15. public function __construct($option)
  16. {
  17. if (!extension_loaded('yac')) {
  18. trigger_error('您尚未安装yac扩展', E_USER_ERROR);
  19. }
  20. $this->prefix = $option['prefix'] ?? Config::get('cache.prefix', '');
  21. $this->handler = new \Yac($this->prefix);
  22. }
  23. public function set($key, $value, $time = 0)
  24. {
  25. return $this->handler->set($key, Serialize::encode($value), $time);
  26. }
  27. public function get($key)
  28. {
  29. $return = $this->handler->get($key);
  30. if ($return === false) {
  31. return null;
  32. } elseif (is_string($return)) {
  33. return Serialize::decode($return);
  34. } else {
  35. return $return;
  36. }
  37. }
  38. public function remove($key)
  39. {
  40. return $this->handler->delete($key);
  41. }
  42. public function inc($key, $num = 1)
  43. {
  44. $data = $this->get($key);
  45. if ($data) {
  46. $data += $num;
  47. $this->set($key, $data);
  48. return $data;
  49. }
  50. return false;
  51. }
  52. public function dec($key, $num = 1)
  53. {
  54. $data = $this->get($key);
  55. if ($data) {
  56. $data -= $num;
  57. $this->set($key, $data);
  58. return $data;
  59. }
  60. return false;
  61. }
  62. public function clear()
  63. {
  64. Yac::flush();
  65. }
  66. }