A php Cache class

Source: Internet
Author: User
Tags delete cache
A php Cache class

  1. Cache. inc. php:
  2. Class Cache {
  3. /**
  4. * $ Dir: cache file storage directory
  5. * $ Lifetime: cache file validity period, in seconds
  6. * $ Cacheid: cache file path, including file name
  7. * $ Ext: cache file extension (not required), which is used for viewing files conveniently.
  8. */
  9. Private $ dir;
  10. Private $ lifetime;
  11. Private $ cacheid;
  12. Private $ ext;
  13. /**
  14. * Destructor: checks whether the cache directory is valid. the default value is assigned.
  15. */
  16. Function _ construct ($ dir = '', $ lifetime = 1800 ){
  17. If ($ this-> dir_isvalid ($ dir )){
  18. $ This-> dir = $ dir;
  19. $ This-> lifetime = $ lifetime;
  20. $ This-> ext = '. php ';
  21. $ This-> cacheid = $ this-> getcacheid ();
  22. }
  23. }
  24. /**
  25. * Check whether the cache is valid
  26. */
  27. Private function isvalid (){
  28. If (! File_exists ($ this-> cacheid) return false;
  29. If (! (@ $ Mtime = filemtime ($ this-> cacheid) return false;
  30. If (mktime ()-$ mtime> $ this-> lifetime) return false;
  31. Return true;
  32. }
  33. /**
  34. * Write cache
  35. * $ Mode = 0. the page content is obtained in browser cache.
  36. * $ Mode = 1. the page content is obtained by directly assigning values (received by the $ content parameter ).
  37. * $ Mode = 2. obtain the page content locally (fopen ile_get_contents) (it seems that this method is unnecessary)
  38. */
  39. Public function write ($ mode = 0, $ content = ''){
  40. Switch ($ mode ){
  41. Case 0:
  42. $ Content = ob_get_contents ();
  43. Break;
  44. Default:
  45. Break;
  46. }
  47. Ob_end_flush ();
  48. Try {
  49. File_put_contents ($ this-> cacheid, $ content );
  50. }
  51. Catch (Exception $ e ){
  52. $ This-> error ('write cache failed! Check the directory permission! ');
  53. }
  54. }
  55. /**
  56. * Load cache
  57. * 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.
  58. * Ob_start () enables browser caching to retrieve page content at the end of the page
  59. */
  60. Public function load (){
  61. If ($ this-> isvalid ()){
  62. Echo "This is Cache .";
  63. // Which of the following two methods is better ?????
  64. Require_once ($ this-> cacheid );
  65. // Echo file_get_contents ($ this-> cacheid );
  66. Exit ();
  67. }
  68. Else {
  69. Ob_start ();
  70. }
  71. }
  72. /**
  73. * Clear cache
  74. */
  75. Public function clean (){
  76. Try {
  77. Unlink ($ this-> cacheid );
  78. }
  79. Catch (Exception $ e ){
  80. $ This-> error ('failed to clear the cached file! Check the directory permission! ');
  81. }
  82. }
  83. /**
  84. * Obtain the cache file path
  85. */
  86. Private function getcacheid (){
  87. Return $ this-> dir. md5 ($ this-> geturl (). $ this-> ext;
  88. }
  89. /**
  90. * Check whether the directory exists or can be created
  91. */
  92. Private function dir_isvalid ($ dir ){
  93. If (is_dir ($ dir) return true;
  94. Try {
  95. Mkdir ($ dir, 0777 );
  96. }
  97. Catch (Exception $ e ){
  98. $ This-> error ('The specified cache directory does not exist and creation failed! Check the directory permission! ');
  99. Return false;
  100. }
  101. Return true;
  102. }
  103. /**
  104. * Obtain the complete url of the current page.
  105. */
  106. Private function geturl (){
  107. $ Url = '';
  108. If (isset ($ _ SERVER ['request _ URI ']) {
  109. $ Url = $ _ SERVER ['request _ URI '];
  110. }
  111. Else {
  112. $ Url = $ _ SERVER ['php _ SELF '];
  113. $ Url. = empty ($ _ SERVER ['query _ string'])? '':'? '. $ _ SERVER ['query _ string'];
  114. }
  115. Return $ url;
  116. }
  117. /**
  118. * Output error message
  119. */
  120. Private function error ($ str ){
  121. Echo'

    '. $ Str .'

    ';
  122. }
  123. }
  124. ?>


  1. Demo. php:
  2. /*
  3. * You can freely repost and use it. please retain the copyright information. thank you for using it!
  4. * Class Name: Cache (For Php5)
  5. * Version: 1.0
  6. * Description: a dynamic cache class used to control automatic page cache generation, call cache, update cache, and delete cache.
  7. * Author: jiangjun8528@163.com, Junin
  8. * Author Page: http://blog.csdn.Net/sdts/
  9. * Last Modify: 2007-8-22
  10. * Remark:
  11. 1. this version is Php5. I have not written the Php4 version yet. if you need it, please refer to it for modification. (it's easy. Don't be so lazy !).
  12. 2. this version is UTF-8 encoded. if the website uses other encoding, convert it by yourself. in Windows, use notepad to open and save it as a file, and select the corresponding encoding (generally ANSI ), in Linux, use the corresponding editing software or the iconv command line.
  13. 3. copy and paste the above 2nd entries.
  14. * Some thoughts on cache:
  15. * The fundamental difference between dynamic cache and static cache is that it is automatic. when a user accesses a page, the cache is generated, browsed, and updated without manual intervention.
  16. * Static cache refers to the generation of static pages. operations are generally completed in the website background and must be manually performed (that is, manually generated ).
  17. */
  18. /*
  19. * Example
  20. ------------------------------------ Demo1 -------------------------------------------
  21. Require_once ('cache. inc. php ');
  22. $ Cachedir = './Cache/'; // sets the Cache directory.
  23. $ Cache = new Cache ($ cachedir, 10); // if the parameter is omitted, the default setting is used. $ cache = new Cache ($ cachedir );
  24. If ($ _ GET ['cacheac']! = 'Rewrite') // Here is a trick, through xx. Php? Cacheact = rewrite: updates the cache, and so on. you can also set some other operations.
  25. $ Cache-> load (); // load the cache. if the cache is valid, the following page code is not executed.
  26. // Page code starts
  27. Echo date ('H: I: s jS f ');
  28. // The page code ends.
  29. $ Cache-> write (); // The first time the cache is run or expires, the cache is generated
  30. ------------------------------------ Demo2 -------------------------------------------
  31. Require_once ('cache. inc. php ');
  32. $ Cachedir = './Cache/'; // sets the Cache directory.
  33. $ Cache = new Cache ($ cachedir, 10); // if the parameter is omitted, the default setting is used. $ cache = new Cache ($ cachedir );
  34. If ($ _ GET ['cacheac']! = 'Rewrite') // Here is a trick, through xx. Php? Cacheact = rewrite: updates the cache, and so on. you can also set some other operations.
  35. $ Cache-> load (); // load the cache. if the cache is valid, the following page code is not executed.
  36. // Page code starts
  37. $ Content = date ('H: I: s jS f ');
  38. Echo $ content;
  39. // The page code ends.
  40. $ Cache-> write (1, $ content); // The cache is generated when the first operation or cache expires.
  41. ------------------------------------ Demo3 -------------------------------------------
  42. Require_once ('cache. inc. php ');
  43. Define ('cacheenable', true );
  44. If (CACHEENABLE ){
  45. $ Cachedir = './Cache/'; // sets the Cache directory.
  46. $ Cache = new Cache ($ cachedir, 10); // if the parameter is omitted, the default setting is used. $ cache = new Cache ($ cachedir );
  47. If ($ _ GET ['cacheac']! = 'Rewrite') // Here is a trick, through xx. Php? Cacheact = rewrite: updates the cache, and so on. you can also set some other operations.
  48. $ Cache-> load (); // load the cache. if the cache is valid, the following page code is not executed.
  49. }
  50. // Page code starts
  51. $ Content = date ('H: I: s jS f ');
  52. Echo $ content;
  53. // The page code ends.
  54. If (CACHEENABLE)
  55. $ Cache-> write (1, $ content); // The cache is generated when the first operation or cache expires.
  56. */
  57. ?>


PHP

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.