MyPhpOffacePsr16Implementation.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace app\common\server;
  3. use Psr\SimpleCache\CacheInterface;
  4. use Psr\SimpleCache\InvalidArgumentException;
  5. /**
  6. * phpoffice 自定义缓存
  7. *
  8. * 参考 PhpOffice\PhpSpreadsheet\Collection\Memory\SimpleCache3
  9. * 使用 PhpOffice\PhpSpreadsheet\Settings::setCache($cache);
  10. *
  11. * @author wj
  12. * @date 2023-01-17
  13. */
  14. class MyPhpOffacePsr16Implementation implements CacheInterface
  15. {
  16. private $redis;
  17. public function __construct()
  18. {
  19. //var_dump(__CLASS__);
  20. $this->redis = new \Redis();
  21. //docker redis1 6379
  22. $this->redis->connect('127.0.0.1', 6379);
  23. $this->redis->auth("qwe110110");
  24. $this->redis->select("1"); //选择1号数据库 默认0号
  25. if (!$this->redis->ping()) {
  26. $error = "connect error";
  27. throw new InvalidArgumentException($error);
  28. //throw new \Exception("链接失败");
  29. }
  30. }
  31. public function get($key, $default = null)
  32. {
  33. $this->redis->get($key);
  34. //throw new InvalidArgumentException($error);
  35. }
  36. public function set($key, $value, $ttl = null)
  37. {
  38. $this->redis->set($key, $value);
  39. //throw new InvalidArgumentException($error);
  40. return true;
  41. }
  42. public function delete($key)
  43. {
  44. $this->redis->del($key);
  45. return true;
  46. //throw new InvalidArgumentException($error);
  47. }
  48. public function clear()
  49. {
  50. $keys = $this->redis->keys("*");
  51. foreach ($keys as $key => $value) {
  52. $this->redis->del($value);
  53. }
  54. return true;
  55. //throw new InvalidArgumentException($error);
  56. }
  57. public function getMultiple($keys, $default = null)
  58. {
  59. $values = [];
  60. foreach ($keys as $key => $value) {
  61. $values[$key] = $this->redis->get($value);
  62. }
  63. return $values;
  64. //throw new InvalidArgumentException($error);
  65. }
  66. public function setMultiple($values, $ttl = null)
  67. {
  68. foreach ($values as $key => $value) {
  69. $this->redis->set($key, $value);
  70. }
  71. return true;
  72. //throw new InvalidArgumentException($error);
  73. }
  74. public function deleteMultiple($keys)
  75. {
  76. foreach ($keys as $key => $value) {
  77. $this->redis->del($key);
  78. }
  79. return true;
  80. //$error = "";
  81. //throw new InvalidArgumentException($error);
  82. }
  83. public function has($key)
  84. {
  85. return $this->redis->exists($key);
  86. //throw new InvalidArgumentException($error);
  87. }
  88. }