Example of using file cache in php

Source: Internet
Author: User
Example of using file cache in php

In web development, you can use file caching to greatly relieve the pressure on the database. The following code is an example of using file caching in php.

CacheLayer. php

  1. Class CacheLayer {
  2. Protected $ root = "";
  3. Protected $ cache = "";
  4. Protected $ key = "";
  5. Protected $ life = 0;
  6. Public function _ construct ($ key, $ root = "/cachelayer "){
  7. $ This-> root = $ _ SERVER ["DOCUMENT_ROOT"]. $ root;
  8. $ This-> key = $ key;
  9. }
  10. Public function expired ($ life_span ){
  11. $ This-> life = $ life_span;
  12. $ File = $ this-> root. "/". $ this-> key. ". cachelayer ";
  13. If (is_file ($ file )){
  14. $ Mtime = filemtime ($ file );
  15. Return (time () >=( $ mtime + $ this-> life ));
  16. } Else {
  17. Return true;
  18. }
  19. }
  20. Public function put ($ content ){
  21. $ File = $ this-> root. "/". $ this-> key. ". cachelayer ";
  22. If (! Is_dir (dirname ($ this-> root ))){
  23. Return false;
  24. }
  25. $ This-> delete ();
  26. $ Content = json_encode ($ content );
  27. Return (bool) file_put_contents ($ file, $ content );
  28. }
  29. Public function get (){
  30. $ File = $ this-> root. "/". $ this-> key. ". cachelayer ";
  31. If (is_file ($ file )){
  32. Return json_decode (file_get_contents ($ file), true );
  33. }
  34. Return array ();
  35. }
  36. Public function delete (){
  37. $ File = $ this-> root. "/". $ this-> key. ". cachelayer ";
  38. If (is_file ($ file )){
  39. Unlink ($ file );
  40. Return true;
  41. }
  42. Return false;
  43. }
  44. }


Example. php

  1. // Load the cachelayer and the database connection (db connection is optional)
  2. Require_once "CacheLayer. php ";
  3. Require_once "db_connection.php ";
  4. // Create a instance of the cachelayer
  5. $ Cl_nav = new CacheLayer ("navigation ");
  6. // Check to see if the cache is expired (60*10 = 10 minutes)
  7. If ($ cl_nav-> expired (60*10 )){
  8. Echo "Cache DOESN'T exist or is expired. Rebuilding...
    ";
  9. // If the cache is expired rebuild it
  10. $ Result = mysql_query ("select id, title from navigation ");
  11. $ New_cache = array ();
  12. While ($ row = mysql_fetch_assoc ($ result )){
  13. $ New_cache [] = $ row;
  14. }
  15. // Save the array into the cache
  16. $ Cl_nav-> put ($ new_cache );
  17. }
  18. Echo "Loading from cache...
    ";
  19. // Get the cache
  20. $ Cache = $ cl_nav-> get ();
  21. // Display the cache
  22. Foreach ($ cache as $ row ){
  23. $ Id = $ row ["id"];
  24. $ Title = $ row ["title"];
  25. Echo "$ title
    ";
  26. }


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.