PHP Cache for websites. Cache is widely used in actual use, which can reduce access to the server database and improve the running speed. Currently, many CMS content management systems frequently use the cache mechanism to improve the system cache, which is widely used in actual use. This can reduce access to server databases and improve the running speed. Currently, many CMS content management systems frequently use caching mechanisms to improve system operation efficiency. The following is a well-written cache class. you can refer to the caching mechanism and writing method.
The cache. php code is as follows:
[Php]View plaincopy
-
- /*
- Constants that need to be defined in advance:
- _ CachePath _ template cache path
- _ CacheEnable _ whether the automatic Cache mechanism is enabled. if it is undefined or empty, the automatic Cache mechanism is disabled.
- _ ReCacheTime _ automatic cache re-caching interval, in seconds. if it is undefined or empty, automatic cache re-caching is disabled.
- */
-
- Class cache
- {
- Var $ cachefile;
- Var $ cachefilevar;
-
- Function cache ()
- {
- // Generate the name of the Cache group for the current page $ this-> cachefilevar and the name $ this-> cachefile
- // Different dynamic page parameters correspond to different Cache files, but all the Cache files on each dynamic page have the same file name, but the extensions are different.
- $ S = array (".", "/"); $ r = array ("_","");
- $ This-> cachefilevar = str_replace ($ s, $ r, $ _ SERVER ["SCRIPT_NAME"]). "_". $ _ GET [_ ActionVar _];
- $ This-> cachefile = $ this-> cachefilevar. ".". md5 ($ _ SERVER ["REQUEST_URI"]);
- }
-
- // Delete the cache of the current page/module
- Function delete ()
- {
- // Delete the cache of the current page
- $ D = dir (_ CachePath _);
- $ Strlen = strlen ($ this-> cachefilevar );
- // Return all Cache file groups on the current page
- While (false! ==( $ Entry = $ d-> read ()))
- {
- If (substr ($ entry, 0, $ strlen) = $ this-> cachefilevar)
- {
- If (! Unlink (_ CachePath _. "/". $ entry) {echo "the Cache directory cannot be written"; exit ;}
- }
- }
- }
-
- // Determine whether or not the Cache has been cached and whether the Cache is required
- Function check ()
- {
- // If the cache update interval _ ReCacheTime _ is set _
- If (_ ReCacheTime _ + 0> 0)
- {
- // Return the last update time of the current page Cache
- $ Var = @ file (_ CachePath _. "/". $ this-> cachefilevar); $ var = $ var [0];
- // Delete the Cache file if the update time exceeds the update interval
- If (time ()-$ var> _ ReCacheTime _)
- {
- $ This-> delete (); $ ischage = true;
- }
- }
- // Return the Cache of the current page
- $ File = _ CachePath _. "/". $ this-> cachefile;
- // Determine whether the current page Cache exists and whether the Cache function is enabled
- Return (file_exists ($ file) and _ CacheEnable _ and! $ Ischange );
- }
-
- // Read Cache
- Function read ()
- {
- // Return the Cache of the current page
- $ File = _ CachePath _. "/". $ this-> cachefile;
- // Read the content of the Cache file
- If (_ CacheEnable _) return readfile ($ file );
- Else return false;
- }
-
- // Generate Cache
- Function write ($ output)
- {
- // Return the Cache of the current page
- $ File = _ CachePath _. "/". $ this-> cachefile;
- // If the Cache function is enabled
- If (_ CacheEnable _)
- {
- // Write the output content to the Cache file
- $ Fp = @ fopen ($ file, 'w ');
- If (! @ Fputs ($ fp, $ output) {echo "template Cache write failed"; exit ;}
- @ Fclose ($ fp );
- // If the cache update interval _ ReCacheTime _ is set _
- If (_ ReCacheTime _ + 0> 0)
- {
- // Update the last update time of the current page Cache
- $ File = _ CachePath _. "/". $ this-> cachefilevar;
- $ Fp = @ fopen ($ file, 'w ');
- If (! @ Fwrite ($ fp, time () {echo "the Cache directory cannot be written"; exit ;}
- @ Fclose ($ fp );
- }
- }
- }
- }
- ?>
Class usage:
[Php]View plaincopy
-
- Define ("_ CachePath _", "./cache /");
- Define ("_ CacheEnable _", "1 ");
- Define ("_ ReCacheTime _", "43200 ");
- Include ('cache. php ');
- $ Cache = new cache ();
- If ($ cache-> check ())
- {
- $ Template = $ cache-> read ();
- }
- Else
- {
- Ob_start ();
- Ob_implicit_flush (0 );
- ?>
- Page content ....
-
- $ Template = ob_get_contents ();
- $ Cache-> write ($ template );
- }
- ?>
Bytes. Currently, many CMS content management systems frequently use caching mechanisms to improve their systems...