This article illustrates the encapsulation class of the PHP implementation operation Redis. Share to everyone for your reference, specific as follows:
<?php/** * Redis operation, support master/slave load cluster * * @author Jackluo/class rediscluster{//whether to use M/s reading and writing cluster scheme Priv
Ate $_isusecluster = false;
Slave handle Mark 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 the M/s scheme is used/the public function __construct ($isUseCluster =false) {
$this->_isusecluster = $isUseCluster; /** * Connect Server, NOTE: This uses long connection, improve efficiency, but will not automatically turn off * * @param array $config redis Server configuration * @param boolean $isMaster currently added services is the Master server * @return Boolean/Public Function connect ($config =array (' Host ' => ' 127.0.0.1 ', ' Port ' =>6379)
, $isMaster =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{//Multiple Slave connections $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 turn off option 0: Close Master 1: Close Slave 2: Close all * @return Boolean/public Functi
On Close ($flag =2) {switch ($flag) {//Shut down Master case 0: $this->getredis ()->close ();
Break Close Slave case 1:for ($i =0; $i < $this->_sn; + + $i) {$this->_linkhandle[' Slave '] [$i]->c
Lose ();
} 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 operations * * @param boolean $isMaster return the server's type true: Return to master FalsE: Return slave * @param boolean $slaveOne return slave select true: Load balancing randomly returns a slave selection false: Returns all slave selection * @return Redis Object */Public Function Getredis ($isMaster =true, $slaveOne =true) {///only returns Master if ($isMaster) {return $this->
_linkhandle[' master '];
}else{return $slaveOne? $this->_getslaveredis (): $this->_linkhandle[' slave ']; }/** * Write Cache * @param string $key Group Key * @param string $value cache value * @param int $expire Expiration time, 0: Indicates no Duration/Public Function set ($key, $value, $expire =0) {//Never timeout if ($expire = 0) {$ret = $this->getredi
S ()->set ($key, $value);
}else{$ret = $this->getredis ()->setex ($key, $expire, $value);
return $ret; /** * Read Cache * * @param string $key cache key, support multiple $key = array (' Key1 ', ' key2 ') * @return String | | Boolean failure returns false, successful return string/Public function get ($key) {//whether to fetch multiple values at once $func = Is_array ($key)?
' Mget ': ' Get '; Not using M/s if (!
$this->_isusecluster) {return $this->getredis ()->{$func} ($key);
/m/s return $this->_getslaveredis ()->{$func} ($key);
/*//Magic function Public Function __call ($name, $arguments) {return Call_user_func ($name, $arguments); * * */** * Conditional form cache, set if key is not saved, set failure * @param string $key Cache key * @param string $value cache value * @retu
RN Boolean/Public Function setnx ($key, $value) {return $this->getredis ()->setnx ($key, $value); /** * Delete Cache * * @param string | |
Array $key cache key, supports a single health: "Key1" or Multiple Health: array (' Key1 ', ' key2 ') * Number of keys deleted by @return Int/Public Function remove ($key) { $key => "Key1" | |
Array (' Key1 ', ' Key2 ') return $this->getredis ()->delete ($key);
/** * Value Gaga operation, similar to + + $i, if the key does not exist automatically set to 0 after the operation of Gaga * * @param string $key cache key * @param int $default operation when the default value
* @return The value of the int operation/Public function incr ($key, $default =1) {if ($default = = 1) { return $this->getredis ()->incr ($key);
}else{return $this->getredis ()->incrby ($key, $default); }/** * Value reduction operation, similar to-$i, if the key does not exist automatically set to 0 after the subtraction operation * * @param string $key cache key * @param int $default operation The default value * @return The value of the int operation/Public function DECR ($key, $default =1) {if ($default = 1) {return $this->g
Etredis ()->DECR ($key);
}else{return $this->getredis ()->decrby ($key, $default); }/** * Adds empty current database * * @return Boolean/Public Function clear () {return $this->getredis ()->FL
Ushdb (); }/* =================== The following private method ===================//** * Random HASH get Redis Slave Server handle * * @return Redis o Bject/Private Function _getslaveredis () {//on a Slave machine directly returns if ($this->_sn <= 1) {return $this-
>_linkhandle[' slave '][0];
}//random Hash gets Slave handle $hash = $this->_hashid (Mt_rand (), $this->_sn); return $this->_linkhandle[' slave ' [$hash]; /** * Gets the value of the hash after 0~m-1 based on ID * * @param string $id * @param int $m * @return int/private Func
tion _hashid ($id, $m =10) {//convert 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 + +) {//additive pattern hash $h + = substr ($b, $i *2,2);
$hash = ($h *1)% $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, $st
Art, $end); }/** * Set hash opeation */Public Function Hset ($name, $key, $value) {if (Is_array ($value)) {return $t
His->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
= $this->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->g
Etredis ()->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 application $redis = new Rediscluster ();
$redis->connect (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);
Print_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 will help you with your PHP programming.