PHPRedis-php Tutorial

Source: Internet
Author: User
PHPRedis operations

  1. /*************************************** **************************************** **
  2. * InitPHP 2.0 domestic php development framework Dao-Nosql-Redis
  3. *-------------------------------------------------------------------------------
  4. * CopyRight: CopyRight By initphp.com
  5. * You can use the source code freely, but keep the author information during use. Respecting Others means respecting yourself.
  6. *-------------------------------------------------------------------------------
  7. * $ Author: zhuli
  8. * $ Dtime: 2011-10-09
  9. **************************************** **************************************** ***/
  10. Class redisInit {
  11. Private $ redis; // redis object
  12. /**
  13. * Initialize Redis
  14. * $ Config = array (
  15. * 'Server' => '127. 0.0.1 'server
  16. * 'Port' => '123' port number
  17. *)
  18. * @ Param array $ config
  19. */
  20. Public function init ($ config = array ()){
  21. If ($ config ['server'] = '') $ config ['server'] = '2017. 0.0.1 ';
  22. If ($ config ['port'] = '') $ config ['port'] = '123 ';
  23. $ This-> redis = new Redis ();
  24. $ This-> redis-> connect ($ config ['server'], $ config ['port']);
  25. Return $ this-> redis;
  26. }
  27. /**
  28. * Set value
  29. * @ Param string $ key KEY name
  30. * @ Param string | the data obtained by array $ value
  31. * @ Param int $ timeOut time
  32. */
  33. Public function set ($ key, $ value, $ timeOut = 0 ){
  34. $ Value = json_encode ($ value, TRUE );
  35. $ RetRes = $ this-> redis-> set ($ key, $ value );
  36. If ($ timeOut> 0) $ this-> redis-> setTimeout ($ key, $ timeOut );
  37. Return $ retRes;
  38. }
  39. /**
  40. * Get data through KEY
  41. * @ Param string $ key KEY name
  42. */
  43. Public function get ($ key ){
  44. $ Result = $ this-> redis-> get ($ key );
  45. Return json_decode ($ result, TRUE );
  46. }
  47. /**
  48. * Delete a piece of data
  49. * @ Param string $ key KEY name
  50. */
  51. Public function delete ($ key ){
  52. Return $ this-> redis-> delete ($ key );
  53. }
  54. /**
  55. * Clear data
  56. */
  57. Public function flushAll (){
  58. Return $ this-> redis-> flushAll ();
  59. }
  60. /**
  61. * Data entry queue
  62. * @ Param string $ key KEY name
  63. * @ Param string | the data obtained by array $ value
  64. * @ Param bool $ right indicates whether to start from the right
  65. */
  66. Public function push ($ key, $ value, $ right = true ){
  67. $ Value = json_encode ($ value );
  68. Return $ right? $ This-> redis-> rPush ($ key, $ value): $ this-> redis-> lPush ($ key, $ value );
  69. }
  70. /**
  71. * Data output queue
  72. * @ Param string $ key KEY name
  73. * @ Param bool $ whether left outputs data from the left
  74. */
  75. Public function pop ($ key, $ left = true ){
  76. $ Val = $ left? $ This-> redis-> lPop ($ key): $ this-> redis-> rPop ($ key );
  77. Return json_decode ($ val );
  78. }
  79. /**
  80. * Auto-increment data
  81. * @ Param string $ key KEY name
  82. */
  83. Public function increment ($ key ){
  84. Return $ this-> redis-> incr ($ key );
  85. }
  86. /**
  87. * Data auto-subtraction
  88. * @ Param string $ key KEY name
  89. */
  90. Public function decrement ($ key ){
  91. Return $ this-> redis-> decr ($ key );
  92. }
  93. /**
  94. * If the key exists, true is returned.
  95. * @ Param string $ key KEY name
  96. */
  97. Public function exists ($ key ){
  98. Return $ this-> redis-> exists ($ key );
  99. }
  100. /**
  101. * Returns the redis object.
  102. * Redis has many operation methods. we only encapsulate some of them.
  103. * With this object, you can directly call the redis method.
  104. */
  105. Public function redis (){
  106. Return $ this-> redis;
  107. }
  108. }


PHP, Redis

This topic was moved by Beckham

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.