SCache.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Module\LCache;
  3. use UCore\Helper\CacheTag;
  4. use UCore\Helper\Logger;
  5. /**
  6. * symfony/cache使用laravel的配置的封装
  7. *
  8. *
  9. */
  10. class SCache
  11. {
  12. /**
  13. * 设置数据
  14. *
  15. * @param $key
  16. * @param $data
  17. * @param $exp
  18. * @param $tags
  19. * @return void
  20. */
  21. static public function put($key, $data, $exp = 60, $tags = [])
  22. {
  23. $key = self::getKey($key);
  24. if ($data instanceof CacheItem) {
  25. $d2 = $data;
  26. } else {
  27. $d2 = new CacheItem($key, $data, 0, $exp);
  28. }
  29. \Illuminate\Support\Facades\Cache::put($key, $d2, $exp);
  30. if ($tags) {
  31. CacheTag::key_tags($key, $tags, null);
  32. }
  33. return $d2;
  34. }
  35. /**
  36. * 获取数据
  37. *
  38. * @param $key
  39. * @param $default
  40. * @return CacheItem
  41. */
  42. static public function get($key, $default = null)
  43. {
  44. $key = self::getKey($key);
  45. Logger::debug("Cache-get" . $key);
  46. $res = \Illuminate\Support\Facades\Cache::get($key, $default);
  47. if ($res instanceof CacheItem) {
  48. return $res;
  49. }
  50. $res = new CacheItem($key, $default, 0);
  51. $res->setHit(false)->expiresAfter(-1);
  52. return $res;
  53. }
  54. /**
  55. * 获取数据
  56. * @param $key
  57. * @param $default
  58. * @return mixed|null
  59. */
  60. static public function getValue($key, $default = null)
  61. {
  62. $item = self::get($key, $default);
  63. return $item->getValue();
  64. }
  65. /**
  66. * has数据
  67. *
  68. * @param $key
  69. * @return bool
  70. */
  71. static public function has($key)
  72. {
  73. $key = self::getKey($key);
  74. return \Illuminate\Support\Facades\Cache::has($key);
  75. }
  76. static public function getTags($key)
  77. {
  78. $key = self::getKey($key);
  79. }
  80. /**
  81. * 获取键名
  82. *
  83. * @param $data
  84. * @return string
  85. */
  86. static public function getKey($data)
  87. {
  88. if(is_string($data)){
  89. return $data;
  90. }
  91. return md5(serialize($data));
  92. }
  93. }