123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441 |
- <?php
- namespace think\cache\driver;
- use think\cache\Driver;
- class Redis extends Driver
- {
- protected $options = [
- 'host' => '8.136.12.232',
- 'port' => 6379,
- 'password' => 'xiang123456',
- '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));
- }
-
- 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 hMSet($key,$fieldArr){
- return $this->handler->hmset($key,$fieldArr);
- }
- public function hGet( $key, $hashKeys ) {
- return $this->handler->hget($key,$hashKeys);
- }
- public function hMGet( $key, $hashKeys ) {
- return $this->handler->hMGet($key,$hashKeys);
- }
- public function hdel( $key, $hashKeys ) {
- return $this->handler->hdel($key,$hashKeys);
- }
- public function hkeys( $key ) {
- return $this->handler->hkeys($key);
- }
-
- public function hGetAll($key){
- return $this->handler->hGetAll($key);
- }
-
- public function hGetvals($key){
- return $this->handler->hvals($key);
- }
-
- public function hGetLen($key){
- return $this->handler->hlen($key);
- }
-
- public function lPush($key,$val){
- return $this->handler->lPush($key,$val);
- }
-
- public function Incr($key){
- $this->handler->incr($key);
- }
-
- public function Incrby($key,$number){
- $this->handler->incrby($key,$number);
- }
-
- public function Decr($key){
- $this->handler->decr($key);
- }
-
- public function Decrby($key,$number){
- $this->handler->decrby($key,$number);
- }
- public function rPush($key,$field_arr){
-
- if(is_array($field_arr)){
- array_unshift($field_arr,$key);
- $keys = $field_arr;
- }else{
- $keys = array($key,$field_arr);
- }
-
- return call_user_func_array(array($this->handler, "rPush"), $keys);
- }
-
- public function lPop($key){
- return $this->handler->lPop($key);
- }
- public function rPop($key){
- return $this->handler->rPop($key);
- }
- public function lGetRange($key,$start,$end){
- return $this->handler->lGetRange($key,$start,$end);
- }
- public function lRange($key,$start,$end){
- return $this->handler->lRange($key,$start,$end);
- }
- public function Llen($key){
- return $this->handler->llen($key);
- }
- public function Ltrim($key,$start,$end){
- return $this->handler->ltrim($key,$start,$end);
- }
-
- public function Setnx($key,$exptime){
- return $this->handler->setnx($key,$exptime);
- }
- public function del($key){
- return $this->handler->del($key);
- }
-
- 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);
- }
- }
|