ZskkCache.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace app\api\actions;
  3. use think\facade\Cache;
  4. trait ZskkCache
  5. {
  6. protected $redis_name = 'db_config_private';
  7. public function getCache($key, $default = false ,$time = 86400) {
  8. $value = false;
  9. try {
  10. // $value = Cache::store($this->redis_name)->get($key);
  11. if($value !== false) {
  12. if(strpos($key,'TOKEN_WXA_') === 0){
  13. $value = json_decode(json_decode($value),true);
  14. }
  15. return $value;
  16. }
  17. $val = Cache::get($key);
  18. if($val !== false) {
  19. return $val;
  20. }
  21. return $default;
  22. } catch(\Exception $e) {
  23. }
  24. return $default;
  25. }
  26. public function setCache($key, $value, $time = 86400) {
  27. $res2 = false;
  28. try {
  29. // $res2 = Cache::store($this->redis_name)->set($key, $value, $time);
  30. if($res2 == false){
  31. $res = Cache::set($key, $value, $time);
  32. if($res !== false){
  33. return $res;
  34. }
  35. }
  36. } catch(\Exception $e) {
  37. }
  38. return $res2;
  39. }
  40. public function delCache($key)
  41. {
  42. $info = false;
  43. // $info = Cache::store($this->redis_name)->rm($key);
  44. if(!$info){
  45. $data = Cache::rm($key);
  46. if($data){
  47. return $data;
  48. }
  49. }
  50. return $info;
  51. }
  52. public function updateCache($key,$value)
  53. {
  54. $info = false;
  55. // if( Cache::store($this->redis_name)->get($key)){
  56. // $data = Cache::store($this->redis_name)->rm($key);
  57. // $info = Cache::store($this->redis_name)->set($key,$value,86400);
  58. // }
  59. if(Cache::get($key)){
  60. $data = Cache::rm($key);
  61. $info = Cache::set($key,$value,86400);
  62. }
  63. return $info;
  64. }
  65. }