12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- <?php
- namespace app\common\server;
- use Psr\SimpleCache\CacheInterface;
- use Psr\SimpleCache\InvalidArgumentException;
- /**
- * phpoffice 自定义缓存
- *
- * 参考 PhpOffice\PhpSpreadsheet\Collection\Memory\SimpleCache3
- * 使用 PhpOffice\PhpSpreadsheet\Settings::setCache($cache);
- *
- * @author wj
- * @date 2023-01-17
- */
- class MyPhpOffacePsr16Implementation implements CacheInterface
- {
- private $redis;
- public function __construct()
- {
- //var_dump(__CLASS__);
- $this->redis = new \Redis();
- //docker redis1 6379
- $this->redis->connect('127.0.0.1', 6379);
- $this->redis->auth("qwe110110");
- $this->redis->select("1"); //选择1号数据库 默认0号
- if (!$this->redis->ping()) {
- $error = "connect error";
- throw new InvalidArgumentException($error);
- //throw new \Exception("链接失败");
- }
- }
- public function get($key, $default = null)
- {
- $this->redis->get($key);
- //throw new InvalidArgumentException($error);
- }
- public function set($key, $value, $ttl = null)
- {
- $this->redis->set($key, $value);
- //throw new InvalidArgumentException($error);
- return true;
- }
- public function delete($key)
- {
- $this->redis->del($key);
- return true;
- //throw new InvalidArgumentException($error);
- }
- public function clear()
- {
- $keys = $this->redis->keys("*");
- foreach ($keys as $key => $value) {
- $this->redis->del($value);
- }
- return true;
- //throw new InvalidArgumentException($error);
- }
- public function getMultiple($keys, $default = null)
- {
- $values = [];
- foreach ($keys as $key => $value) {
- $values[$key] = $this->redis->get($value);
- }
- return $values;
- //throw new InvalidArgumentException($error);
- }
- public function setMultiple($values, $ttl = null)
- {
- foreach ($values as $key => $value) {
- $this->redis->set($key, $value);
- }
- return true;
- //throw new InvalidArgumentException($error);
- }
- public function deleteMultiple($keys)
- {
- foreach ($keys as $key => $value) {
- $this->redis->del($key);
- }
- return true;
- //$error = "";
- //throw new InvalidArgumentException($error);
- }
- public function has($key)
- {
- return $this->redis->exists($key);
- //throw new InvalidArgumentException($error);
- }
- }
|