Php + redis Cache

Source: Internet
Author: User
Php + redis Cache
Php + redis Cache

  1. Class redisCache {
  2. /**
  3. * $ Host: ip address of the redis server
  4. * $ Port: redis server port
  5. * $ Lifetime: cache file validity period, in seconds
  6. * $ Cacheid: cache file path, including file name
  7. */
  8. Private $ host;
  9. Private $ port;
  10. Private $ lifetime;
  11. Private $ cacheid;
  12. Private $ data;
  13. Public $ redis;
  14. /**
  15. * Destructor: checks whether the cache directory is valid. the default value is assigned.
  16. */
  17. Function _ construct ($ lifetime = 1800 ){
  18. // Configure
  19. $ This-> host = "127.0.0.1 ";
  20. $ This-> port = "6379 ";
  21. $ Redis = new Redis ();
  22. $ Redis-> pconnect ($ this-> host, $ this-> port );
  23. $ This-> redis = $ redis;
  24. $ This-> cacheid = $ this-> getcacheid ();
  25. $ This-> lifetime = $ lifetime;
  26. $ This-> data = $ redis-> HMET ($ this-> cacheid, array ('content', 'creattime '));
  27. // Print_r ($ this-> redis );
  28. // Print_r ($ this-> data );
  29. }
  30. /**
  31. * Check whether the cache is valid
  32. */
  33. Private function isvalid (){
  34. $ Data = $ this-> data;
  35. If (! $ Data ['content']) return false;
  36. If (time ()-$ data ['creattime']> $ this-> lifetime) return false;
  37. Return true;
  38. }
  39. /**
  40. * Write cache
  41. * $ Mode = 0. the page content is obtained in browser cache.
  42. */
  43. Public function write ($ mode = 0, $ content = ''){
  44. Switch ($ mode ){
  45. Case 0:
  46. $ Content = ob_get_contents ();
  47. Break;
  48. Default:
  49. Break;
  50. }
  51. Ob_end_flush ();
  52. Try {
  53. $ This-> redis-> hMset ($ this-> cacheid, array ('content' => $ content, 'creattime' => time ()));
  54. $ This-> redis-> expireAt ($ this-> cacheid, time () + $ this-> lifetime );
  55. }
  56. Catch (Exception $ e ){
  57. $ This-> error ('write cache failed! ');
  58. }
  59. }
  60. /**
  61. * Load cache
  62. * Exit () stops the execution of the original page program after loading the cache. if the cache is invalid, run the original page program to generate the cache.
  63. * Ob_start () enables browser caching to retrieve page content at the end of the page
  64. */
  65. Public function load (){
  66. If ($ this-> isvalid ()){
  67. Echo $ this-> data ['content'];
  68. Exit ();
  69. }
  70. Else {
  71. Ob_start ();
  72. }
  73. }
  74. /**
  75. * Clear cache
  76. */
  77. Public function clean (){
  78. Try {
  79. $ This-> redis-> hDel ($ this-> cacheid, array ('content', 'creattime '));
  80. }
  81. Catch (Exception $ e ){
  82. $ This-> error ('cache cleanup failed! ');
  83. }
  84. }
  85. /**
  86. * Obtain the cache file path
  87. */
  88. Private function getcacheid (){
  89. Return $ this-> dir. md5 ($ this-> geturl (). $ this-> ext;
  90. }
  91. /**
  92. * Obtain the complete url of the current page.
  93. */
  94. Private function geturl (){
  95. $ Url = '';
  96. If (isset ($ _ SERVER ['request _ URI ']) {
  97. $ Url = $ _ SERVER ['request _ URI '];
  98. }
  99. Else {
  100. $ Url = $ _ SERVER ['php _ SELF '];
  101. $ Url. = empty ($ _ SERVER ['query _ string'])? '':'? '. $ _ SERVER ['query _ string'];
  102. }
  103. Return $ url;
  104. }
  105. /**
  106. * Output error message
  107. */
  108. Private function error ($ str ){
  109. Echo'

    '. $ Str .'

    ';
  110. }
  111. }
  112. // Usage:
  113. // Require_once ('rediscache. php ');
  114. // $ Cache = new redisCache (10); // sets the cache lifetime.
  115. // If ($ _ GET ['Your ache']) $ cache-> clean ();
  116. // Else $ cache-> load (); // load the cache. if the cache is valid, the following page code is not executed.
  117. /// Page code starts
  118. /// The page code ends.
  119. // $ Cache-> write (); // The cache is generated when the first operation or cache expires.
  120. ?>


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.