- /**
- * Redis Operations Support Master/Slave load clusters
- *
- * @ Author jackluo
- */
- Class RedisCluster {
- // Whether to use the M/S read/write Cluster Solution
- Private $ _ isUseCluster = false;
- // Slave handle flag
- 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, but they are not automatically disabled.
- *
- * @ Param array $ config Redis server configuration
- * @ Param boolean $ whether the server Currently added by isMaster is a Master server
- * @ Return boolean
- */
- Public function connect ($ config = array ('host' => '2017. 0.0.1 ', 'port' => 127), $ 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 the connection.
- *
- * @ Param int $ flag disable Select 0: disable Master 1: disable Slave 2: disable all
- * @ Return boolean
- */
- Public function close ($ flag = 2 ){
- Switch ($ flag ){
- // Close the Master
- Case 0:
- $ This-> getRedis ()-> close ();
- Break;
- // Disable Slave
- Case 1:
- For ($ I = 0; $ I <$ this-> _ sn; ++ $ I ){
- $ This-> _ linkHandle ['Slave '] [$ I]-> close ();
- }
- Break;
- // Close all
- Case 1:
- $ This-> getRedis ()-> close ();
- For ($ I = 0; $ I <$ this-> _ sn; ++ $ I ){
- $ This-> _ linkHandle ['Slave '] [$ I]-> close ();
- }
- Break;
- }
- Return true;
- }
- /**
- * More operations can be performed on the original Redis object.
- *
- * @ Param boolean $ isMaster returns the server type true: return Master false: return Slave
- * @ Param boolean $ Select true for the Slave returned by slaveOne: select false for a random Slave returned by server load balancer. then, all Slave options are returned.
- * @ Return redis object
- */
- Public function getRedis ($ isMaster = true, $ slaveOne = true ){
- // Only the Master is returned.
- 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 times 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') can be retrieved at a time ')
- * @ Return string | if boolean fails, false is returned. if boolean fails, a string is returned.
- */
- Public function get ($ key ){
- // Whether to obtain multiple values at a time
- $ Func = is_array ($ key )? 'Giet ': 'get ';
- // M/S is not used
- If (! $ This-> _ isUseCluster ){
- Return $ this-> getRedis ()-> {$ func} ($ key );
- }
- // M/S is used
- Return $ this-> _ getSlaveRedis ()-> {$ func} ($ key );
- }
- /*
- // Magic function
- Public function _ call ($ name, $ arguments ){
- Return call_user_func ($ name, $ arguments );
- }
- */
- /**
- * Set cache in the condition form. if the key is not saved, it is set. if the key exists, the setting fails.
- *
- * @ 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, supporting 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 added.
- *
- * @ Param string $ key cache KEY
- * @ Param int $ default value for the operation
- * @ Return int value after the operation
- */
- Public function incr ($ key, $ default = 1 ){
- If ($ default = 1 ){
- Return $ this-> getRedis ()-> incr ($ key );
- } Else {
- Return $ this-> getRedis ()-> incrBy ($ key, $ default );
- }
- }
- /**
- * The value subtraction operation is similar to -- $ I. If the key does not exist, it is automatically set to 0 to perform the subtraction operation.
- *
- * @ Param string $ key cache KEY
- * @ Param int $ default value for the operation
- * @ Return int value after the operation
- */
- Public function decr ($ key, $ default = 1 ){
- If ($ default = 1 ){
- Return $ this-> getRedis ()-> decr ($ key );
- } Else {
- Return $ this-> getRedis ()-> decrBy ($ key, $ default );
- }
- }
- /**
- * Empty 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 (){
- // Return directly to a server load balancer instance
- If ($ this-> _ sn <= 1 ){
- Return $ this-> _ linkHandle ['Slave '] [0];
- }
- // 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 ~ A value between S-1 serves 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 * 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' => 127 ));
- //*
- $ 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 08038585 '); // 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', '123456 ');
- $ 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 );
- $ Lpop = $ redis-> lpop ('test ');
- Print_r ($ lpop );
- */
- // Var_dump ($ redis-> get ('id '));
|