CacheService.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace app\api\service;
  3. use think\cache\driver\Redis;
  4. class CacheService
  5. {
  6. /**
  7. * redis 实例
  8. *
  9. * @var think\cache\driver\Redis $redis
  10. */
  11. private $redis;
  12. public function __construct()
  13. {
  14. $this->redis = new Redis([ 'select'=> 2]);
  15. }
  16. /**
  17. * User: YunXi
  18. * Date: 2022-05-21
  19. * Time: 22:00
  20. * Notes: 设置自增
  21. *
  22. * @param [type] $name
  23. * @param integer $step
  24. * @return integer
  25. */
  26. public static function incr($name, $step = 1) :int
  27. {
  28. return (int) self::$redis->inc($name, $step);
  29. }
  30. /**
  31. * User: YunXi
  32. * Date: 2022-05-21
  33. * Time: 22:00
  34. * Notes: 设置过期时间
  35. *
  36. * @param [type] $key
  37. * @param [type] $ttl
  38. * @return void
  39. */
  40. public static function expire($key,$ttl)
  41. {
  42. return self::$redis->expire($key,$ttl);
  43. }
  44. /**
  45. * User: YunXi
  46. * Date: 2022-05-21
  47. * Time: 22:03
  48. * Notes: 删除锁
  49. *
  50. * @param string $key
  51. * @return void
  52. */
  53. public static function del($key)
  54. {
  55. return self::$redis->del($key);
  56. }
  57. }