search
HomePHP LibrariesOther librariesPHP class library for file caching
PHP class library for file caching
<?php
class CacheLayer{
  protected $root = "";
  protected $cache = "";
  protected $key = "";
  protected $life = 0;
  public function __construct($key, $root = "/cachelayer"){
    $this->root = $_SERVER["DOCUMENT_ROOT"].$root;
    $this->key = $key;
  }
  public function expired($life_span){
    $this->life = $life_span;
    $file = $this->root."/".$this->key.".cachelayer";
    if(is_file($file)){
      $mtime = filemtime($file);
      return (time() >= ($mtime + $this->life));
    }else{
      return true;
    }
  }
  public function put($content){
    $file = $this->root."/".$this->key.".cachelayer";
    if(!is_dir(dirname($this->root))){
      return false;
    }
    $this->delete();
    $content = json_encode($content);
    return (bool)file_put_contents($file, $content);
  }
  public function get(){
    $file = $this->root."/".$this->key.".cachelayer";
    if(is_file($file)){
      return json_decode(file_get_contents($file), true);
    }
    return array();
  }
  public function delete(){
    $file = $this->root."/".$this->key.".cachelayer";
    if(is_file($file)){
      unlink($file);
      return true;
    }
    return false;
  }
}
?>

This is a very useful PHP caching library. Friends who need it can download and use it. It can greatly relieve the pressure on the database through file caching

Disclaimer

All resources on this site are contributed by netizens or reprinted by major download sites. Please check the integrity of the software yourself! All resources on this site are for learning reference only. Please do not use them for commercial purposes. Otherwise, you will be responsible for all consequences! If there is any infringement, please contact us to delete it. Contact information: admin@php.cn

Related Article

PHP caching class for your own use, your own PHP caching class_PHP tutorialPHP caching class for your own use, your own PHP caching class_PHP tutorial

12Jul2016

My own PHP caching class, my own PHP caching class. PHP cache class for your own use, your own PHP cache class?php/** * Cache class, data implementation, output cache* @author ZhouHr 2012-11-09 http://www.ketann.com * @copyright version 0.1 */ class C

How Do I Link Static Libraries That Depend on Other Static Libraries?How Do I Link Static Libraries That Depend on Other Static Libraries?

13Dec2024

Linking Static Libraries to Other Static Libraries: A Comprehensive ApproachStatic libraries provide a convenient mechanism to package reusable...

PHP implements paging class suitable for file content operations, _PHP tutorialPHP implements paging class suitable for file content operations, _PHP tutorial

12Jul2016

PHP implements paging classes suitable for file content operations. PHP implements the paging class suitable for file content operations. This article shares an example of PHP implementing the paging class for file content operations. It is emphasized that it is only for file operations for your reference.

How to Silence TensorFlow\'s Debugging Output?How to Silence TensorFlow\'s Debugging Output?

28Oct2024

Suppression of Tensorflow Debugging OutputTensorflow prints extensive information about loaded libraries, found devices, and other debugging data...

How Does jQuery Simplify DOM Manipulation for Web Developers?How Does jQuery Simplify DOM Manipulation for Web Developers?

03Jan2025

Overflow: Hidden and Expansion of HeightjQuery distinguishes itself from other JavaScript libraries through its cross-platform compatibility and...

Which native Java image processing library is right for you?Which native Java image processing library is right for you?

30Oct2024

Native Java Image Processing Libraries for High-Quality ResultsAs you have encountered limitations with ImageMagick and JAI, let's explore other...

See all articles