Php redies encapsulation class-php Tutorial

Source: Internet
Author: User
Php redies encapsulation class

  1. /**
  2. * Redis Operations Support Master/Slave load clusters
  3. *
  4. * @ Author jackluo
  5. */
  6. Class RedisCluster {
  7. // Whether to use the M/S read/write Cluster Solution
  8. Private $ _ isUseCluster = false;
  9. // Slave handle flag
  10. Private $ _ sn = 0;
  11. // Server connection handle
  12. Private $ _ linkHandle = array (
  13. 'Master' => null, // only one master is supported
  14. 'Slave '=> array (), // you can have multiple slave instances.
  15. );
  16. /**
  17. * Constructor
  18. *
  19. * @ Param boolean $ whether isUseCluster adopts the M/S solution
  20. */
  21. Public function _ construct ($ isUseCluster = false ){
  22. $ This-> _ isUseCluster = $ isUseCluster;
  23. }
  24. /**
  25. * Connect to the server. note: persistent connections are used to improve efficiency, but they are not automatically disabled.
  26. *
  27. * @ Param array $ config Redis server configuration
  28. * @ Param boolean $ whether the server Currently added by isMaster is a Master server
  29. * @ Return boolean
  30. */
  31. Public function connect ($ config = array ('host' => '2017. 0.0.1 ', 'port' => 127), $ isMaster = true ){
  32. // Default port
  33. If (! Isset ($ config ['port']) {
  34. $ Config ['port'] = 6379;
  35. }
  36. // Set the Master connection
  37. If ($ isMaster ){
  38. $ This-> _ linkHandle ['master'] = new Redis ();
  39. $ Ret = $ this-> _ linkHandle ['master']-> pconnect ($ config ['host'], $ config ['port']);
  40. } Else {
  41. // Multiple Slave connections
  42. $ This-> _ linkHandle ['Slave '] [$ this-> _ sn] = new Redis ();
  43. $ Ret = $ this-> _ linkHandle ['Slave '] [$ this-> _ sn]-> pconnect ($ config ['host'], $ config ['port']);
  44. + $ This-> _ sn;
  45. }
  46. Return $ ret;
  47. }
  48. /**
  49. * Close the connection.
  50. *
  51. * @ Param int $ flag disable Select 0: disable Master 1: disable Slave 2: disable all
  52. * @ Return boolean
  53. */
  54. Public function close ($ flag = 2 ){
  55. Switch ($ flag ){
  56. // Close the Master
  57. Case 0:
  58. $ This-> getRedis ()-> close ();
  59. Break;
  60. // Disable Slave
  61. Case 1:
  62. For ($ I = 0; $ I <$ this-> _ sn; ++ $ I ){
  63. $ This-> _ linkHandle ['Slave '] [$ I]-> close ();
  64. }
  65. Break;
  66. // Close all
  67. Case 1:
  68. $ This-> getRedis ()-> close ();
  69. For ($ I = 0; $ I <$ this-> _ sn; ++ $ I ){
  70. $ This-> _ linkHandle ['Slave '] [$ I]-> close ();
  71. }
  72. Break;
  73. }
  74. Return true;
  75. }
  76. /**
  77. * More operations can be performed on the original Redis object.
  78. *
  79. * @ Param boolean $ isMaster returns the server type true: return Master false: return Slave
  80. * @ 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.
  81. * @ Return redis object
  82. */
  83. Public function getRedis ($ isMaster = true, $ slaveOne = true ){
  84. // Only the Master is returned.
  85. If ($ isMaster ){
  86. Return $ this-> _ linkHandle ['master'];
  87. } Else {
  88. Return $ slaveOne? $ This-> _ getSlaveRedis (): $ this-> _ linkHandle ['Slave '];
  89. }
  90. }
  91. /**
  92. * Write cache
  93. *
  94. * @ Param string $ key group storage KEY
  95. * @ Param string $ value cache value
  96. * @ Param int $ expire Expiration Time, 0: No Expiration Time
  97. */
  98. Public function set ($ key, $ value, $ expire = 0 ){
  99. // Never times out
  100. If ($ expire = 0 ){
  101. $ Ret = $ this-> getRedis ()-> set ($ key, $ value );
  102. } Else {
  103. $ Ret = $ this-> getRedis ()-> setex ($ key, $ expire, $ value );
  104. }
  105. Return $ ret;
  106. }
  107. /**
  108. * Read cache
  109. *
  110. * @ Param string $ key: cache KEY. multiple $ key = array ('key1', 'key2') can be retrieved at a time ')
  111. * @ Return string | if boolean fails, false is returned. if boolean fails, a string is returned.
  112. */
  113. Public function get ($ key ){
  114. // Whether to obtain multiple values at a time
  115. $ Func = is_array ($ key )? 'Giet ': 'get ';
  116. // M/S is not used
  117. If (! $ This-> _ isUseCluster ){
  118. Return $ this-> getRedis ()-> {$ func} ($ key );
  119. }
  120. // M/S is used
  121. Return $ this-> _ getSlaveRedis ()-> {$ func} ($ key );
  122. }
  123. /*
  124. // Magic function
  125. Public function _ call ($ name, $ arguments ){
  126. Return call_user_func ($ name, $ arguments );
  127. }
  128. */
  129. /**
  130. * Set cache in the condition form. if the key is not saved, it is set. if the key exists, the setting fails.
  131. *
  132. * @ Param string $ key cache KEY
  133. * @ Param string $ value cache value
  134. * @ Return boolean
  135. */
  136. Public function setnx ($ key, $ value ){
  137. Return $ this-> getRedis ()-> setnx ($ key, $ value );
  138. }
  139. /**
  140. * Delete cache
  141. *
  142. * @ Param string | array $ key cache KEY, supporting a single key: "key1" or multiple keys: array ('key1', 'key2 ')
  143. * @ Return int: number of keys deleted
  144. */
  145. Public function remove ($ key ){
  146. // $ Key => "key1" | array ('key1', 'key2 ')
  147. Return $ this-> getRedis ()-> delete ($ key );
  148. }
  149. /**
  150. * Value addition operation, similar to ++ $ I. If the key does not exist, it is automatically set to 0 and then added.
  151. *
  152. * @ Param string $ key cache KEY
  153. * @ Param int $ default value for the operation
  154. * @ Return int value after the operation
  155. */
  156. Public function incr ($ key, $ default = 1 ){
  157. If ($ default = 1 ){
  158. Return $ this-> getRedis ()-> incr ($ key );
  159. } Else {
  160. Return $ this-> getRedis ()-> incrBy ($ key, $ default );
  161. }
  162. }
  163. /**
  164. * 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.
  165. *
  166. * @ Param string $ key cache KEY
  167. * @ Param int $ default value for the operation
  168. * @ Return int value after the operation
  169. */
  170. Public function decr ($ key, $ default = 1 ){
  171. If ($ default = 1 ){
  172. Return $ this-> getRedis ()-> decr ($ key );
  173. } Else {
  174. Return $ this-> getRedis ()-> decrBy ($ key, $ default );
  175. }
  176. }
  177. /**
  178. * Empty the current database.
  179. *
  180. * @ Return boolean
  181. */
  182. Public function clear (){
  183. Return $ this-> getRedis ()-> flushDB ();
  184. }
  185. /* = = */
  186. /**
  187. * Obtain the Redis Slave server handle by random HASH.
  188. *
  189. * @ Return redis object
  190. */
  191. Private function _ getSlaveRedis (){
  192. // Return directly to a server load balancer instance
  193. If ($ this-> _ sn <= 1 ){
  194. Return $ this-> _ linkHandle ['Slave '] [0];
  195. }
  196. // Obtain the Slave handle by random Hash.
  197. $ Hash = $ this-> _ hashId (mt_rand (), $ this-> _ sn );
  198. Return $ this-> _ linkHandle ['Slave '] [$ hash];
  199. }
  200. /**
  201. * Get the hash value 0 ~ based on the ID ~ M-1
  202. *
  203. * @ Param string $ id
  204. * @ Param int $ m
  205. * @ Return int
  206. */
  207. Private function _ hashId ($ id, $ m = 10)
  208. {
  209. // Convert string K to 0 ~ A value between S-1 serves as the hash address of the corresponding record
  210. $ K = md5 ($ id );
  211. $ L = strlen ($ k );
  212. $ B = bin2hex ($ k );
  213. $ H = 0;
  214. For ($ I = 0; $ I <$ l; $ I ++)
  215. {
  216. // Add mode HASH
  217. $ H + = substr ($ B, $ I * 2, 2 );
  218. }
  219. $ Hash = ($ h * 1) % $ m;
  220. Return $ hash;
  221. }
  222. /**
  223. * Lpush
  224. */
  225. Public function lpush ($ key, $ value ){
  226. Return $ this-> getRedis ()-> lpush ($ key, $ value );
  227. }
  228. /**
  229. * Add lpop
  230. */
  231. Public function lpop ($ key ){
  232. Return $ this-> getRedis ()-> lpop ($ key );
  233. }
  234. /**
  235. * Lrange
  236. */
  237. Public function lrange ($ key, $ start, $ end ){
  238. Return $ this-> getRedis ()-> lrange ($ key, $ start, $ end );
  239. }
  240. /**
  241. * Set hash opeation
  242. */
  243. Public function hset ($ name, $ key, $ value ){
  244. If (is_array ($ value )){
  245. Return $ this-> getRedis ()-> hset ($ name, $ key, serialize ($ value ));
  246. }
  247. Return $ this-> getRedis ()-> hset ($ name, $ key, $ value );
  248. }
  249. /**
  250. * Get hash opeation
  251. */
  252. Public function hget ($ name, $ key = null, $ serialize = true ){
  253. If ($ key ){
  254. $ Row = $ this-> getRedis ()-> hget ($ name, $ key );
  255. If ($ row & $ serialize ){
  256. Unserialize ($ row );
  257. }
  258. Return $ row;
  259. }
  260. Return $ this-> getRedis ()-> hgetAll ($ name );
  261. }
  262. /**
  263. * Delete hash opeation
  264. */
  265. Public function hdel ($ name, $ key = null ){
  266. If ($ key ){
  267. Return $ this-> getRedis ()-> hdel ($ name, $ key );
  268. }
  269. Return $ this-> getRedis ()-> hdel ($ name );
  270. }
  271. /**
  272. * Transaction start
  273. */
  274. Public function multi (){
  275. Return $ this-> getRedis ()-> multi ();
  276. }
  277. /**
  278. * Transaction send
  279. */
  280. Public function exec (){
  281. Return $ this-> getRedis ()-> exec ();
  282. }
  283. } // End Class
  284. // ============================ Test demo ==============================
  285. // Only one Redis application
  286. $ Redis = new RedisCluster ();
  287. $ Redis-> connect (array ('host' => '2017. 0.0.1 ', 'port' => 127 ));
  288. //*
  289. $ Cron_id = 10001;
  290. $ CRON_KEY = 'cron _ list ';//
  291. $ PHONE_KEY = 'phone _ LIST: '. $ cron_id ;//
  292. // Cron info
  293. $ Cron = $ redis-> hget ($ CRON_KEY, $ cron_id );
  294. If (empty ($ cron )){
  295. $ Cron = array ('id' => 10, 'name' => 'jackluo'); // mysql data
  296. $ Redis-> hset ($ CRON_KEY, $ cron_id, $ cron); // set redis
  297. }
  298. // Phone list
  299. $ Phone_list = $ redis-> lrange ($ PHONE_KEY, 0,-1 );
  300. Print_r ($ phone_list );
  301. If (empty ($ phone_list )){
  302. $ Phone_list = explode (',', '2017 08038585 '); // mysql data
  303. // Join list
  304. If ($ phone_list ){
  305. $ Redis-> multi ();
  306. Foreach ($ phone_list as $ phone ){
  307. $ Redis-> lpush ($ PHONE_KEY, $ phone );
  308. }
  309. $ Redis-> exec ();
  310. }
  311. }
  312. Print_r ($ phone_list );
  313. /* $ List = $ redis-> hget ($ cron_list ,);
  314. Var_dump ($ list );*/
  315. //*/
  316. // $ Redis-> set ('id', 35 );
  317. /*
  318. $ Redis-> lpush ('test', '123456 ');
  319. $ Redis-> lpush ('test', '123456 ');
  320. $ Redis-> lpush ('test', '123456 ');
  321. $ List = $ redis-> lrange ('test', 0,-1 );
  322. Print_r ($ list );
  323. $ Lpop = $ redis-> lpop ('test ');
  324. Print_r ($ lpop );
  325. $ Lpop = $ redis-> lpop ('test ');
  326. Print_r ($ lpop );
  327. $ Lpop = $ redis-> lpop ('test ');
  328. Print_r ($ lpop );
  329. */
  330. // Var_dump ($ redis-> get ('id '));


Php, redies

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.