Php+redis Cache Class

Source: Internet
Author: User
Tags server port redis server
Php+redis Cache Class
  1. Class Rediscache {
  2. /**
  3. * $host: Redis server IP
  4. * $port: Redis server port
  5. * $lifetime: Cache file validity, 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, check whether the cache directory is valid, default assignment
  16. */
  17. function __construct ($lifetime =1800) {
  18. Configuration
  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->hmget ($this->cacheid, Array (' content ', ' creattime '));
  27. Print_r ($this->redis);
  28. Print_r ($this->data);
  29. }
  30. /**
  31. * Check if 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, get page content 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 () to terminate the execution of the original page program after loading the cache, and run the original page program generation cache if the cache is invalid
  63. * Ob_start () Open browser cache to get 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 (' Purge cache failed! ');
  83. }
  84. }
  85. /**
  86. * Get cache file path
  87. */
  88. Private Function Getcacheid () {
  89. return $this->dir.md5 ($this->geturl ()). $this->ext;
  90. }
  91. /**
  92. * Get the full 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); Set Cache lifetime
  115. if ($_get[' ClearCache ')) $cache->clean ();
  116. else $cache->load (); Load cache, cache is valid do not execute the following page code
  117. Page code Start
  118. Page code End
  119. $cache->write (); First run or cache expiration, generating cache
  120. ?>
Copy Code
PHP, Redis This topic was moved by Beckham on 2015-11-12 08:43
  • 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.