123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- <?php
- namespace think\cache\driver;
- use think\cache\Driver;
- class Redis extends Driver
- {
- protected $options = [
- 'host' => '127.0.0.1',
- 'port' => 6379,
- 'password' => '',
- 'select' => 0,
- 'timeout' => 0,
- 'expire' => 0,
- 'persistent' => false,
- 'prefix' => '',
- 'serialize' => true,
- ];
-
- public function __construct($options = [])
- {
- if (!empty($options)) {
- $this->options = array_merge($this->options, $options);
- }
- if (extension_loaded('redis')) {
- $this->handler = new \Redis;
- if ($this->options['persistent']) {
- $this->handler->pconnect($this->options['host'], $this->options['port'], $this->options['timeout'], 'persistent_id_' . $this->options['select']);
- } else {
- $this->handler->connect($this->options['host'], $this->options['port'], $this->options['timeout']);
- }
- if ('' != $this->options['password']) {
- $this->handler->auth($this->options['password']);
- }
- if (0 != $this->options['select']) {
- $this->handler->select($this->options['select']);
- }
- } elseif (class_exists('\Predis\Client')) {
- $params = [];
- foreach ($this->options as $key => $val) {
- if (in_array($key, ['aggregate', 'cluster', 'connections', 'exceptions', 'prefix', 'profile', 'replication', 'parameters'])) {
- $params[$key] = $val;
- unset($this->options[$key]);
- }
- }
- if ('' == $this->options['password']) {
- unset($this->options['password']);
- }
- $this->handler = new \Predis\Client($this->options, $params);
- $this->options['prefix'] = '';
- } else {
- throw new \BadFunctionCallException('not support: redis');
- }
- }
-
- public function has($name)
- {
- return $this->handler->exists($this->getCacheKey($name)) ? true : false;
- }
-
- public function get($name, $default = false)
- {
- $this->readTimes++;
- $value = $this->handler->get($this->getCacheKey($name));
- if (is_null($value) || false === $value) {
- return $default;
- }
- return $this->unserialize($value);
- }
-
- public function set($name, $value, $expire = null)
- {
- $this->writeTimes++;
- if (is_null($expire)) {
- $expire = $this->options['expire'];
- }
- if ($this->tag && !$this->has($name)) {
- $first = true;
- }
- $key = $this->getCacheKey($name);
- $expire = $this->getExpireTime($expire);
- $value = $this->serialize($value);
- if ($expire) {
- $result = $this->handler->setex($key, $expire, $value);
- } else {
- $result = $this->handler->set($key, $value);
- }
- isset($first) && $this->setTagItem($key);
- return $result;
- }
-
- public function inc($name, $step = 1)
- {
- $this->writeTimes++;
- $key = $this->getCacheKey($name);
- return $this->handler->incrby($key, $step);
- }
-
- public function dec($name, $step = 1)
- {
- $this->writeTimes++;
- $key = $this->getCacheKey($name);
- return $this->handler->decrby($key, $step);
- }
-
- public function rm($name)
- {
- $this->writeTimes++;
- return $this->handler->del($this->getCacheKey($name));
- }
-
- public function clear($tag = null)
- {
- if ($tag) {
-
- $keys = $this->getTagItem($tag);
- $this->handler->del($keys);
- $tagName = $this->getTagKey($tag);
- $this->handler->del($tagName);
- return true;
- }
- $this->writeTimes++;
- return $this->handler->flushDB();
- }
-
- public function tag($name, $keys = null, $overlay = false)
- {
- if (is_null($keys)) {
- $this->tag = $name;
- } else {
- $tagName = $this->getTagKey($name);
- if ($overlay) {
- $this->handler->del($tagName);
- }
- foreach ($keys as $key) {
- $this->handler->sAdd($tagName, $key);
- }
- }
- return $this;
- }
-
- protected function setTagItem($name)
- {
- if ($this->tag) {
- $tagName = $this->getTagKey($this->tag);
- $this->handler->sAdd($tagName, $name);
- }
- }
-
- protected function getTagItem($tag)
- {
- $tagName = $this->getTagKey($tag);
- return $this->handler->sMembers($tagName);
- }
- }
|