Write a Redis package class

Source: Internet
Author: User

Plan to encapsulate a Redis operation class for ease of use and a certain log record.
Redis's Packaging ideas:
Further encapsulation based on Redis class


General properties

Singleton (configuration parameter read from config file or write dead?) Consider switching between multiple configurations)

General operations are handled separately according to the business. (Beware of robustness and fault tolerance)

For different purposes, the package that may be done is also the same.
We want to do some low-abstraction packages based on our business, and we will do some high-abstraction encapsulation of tool-level as needed. If the abstraction is high enough, it can make versatility better.
In this case, I'm going to do a generic Redis operation class, first of all, for the most basic operations.

The code is as follows:

<?PHP/** * Created by Phpstorm. * user:freephp * DATE:2016/1/4 * time:11:09*/classMyredisextendsRedis { Public Static $instance=NULL;//single-instance objects     Public $isConnect=false;//determine if Redis is linked successfully     Public $CONNECTMSG= ";//Redis Link Information     Public $date=NULL;//Log Record Date    Private $config= [        //Primary (Master) server' Master ' = [            ' Hostname ' = ' 192.168.1.223 ', ' password ' = ' sewerew ', ' port ' = ' 6379 ', ' Timeo UT ' = ' 5 ',        ],//from the (slave) server' Slave ' = [            ' Hostname ' = ' 192.168.1.230 ', ' password ' = ' qweqq ', ' port ' = ' 6379 ', ' timeout ' = ' 5 ',        ]    ]; function__construct ($params=Array()) {Parent::__construct (); $serverArray=$this-config; //Assembly Parameters        $hostname=$serverArray[' Master '] [' hostname '];//Connection Address        $password=$serverArray[' Master '] [' Password '];//Password        $port=$serverArray[' Master '] [' Port '];//Port        $timeout=$serverArray[' Master '] [' Timeout '];//Timeout//Select user-specified host and database        if(isset($params[' Redis ']) &&array_key_exists($params[' Redis '],$serverArray)) {            $hostname=$serverArray[$params[' Redis '] [' hostname '];//Connection Address            $password=$serverArray[$params[' Redis '] [' Password '];//Password            $port=$serverArray[$params[' Redis '] [' Port '];//Port            $timeout=$serverArray[$params[' Redis '] [' Timeout '];//timed out        }        $this-Date=Date(' y-m-d ', Time()); $this->_connect ($hostname,$port,$timeout,$password); }    /** * Connect to Database * * @param string $hostname host address * @param int $port redis port * @param int $t Imeout Timeout Time Default 30s * @param string $password Verify password * @param bool $isPConnect long connection: Default false Not long connection * @return Bo OL return Value: Successful return true, failed false*/    Private function_connect ($hostname,$port= 6379,$timeout= 5,$password= ",$isPConnect=false) {        //start connecting to the database        Try {            if($isPConnect==false) {                $status=$this->connect ($hostname,$port,$timeout); } Else {                $status=$this->pconnect ($hostname,$port,$timeout); }            if(!$status) {                Error_log(Date(' y-m-d h:i:s '). ":" . ‘ Redis connect error '. "\ T", 3, "./application/logs/redis-error-{$this->date}.log "); return $this->response (false, ' Redis connect error '); }        } Catch(Exception $e) {            Error_log(Date(' y-m-d h:i:s '). ":" .$e->getmessage (). "\ T", 3, "./application/logs/redis-error-{$this->date}.log "); return $this->response (false,$e-getMessage ()); }        //Verify Password        if($password&&!$this->auth ($password)) {            Error_log(Date(' y-m-d h:i:s '). ":" . ‘ Redis password error '. "\ T", 3, "./application/logs/redis-error-{$this->date}.log "); return $this->response (false, ' Redis password error '); }        return $this->response (true, ' Redis connect success '); }     Public Static functionGetInstance ($params=Array(),$flag=false) {        if(! (Self::$instanceinstanceof Self) | |$flag) { self::$instance=NewSelf$params=Array()); }        returnSelf::$instance; }    /** * Return message * * @param bool $status status * @param string $msg message * @return void*/    Private functionResponse$status=false,$msg= ' ') {        $this->isconnect =$status;//determine if Redis is connected successfully        $this->connectmsg =$msg;//Messaging notifications to connect to Redis        return $status; }}
Called$myredis=NewMyredis ();Var_dump($myredis-&GT;CONNECTMSG); die();

Other encapsulation classes that we do based on more detailed business can use this Myredis as an injection class, multi-use combination, to achieve decoupling.

Write a Redis package class

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.