PRedis.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * 缓存服务
  4. * @author wesmiler
  5. */
  6. namespace app\weixin\service;
  7. use \think\cache\driver\Redis as BaseRedis;
  8. class PRedis
  9. {
  10. public static $redis = null;
  11. public static function instance(){
  12. if(self::$redis){
  13. return self::$redis;
  14. }
  15. $option = config('config.redis');
  16. $redis = new BaseRedis($option);
  17. return $redis;
  18. }
  19. /**
  20. * 设置
  21. * @param $key
  22. * @param $val
  23. * @param int $expire
  24. */
  25. public static function set($key, $val, $expire=0){
  26. $expire = $expire? $expire : 0;
  27. $val = is_array($val)? json_encode($val, 256) : $val;
  28. self::instance()->set($key, $val, $expire);
  29. }
  30. /**
  31. * 获取
  32. * @param $key
  33. * @return bool
  34. */
  35. public static function get($key){
  36. $cacheData = self::instance()->get($key);
  37. $data = $cacheData? json_decode($cacheData, true) : [];
  38. if(empty($data)){
  39. $data = $cacheData;
  40. }
  41. return $data;
  42. }
  43. /**
  44. * 删除
  45. * @param $key
  46. * @return bool
  47. */
  48. public static function del($key){
  49. return self::instance()->rm($key);
  50. }
  51. /**
  52. * 递增
  53. * @param $key
  54. * @return bool
  55. */
  56. public static function inc($key, $step=1){
  57. return self::instance()->inc($key, $step);
  58. }
  59. /**
  60. * 递减
  61. * @param $key
  62. * @return bool
  63. */
  64. public static function dec($key, $step=1){
  65. return self::instance()->dec($key, $step);
  66. }
  67. /**
  68. * 批量删除
  69. * @param $key
  70. * @return bool
  71. */
  72. public static function delByKeys($key){
  73. $keys = self::instance()->keys($key);
  74. if(empty($keys)){
  75. return false;
  76. }
  77. foreach($keys as $key){
  78. self::instance()->rm($key);
  79. }
  80. }
  81. /**
  82. * 右入队处理
  83. * @param $key
  84. * @param $data
  85. */
  86. public static function rpush($key, $data){
  87. return self::instance()->rpush($key, $data);
  88. }
  89. /**
  90. * 左入出处理
  91. * @param $key
  92. */
  93. public static function lpop($key){
  94. return self::instance()->lpop($key);
  95. }
  96. /**
  97. * 设置有效时间
  98. * @param $key
  99. * @param $expire
  100. * @return mixed
  101. */
  102. public static function expire($key, $expire){
  103. return self::instance()->expire($key, $expire);
  104. }
  105. /**
  106. * 键名是否存在
  107. * @param $key
  108. * @param $expire
  109. * @return mixed
  110. */
  111. public static function exists($key){
  112. return self::instance()->exists($key);
  113. }
  114. }