Php redies encapsulation class

Source: Internet
Author: User

Php redies encapsulation class

 

 

 Null, // only one Master is supported. 'slave '=> array (), // multiple slave instances are supported ); /*** 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 ($ config = array ('host' => '2017. 0. 0.1 ', 'Port' => 6379), $ 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, $ slaveOne = true) {// 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 Multiple $ key = array ('key1', 'key2') * @ return string | returns false if boolean fails, returns the 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 );} // M/S return $ this-> _ getSlaveRedis ()-> {$ func} ($ key );} /* // magic function public function _ call ($ name, $ arguments) {return call_user_func ($ name, $ arguments );} * // *** set the cache as a condition. If the key is not saved, 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-> g EtRedis ()-> setnx ($ key, $ value);}/*** Delete cache ** @ param string | array $ key cache KEY, supporting a single key: "key1" or multiple keys: number of keys deleted by array ('key1', 'key2') * @ return int */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 fu Nction 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-> getRe Dis ()-> decrBy ($ key, $ default) ;}/ *** Add 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 by 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 ();}} // End Class // ============================== test demo ======================== ==// only one Redis application $ redis = new RedisCluster (); $ redis-> connect (array ('host' => '2017. 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 (',', '2017 0801_85 '); // 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', '123 '); $ redis-> lpush ('test', '123456'); $ redis-> lpush ('test', '123456 '); $ list = $ redis-> lrange ('test', 0,-1); print_r ($ list); $ lpop = $ redis-> lpop ('test '); print_r ($ lpop); $ lpop = $ redis-> lpop ('test '); print_r ($ lpop); * // var_dump ($ redis-> get ('id '));


 

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.