TP5 Cache driver Predis PHP does not have a REDIS driver module when using a Predis connection using Redis

Source: Internet
Author: User
Tags tag name delete cache value store
This article mainly introduces about TP5 cache driver Predis PHP does not have Redis driver module when using Predis connection using Redis, has a certain reference value, now share to everyone, the need for friends can refer to

Use Predis to connect using Redis without the Redis driver module

Installation

Composer require Predis/predis

TP5 Cache driver Increases predis.php

<?phpnamespace think\cache\driver;use think\cache\driver;/** * Predis Cache driver, is the use of PHP without redis extension, suitable for single-machine deployment, There are front-end agents for high-availability scenarios, best performance * There is a need to implement read/write separation at the business level, or to use Rediscluster, please use REDISD driver * * required to install Predis extension: Https://github.com/nrk/predis * /class Predis extends driver{protected $options = [' Scheme ' = ' tcp ', ' host ' = ' 127.0 '.  0.1 ', ' port ' = 6379, ' password ' = ', ' select ' = 0, ' timeout ' = 0, ' expire ' and 0, ' persistent ' and false, ' prefix ' = ', ' database ' and ' = '        15,];    /** * constructor * @param array $options cache parameter * @access public */Public function __construct ($options = [])        {if (!empty ($options)) {$this->options = Array_merge ($this->options, $options); } $func = $this->options[' persistent ')?                ' Pconnect ': ' Connect ';           $this->handler = new \predis\client;     $this->handler-> $func ($this->options[' host '], $this->options[' Port '), $this->options[' timeout '); if ('! = $this->options[' password ') {$this->handler->auth ($this->options[' PA        ssWOrd ']); } if (0! = $this->options[' select ') {$this->handler->select ($this->options        [' SELECT ']);  }}/** * Determine cache * @access public * @param string $name cache variable name * @return BOOL */Public function    Has ($name) {return $this->handler->get ($this->getcachekey ($name))? True:false; /** * Read Cache * @access public * @param string $name cache variable name * @param mixed $default Default value * @return mi Xed */Public function get ($name, $default = False) {$value = $this->handler->get ($this-&GT;GETCAC                Hekey ($name));        if (Is_null ($value)) {return $default; } $jsonData =Json_decode ($value, true); Detects if JSON data is true returns the JSON parsing array, FALSE returns the source data Byron sampson<xiaobo.sun@qq.com> return (null = = $jsonData)?    $value: $jsonData; /** * Write Cache * @access public * @param string $name cache variable name * @param mixed $value store data * @par        AM integer $expire effective time (seconds) * @return Boolean */Public function set ($name, $value, $expire = null) {        if (Is_null ($expire)) {$expire = $this->options[' expire '];        } if ($this->tag &&! $this->has ($name)) {$first = true;        } $key = $this->getcachekey ($name);  Cache processing of array/object data to ensure data integrity Byron sampson<xiaobo.sun@qq.com> $value = (is_object ($value) | | Is_array ($value))?                Json_encode ($value): $value;         if (Is_int ($expire) && $expire) {$result = $this->handler->setex ($key, $expire, $value);         } else {           $result = $this->handler->set ($key, $value);                } isset ($first) && $this->settagitem ($key);    return $result; }/** * Self-cache (for numeric cache) * @access public * @param string $name cache variable name * @param int $step Stride Size *                @return False|int */Public Function Inc ($NAME, $step = 1) {$key = $this->getcachekey ($name);    return $this->handler->incrby ($key, $step); }/** * Self-retarding (for value cache) * @access public * @param string $name cache variable name * @param int $step Step Size *                @return False|int */Public function Dec ($name, $step = 1) {$key = $this->getcachekey ($name);    return $this->handler->decrby ($key, $step); }/** * Delete cache * @access public * @param string $name cache variable Name * @return Boolean */Public function R    M ($name) {return $this->handler->delete ($this->getcachekey ($name));   } /** * Clear Cache * @access Public * @param string $tag tag name * @return Boolean */Public function Clear ($t                        AG = null) {if ($tag) {//Specify label Clear $keys = $this->gettagitem ($tag);            foreach ($keys as $key) {$this->handler->delete ($key);                        } $this->rm (' Tag_ '. MD5 ($tag));        return true;    } return $this->handler->flushdb (); }}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.