Cache.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace App\Module\LCache;
  3. use UCore\Helper\CacheTag;
  4. use UCore\Helper\Logger;
  5. class Cache
  6. {
  7. /**
  8. * 回调函数的缓存
  9. *
  10. * @param $key
  11. * @param callable $callable
  12. * @param $param_arr
  13. * @param $exp
  14. * @return mixed
  15. *
  16. */
  17. static public function cacheCall($key2, callable $callable, $param_arr, $exp = 60, $tags = [])
  18. {
  19. $key = self::getKey($key2);
  20. $old = \Illuminate\Support\Facades\Cache::get($key);
  21. if (!is_null($old)) {
  22. return $old;
  23. }
  24. $new = call_user_func_array($callable, $param_arr);
  25. \Illuminate\Support\Facades\Cache::put($key, $new, $exp);
  26. if ($tags) {
  27. CacheTag::key_tags($key, $tags, null);
  28. }
  29. return $new;
  30. }
  31. /**
  32. * 设置数据
  33. *
  34. * @param $key
  35. * @param $data
  36. * @param $exp
  37. * @param $tags
  38. * @return void
  39. */
  40. static public function put($key, $data, $exp = 60, $tags = [])
  41. {
  42. $key = self::getKey($key);
  43. \Illuminate\Support\Facades\Cache::put($key, $data, $exp);
  44. if ($tags) {
  45. CacheTag::key_tags($key, $tags, null);
  46. }
  47. }
  48. /**
  49. * 获取数据
  50. *
  51. * @param $key
  52. * @param $default
  53. * @return mixed
  54. */
  55. static public function get($key, $default = null)
  56. {
  57. $key = self::getKey($key);
  58. Logger::debug("Cache-get".$key);
  59. return \Illuminate\Support\Facades\Cache::get($key,$default);
  60. }
  61. /**
  62. * has数据
  63. * @param $key
  64. * @return bool
  65. */
  66. static public function has($key)
  67. {
  68. $key = self::getKey($key);
  69. return \Illuminate\Support\Facades\Cache::has($key);
  70. }
  71. static public function getTags($key)
  72. {
  73. $key = self::getKey($key);
  74. }
  75. /**
  76. * 获取键名
  77. * @param $data
  78. * @return string
  79. */
  80. static public function getKey($data)
  81. {
  82. return md5(serialize($data));
  83. }
  84. }