Redis.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace app\common\server;
  3. use Psr\SimpleCache\InvalidArgumentException;
  4. /**
  5. * excel导出
  6. *
  7. * @Author wj
  8. * @DateTime 2022-10-02
  9. * @Describe
  10. */
  11. class Redis
  12. {
  13. private $redis;
  14. public function __construct()
  15. {
  16. $this->redis = new \Redis();
  17. //docker redis1 6379
  18. $this->redis->connect('127.0.0.1', 6379);
  19. //$this->redis->auth("qwe110110");
  20. $this->redis->auth("zhonghui0123");
  21. $this->redis->select("1"); //选择1号数据库 默认0号
  22. if (!$this->redis->ping()) {
  23. $error = "connect error";
  24. throw new InvalidArgumentException($error);
  25. //throw new \Exception("链接失败");
  26. }
  27. }
  28. public function get($key, $default = null)
  29. {
  30. $value = $this->redis->get($key);
  31. return $value;
  32. //throw new InvalidArgumentException($error);
  33. }
  34. public function set($key, $value, $ttl = null)
  35. {
  36. $this->redis->set($key, $value);
  37. //throw new InvalidArgumentException($error);
  38. return true;
  39. }
  40. public function delete($key)
  41. {
  42. $this->redis->del($key);
  43. return true;
  44. //throw new InvalidArgumentException($error);
  45. }
  46. public function has($key)
  47. {
  48. return $this->redis->exists($key);
  49. //throw new InvalidArgumentException($error);
  50. }
  51. }