PHP implementation of the Redis encapsulation class complete instance, Phpredis encapsulation instance
The examples in this paper are the encapsulation classes that PHP implements to operate Redis. Share to everyone for your reference, as follows:
<?php/** * Redis operation, support master/slave load cluster * * @author jackluo */class rediscluster{//whether to use M/s read/write cluster scheme private $_isu Secluster = false; Slave handle tag Private $_SN = 0; Server connection handle Private $_linkhandle = Array (' master ' =>null,//only supports one master ' slave ' =>array (),//can have multiple slave); /** * Constructor * * @param boolean $isUseCluster whether to use M/s scheme */Public function __construct ($isUseCluster =false) { $this->_isusecluster = $isUseCluster; /** * Connect Server, Note: Use long connection here, improve efficiency, but do not automatically close * * @param array $config redis Server configuration * @param boolean $isMaster whether the currently added server is Master Server * @return Boolean */Public Function connect ($config =array (' host ' = ' 127.0.0.1 ', ' Port ' =>6379) $isMas Ter=true) {//default port if (!isset ($config [' Port ')]) {$config [' port '] = 6379; }//Set Master connection if ($isMaster) {$this->_linkhandle[' Master '] = new Redis (); $ret = $this->_linkhandle[' master ']->pconnect ($config [' Host '], $config [' Port ']; }else{//MultiA Slave connection $this->_linkhandle[' Slave ' [$this->_sn] = new Redis (); $ret = $this->_linkhandle[' slave '] [$this->_sn]->pconnect ($config [' Host '), $config [' Port ']; + + $this->_sn; } return $ret; /** * Close Connection * * @param int $flag off select 0: Off Master 1: Off Slave 2: Close all * @return Boolean */Public function Clos E ($flag =2) {switch ($flag) {//close Master case 0: $this->getredis ()->close (); Break Close Slave case 1:for ($i =0; $i < $this->_sn; + + $i) {$this->_linkhandle[' Slave '] [$i]->clos E (); } break; Close all Case 1: $this->getredis ()->close (); for ($i =0; $i < $this->_sn; + + $i) {$this->_linkhandle[' slave '] [$i]->close (); } break; } return true; }/** * Get Redis Original object can have more action * * @param boolean $isMaster Returns the type of the server true: Returns the master false: returns slave * @param boolean $slaveOne returned slave Select true: Load balancer randomly returns a Slave select false: ReturnBack to all slave select * @return Redis Object */Public Function Getredis ($isMaster =true, $slaveOne =true) {//return only Master if ($isMaster) {return $this->_linkhandle[' master ']; }else{return $slaveOne? $this->_getslaveredis (): $this->_linkhandle[' slave '; }}/** * Write Cache * * @param string $key Save key * @param string $value cache value * @param int $expire Expiration time, 0: No Expiration Time * /Public Function set ($key, $value, $expire =0) {//Never Time out if ($expire = = 0) {$ret = $this->getredis ()->set ( $key, $value); }else{$ret = $this->getredis ()->setex ($key, $expire, $value); } return $ret; }/** * Read cache * * @param string $key cache key, supports fetching multiple $key at once = Array (' Key1 ', ' key2 ') * @return String | | Boolean failure returns FALSE, string successfully returned */Public function get ($key) {//Is multiple values taken at a time $func = Is_array ($key)? ' MGet ': ' Get '; Not using M/s if (! $this->_isusecluster) {return $this->getredis ()->{$func} ($key); }//using M/s return $this-> _getslaveredis ()->{$func} ($key); }/*//Magic function Public Function __call ($name, $arguments) {return Call_user_func ($name, $arguments); }*//** * Conditional form Set cache, if key is not set, setting fails when present, set failure * * @param string $key Cache key * @param string $value cache value * @return bool EAN */Public Function setnx ($key, $value) {return $this->getredis ()->setnx ($key, $value); }/** * Delete cache * * @param string | | Array $key cache key, supports single health: "Key1" or Multiple keys: Array (' Key1 ', ' key2 ') * @return int deleted number of kin */Public function remove ($key) {// $key = "Key1" | | Array (' Key1 ', ' Key2 ') return $this->getredis ()->delete ($key); }/** * value-plus operation, similar to + + $i, if key does not exist automatically set to 0 after the + + operation * * @param string $key cache key * @param int $default operation default value * @return int Operation value */Public Function incr ($key, $default =1) {if ($default = = 1) {return $this->getredis () INCR ($key); }else{return $this->getredis ()->incrby ($key, $default); }}/** * value minus operation, similar-$i if key is notDecrement when present automatically set to 0 * * @param string $key cache key * @param int $default operation default value * @return int Action Value */Public fun Ction decr ($key, $default =1) {if ($default = = 1) {return $this->getredis ()->DECR ($key); }else{return $this->getredis ()->decrby ($key, $default); }}/** * Add empty current Database * * @return Boolean */Public Function clear () {return $this->getredis ()->flushdb (); }/* =================== The following private method =================== *//** * random HASH get Redis Slave Server handle * * @return Redis Obje CT */Private Function _getslaveredis () {//Slave directly returns if ($this->_sn <= 1) {return $this->_lin khandle[' slave '][0]; }//Random Hash Gets the handle of the Slave $hash = $this->_hashid (Mt_rand (), $this->_sn); return $this->_linkhandle[' slave ' [$hash]; }/** * The value between 0~m-1 is obtained by ID * @param string $id * @param int $m * @return int */Private Function _ha Shid ($id, $m =10) {//Converts the string k to a value between 0~m-1 as the hash address of the corresponding record $k = MD5 ($id); $l = strlen ($k); $b = Bin2Hex ($k); $h = 0; for ($i =0; $i < $l; $i + +) {//Add mode hash $h + = substr ($b, $i *2,2); } $hash = ($h)% $m; return $hash; }/** * Lpush */Public Function Lpush ($key, $value) {return $this->getredis ()->lpush ($key, $value); }/** * Add Lpop */Public Function Lpop ($key) {return $this->getredis ()->lpop ($key); }/** * Lrange */Public Function Lrange ($key, $start, $end) {return $this->getredis ()->lrange ($key, $start, $ End); }/** * Set hash opeation */Public Function Hset ($name, $key, $value) {if (Is_array ($value)) {return $this-&G T;getredis ()->hset ($name, $key, serialize ($value)); } return $this->getredis ()->hset ($name, $key, $value); }/** * Get hash opeation */Public Function hget ($name, $key = null, $serialize =true) {if ($key) {$row = $thi S->getredis ()->hget ($name, $key); if ($row && $serialize) {unserialIze ($row); } return $row; } return $this->getredis ()->hgetall ($name); }/** * Delete hash opeation */Public Function Hdel ($name, $key = null) {if ($key) {return $this->getredi S ()->hdel ($name, $key); } return $this->getredis ()->hdel ($name); }/** * Transaction start */Public Function multi () {return $this->getredis ()->multi (); }/** * Transaction send */Public function exec () {return $this->getredis ()->exec (); }}//End class//================= TEST DEMO =================//only one Redis app $redis = new Rediscluster (); $redis->conn ECT (Array (' host ' = ' 127.0.0.1 ', ' Port ' =>6379),//* $cron _id = 10001; $CRON _key = ' cron_list '; $PHONE _key = ' phone_list: '. $cron _id;////cron Info$cron = $redis->hget ($CRON _key, $cron _id); if (empty ($cron)) {$ Cron = array (' ID ' =>10, ' name ' = ' Jackluo ');//mysql data $redis->hset ($CRON _key, $cron _id, $cron); Set Redis}//phone list$phone_list = $redis->lrangE ($PHONE _key,0,-1);p rint_r ($phone _list), if (Empty ($phone _list)) {$phone _list =explode (', ', ' 13228191831,18608041585 '); MySQL Data//join list if ($phone _list) {$redis->multi (); foreach ($phone _list as $phone) {$redis->lpush ($PHONE _key, $phone); } $redis->exec (); }}print_r ($phone _list);/* $list = $redis->hget ($cron _list,); Var_dump ($list); *///*///$redis->set (' id ', 35);/* $redis->lpush (' Test ', ' 1111 '); $redis->lpush (' Test ', ' 2222 '); $redis->lpush (' Test ', ' 3333 '); $list = $redis->lrange (' Test ', 0,-1); Print_r ($list); $lpop = $redis->lpop (' Test '); Print_r ($lpop); $lpop = $redis->lpop (' Test '); Print_r ($lpop); $lpop = $redis->lpop (' Test '); Print_r ($lpop); *///var_dump ($redis->get (' id '));
I hope this article is helpful to you in PHP programming.
http://www.bkjia.com/PHPjc/1071398.html www.bkjia.com true http://www.bkjia.com/PHPjc/1071398.html techarticle PHP Implementation of the Redis encapsulation class complete instance, Phpredis Package Example This article describes the PHP implementation of Redis encapsulation class. Share to everyone for your reference, as follows: php/** ...