A php page cache class

Source: Internet
Author: User
A php page cache class

  1. /* $ Cache = new Cache ("../cache/", 20); // Constructor to create a cache class object

  2. $ Cache-> PutCache (); // reverse cache
  3. */
  4. Class Cache
  5. {
  6. Private $ CacheDir = 'cache';/* Cache directory */
  7. Private $ SetTimeOut = 10;/* cache expiration time */
  8. Private $ SetExt = '. cache';/* cache file suffix */
  9. Private $ CacheFileUrl = '';/* address of the cached file */
  10. Private $ CacheConfigFile = '';/* cache file configuration information */

  11. Public $ LastUnixTimePoke = 0;/* The last cached Unix timestamp */

  12. Public $ CurrentUnixTimePoke = 0;/* Unix timestamp currently cached */
  13. Public $ NextUnixTimePoke = 0;/* Unix timestamp cached next time */
  14. Public $ UnixNowToNext = 0;/* Unix timestamp that is different from the next cache */

  15. Public $ LastTimePoke = 0;/* last cache time */

  16. Public $ CurrentTimePoke = 0;/* current cache time */
  17. Public $ NextTimePoke = 0;/* The next cache time */

  18. Public $ DataLength = 0;/* cache content length */

  19. Public $ CacheToPage = '';/* cache file content */
  20. Private $ SplitTeam = false;/* Whether to group and store Cache files */

  21. Public $ Cache = false;/* whether the Cache is required, which is determined by the user */

  22. Private $ _ IsCache = false;/* indicates whether data can be cached */

  23. Public function Cache ($ SetTimeOut = 20, $ CacheDir = 'cache', $ SplitTeam = false, $ SetExt = '. cache ')

  24. {
  25. $ This-> CacheDir = $ CacheDir;
  26. $ This-> SplitTeam = $ SplitTeam;
  27. If (! Is_numeric ($ SetTimeOut ))
  28. {
  29. $ This-> ErrResponse ('invalid cache expiration time settings ');
  30. Return false;
  31. } Else {
  32. $ This-> SetTimeOut = $ SetTimeOut;
  33. }
  34. $ This-> SetExt = $ SetExt;

  35. /* Cache start */

  36. Ob_clean ();
  37. Ob_start ();
  38. Ob_implicit_flush (0 );
  39. $ This-> CreateCache ();
  40. Return true;
  41. }

  42. Private function CreateCache ()

  43. {
  44. $ _ CacheFile = str_replace ('.', '_', basename ($ _ SERVER ['php _ SELF ']).' _ '.
  45. Md5 (basename ($ _ SERVER ['php _ SELF ']). $ this-> SetExt;
  46. $ _ CacheConfig = str_replace ('.', '_', basename ($ _ SERVER ['php _ SELF ']).' _ '.'. cof ';

  47. If (! File_exists ($ this-> CacheDir ))

  48. {
  49. Mkdir ($ this-> CacheDir, 0777 );
  50. }

  51. If ($ this-> SplitTeam)

  52. {
  53. $ _ CacheConfigDir = $ this-> CacheDir. str_replace ('.', '_', basename ($ _ SERVER ['php _ SELF ']).' _/';
  54. If (! File_exists ($ _ CacheConfigDir ))
  55. {
  56. Mkdir ($ _ CacheConfigDir, 0777 );
  57. }
  58. $ _ CacheUrl = $ this-> CacheDir. $ _ CacheConfigDir. $ _ CacheFile;
  59. $ _ CacheConfigUrl = $ this-> CacheDir. $ _ CacheConfigDir. $ _ CacheConfig;
  60. } Else {
  61. $ _ CacheUrl = $ this-> CacheDir. $ _ CacheFile;
  62. $ _ CacheConfigUrl = $ this-> CacheDir. $ _ CacheConfig;
  63. }

  64. If (! File_exists ($ _ CacheUrl ))

  65. {
  66. $ Hanld = @ fopen ($ _ CacheUrl, "w ");
  67. @ Fclose ($ hanld );
  68. }

  69. If (! File_exists ($ _ CacheConfigUrl ))

  70. {
  71. $ Hanld = @ fopen ($ _ CacheConfigUrl, "w ");
  72. @ Fclose ($ hanld );
  73. }

  74. $ This-> CacheConfigFile =$ _ CacheConfigUrl;

  75. $ This-> CacheFileUrl =$ _ CacheUrl;
  76. $ This-> CheckCache ();
  77. Return true;
  78. }

  79. Private function CheckCache ()

  80. {
  81. $ _ FileEditTime = @ filemtime ($ this-> CacheFileUrl );
  82. $ _ TimeOut = $ this-> SetTimeOut;
  83. $ _ IsTimeOut =$ _ FileEditTime $ _ TimeOut;

  84. $ This-> LastUnixTimePoke =$ _ FileEditTime;

  85. $ This-> NextUnixTimePoke =$ _ IsTimeOut;
  86. $ This-> CurrentUnixTimePoke = time ();
  87. $ This-> UnixNowToNext = $ this-> NextUnixTimePoke-time ();

  88. $ This-> LastTimePoke = date ("Y-m-d H: I: s", $ _ FileEditTime );

  89. $ This-> NextTimePoke = date ("Y-m-d H: I: s", $ _ IsTimeOut );
  90. $ This-> CurrentTimePoke = date ("Y-m-d H: I: s", time ());

  91. $ _ TxtInformation = "Last cache timestamp: $ this-> LastUnixTimePoke ";

  92. $ _ TxtInformation. = "current cache timestamp: $ this-> CurrentUnixTimePoke ";
  93. $ _ TxtInformation. = "Next cache timestamp: $ this-> NextUnixTimePoke ";

  94. $ _ TxtInformation. = "Last cache Time: $ this-> LastTimePoke ";

  95. $ _ TxtInformation. = "current cache Time: $ this-> CurrentTimePoke ";
  96. $ _ TxtInformation. = "Next cache Time: $ this-> NextTimePoke ";

  97. $ _ TxtInformation. = "distance from the next cache timestamp: $ this-> UnixNowToNext ";

  98. $ Handl = @ fopen ($ this-> CacheConfigFile, 'w ');

  99. If ($ handl)
  100. {
  101. @ Fwrite ($ handl, $ _ TxtInformation );
  102. @ Fclose ($ handl );
  103. }

  104. If ($ _ IsTimeOut> = time ())

  105. {
  106. $ This-> GetCacheData ();
  107. }
  108. }

  109. Private function implements achefile ()

  110. {
  111. @ Unlink ($ this-> CacheFileUrl );
  112. @ Unlink ($ this-> CacheConfigFile );
  113. }

  114. Public function PutCache ()

  115. {
  116. $ This-> DataLength = ob_get_length ();
  117. $ PutData = ob_get_contents ();
  118. If (! File_exists ($ this-> CacheFileUrl ))
  119. {
  120. $ CreateOK = $ this-> CreateCache ();
  121. If (! $ CreateOK)
  122. {
  123. $ This-> ErrResponse ('errors occurred during cache file check, cache file creation failed ');
  124. Return false;
  125. }
  126. } Else {
  127. $ Hanld = @ fopen ($ this-> CacheFileUrl, "w ");
  128. If ($ hanld)
  129. {

  130. If (@ is_writable ($ this-> CacheFileUrl ))

  131. {
  132. @ Flock ($ hanld, LOCK_EX );
  133. $ _ PutData = @ fwrite ($ hanld, $ PutData );
  134. @ Flock ($ hanld, LOCK_UN );
  135. If (! $ _ PutData)
  136. {
  137. $ This-> ErrResponse ('unable to change the content of the current cached file ');
  138. Return false;
  139. } Else {
  140. @ Fclose ($ hanld );
  141. Return true;
  142. }
  143. } Else {
  144. $ This-> ErrResponse ('writable cached files ');
  145. Return false;
  146. }
  147. } Else {

  148. $ This-> ErrResponse ('fatal error occurred when opening the cached file ');

  149. Return false;
  150. }
  151. }
  152. }

  153. Public function GetCacheData ()

  154. {
  155. $ Hanld = @ fopen ($ this-> CacheFileUrl, "r ");
  156. If ($ hanld)
  157. {
  158. If (@ is_readable ($ this-> CacheFileUrl ))
  159. {
  160. $ This-> CacheToPage = @ file_get_contents ($ this-> CacheFileUrl );
  161. $ IsEmpty = count (file ($ this-> CacheFileUrl); // determines whether the cached file is empty.

  162. If ($ IsEmpty> 0)

  163. {
  164. Echo $ this-> CacheToPage;
  165. @ Fclose ($ hanld );
  166. Ob_end_flush ();
  167. Exit ();
  168. }
  169. } Else {
  170. $ This-> ErrResponse ('failed to read cached file content ');
  171. Return false;
  172. }
  173. } Else {
  174. $ This-> ErrResponse ('failed to open the cache file ');
  175. Return false;
  176. }
  177. }

  178. Private function ErrResponse ($ Msg)

  179. {
  180. Echo $ Msg;
  181. }
  182. }
  183. ?>

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.