: This article mainly introduces the complete PHPRedis encapsulation class example. if you are interested in the PHP Tutorial, you can refer to it.
This article uses an excerpt to add a classic image to your favorites. Hope you can share your comments together.
Class RedisCluster {
// Whether to use the M/S read/write Cluster Solution
Private IsUseCluster = false; // The Slave handle is private. _ Sn = 0;
// Server connection handle
Private $ _ linkHandle = array (
'Master' => null, // only one master is supported
'Slave '=> array (), // you can have multiple slave instances.
);
/*** Constructor ** @ param boolean $ whether isUseCluster adopts the M/S solution */public function _ construct ($ isUseCluster = false) {$ this-> _ isUseCluster = $ isUseCluster;}/*** connect to the server. note: persistent connections are used to improve efficiency, however, the ** @ param array $ config Redis server configuration * @ param boolean $ whether the server Currently added by the isMaster is the Master server * @ return boolean */public function connect ($ c $ isMaster = true) {// default port if (! Isset ($ config ['port']) {$ config ['port'] = 6379;} // Set the 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 close Select 0: close Master 1: disable Slave 2: close all * @ return boolean */public function close ($ 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]-> close ();} break; // close all cases 1: $ this-> getRedis () -> close (); for ($ I = 0; $ I <$ this-> _ sn; ++ $ I) {$ this-> _ linkHandle ['Slave '] [$ I]-> close ();} break;} return true ;} /*** get the original Redis object can have more operations ** @ param boolean $ isMaster returns the server type true: returns the Master false: return Slave * @ param boolean $ Select true for the Slave returned by slaveOne: select false for a randomly returned Slave: return to all Slave select * @ return redis object */public function getRedis ($ isMaster = true, $ slave // only return Master if ($ isMaster) {return $ this-> _ linkHandle ['master'];} else {return $ slaveOne? $ This-> _ getSlaveRedis (): $ this-> _ linkHandle ['Slave '];} /*** write cache ** @ param string $ key group storage 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 one time Multiple $ key = array ('key1', 'key2') * @ return string | boolean failure, false, success, return string */public function get ($ key) {// do you want to obtain multiple values at a time $ func = is_array ($ key )? 'Giet ': 'get'; // No M/S if (! $ This-> _ isUseCluster) {return $ this-> getRedis ()-> {$ func} ($ key );} // use the M/S return $ this-> _ getSlaveRedis ()-> {$ func} ($ key);}/*** condition to set the cache, if the key is not stored, failed to set when the parameter exists ** @ param string $ key cache KEY * @ param string $ value cache value * @ return boolean */public function setnx ($ key, $ value) {return $ this-> getRedis ()-> setnx ($ key, $ value);}/*** delete cache ** @ param string | array $ key cache KEY, supports a single key: "key1" or multiple keys: array ('key1', 'Key2') * @ return int number of keys deleted */public function remove ($ key) {// $ key => "key1" | array ('key1', 'key2') return $ this-> getRedis ()-> delete ($ key );} /*** value addition operation, similar to ++ $ I, if the key does not exist, it is automatically set to 0 and then the ADD operation ** @ param string $ key cache KEY * @ param int $ default value * @ return int value * /public function incr ($ key, $ default = 1) {if ($ default = 1) {return $ this-> getRedis ()-> incr ($ key );} else {return $ this-> getRedis ()-> IncrBy ($ key, $ default) ;}/ *** value subtraction operation, similar to -- $ I, if the key does not exist, it is automatically set to 0 and then perform the subtraction operation ** @ param string $ key cache KEY * @ param int $ default value * @ return int value */public function decr ($ key, $ default = 1) {if ($ default = 1) {return $ this-> getRedis ()-> decr ($ key );} else {return $ this-> getRedis ()-> decrBy ($ key, $ default );}} /*** fill in the current database *** @ return boolean */public function clear () {return $ this-> getRedis ()-> FlushDB ();} /* = ===* // *** obtain the Redis Slave server handle by random HASH ** @ return redis object */private function _ getSlaveRedis () {// directly return if ($ this-> _ sn <= 1) {return $ this-> _ linkHandle ['Slave '] [0];} on a server load balancer instance // Obtain the Slave handle from random Hash $ hash = $ this-> _ hashId (mt_rand (), $ this-> _ sn ); return $ this-> _ linkHandle ['Slave '] [$ hash];}/*** get the hash value 0 ~ based on the ID ~ M-1 @ param string $ id * @ param int $ m * @ return int */private function _ hashId ($ id, $ m = 10) {// Convert string K to 0 ~ M-1 address $ 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 * 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, $ start, $ end);}/*** set hash opeation */public function hset ($ name, $ key, $ value) {if (is_array ($ value) {return $ this-> 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-> getRedis ()-> 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 ();}
}
'). AddClass ('pre-numbering '). hide (); $ (this ). addClass ('Has-numbering '). parent (). append ($ numbering); for (I = 1; I <= lines; I ++) {$ numbering. append ($ ('
'). Text (I) ;}; $ numbering. fadeIn (1700) ;}) ;}; script
The above describes the complete PHPRedis encapsulation class instance, including the content, hope to be helpful to friends interested in PHP tutorials.