Examples of using file caching in PHP

Source: Internet
Author: User

In web development, the pressure on the database can be greatly mitigated by the file cache. 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. }
Copy Code

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 (ten * = 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. }
Copy Code
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.