PHP File Cache class sample sharing, _php tutorial

Source: Internet
Author: User

PHP File Cache class sample sharing,


Copy CodeThe code is as follows:
<?php
/**
* @desc File cache
*/
Class cache{
Const C_file = '/runtime/';
Private $dir = ';
Const EXT = '. TPL ';
Private $filename = ';
Public function __construct ($dir = ") {
$this->dir = $dir;
}
/**
* @desc Set File cache
* @param string $key file name
* @param unkonw $data Cache data
* @param int $expire Expiration Time
*/
Public function set ($key, $data, $expire = 0) {
$this->filename = dirname (__file__). Self::c_file. $this->dir. $key. Self::ext;
if (file_exists ($this->filename)) {
$res = $this->get ($key);
if (MD5 ($res) = = MD5 (Json_encode ($data))) {
return true;
}
}
if (!is_dir (dirname ($this->filename))) {
mkdir (dirname ($this->filename), 0777);
}
$source = fopen ($this->filename, ' w+ ');
Fwrite ($source, Json_encode ($data));
Fclose ($source);
}
/**
* @desc Get files
* @param string $key file name
*/
Public function Get ($key) {
$filename = DirName (__file__). Self::c_file. $this->dir. $key. Self::ext;
if (!file_exists ($this->filename)) {
Return ' cache file no longer exists ';
}else{
$res = file_get_contents ($this->filename);
}
return $res;
}
/**
* @desc Delete Files
* @param string $key file name
*/
Public Function del ($key) {
Unlink ($this->filename);
}
}
$data = Array (' name ' = ' song ', ' Age ' =>20, ' sex ' = ' man ', ' favority ' =>array (' apple ', ' banana ');
$cache = new cache ();
$cache->set (' cache ', $data);
$cache->get (' cache ');
$cache->del (' cache ');

http://www.bkjia.com/PHPjc/950891.html www.bkjia.com true http://www.bkjia.com/PHPjc/950891.html techarticle PHP File Cache class sample sharing, copy code code as follows: PHP/** * @desc File Cache */class cache{const C_file = '/runtime/'; private $dir = '; Const EXT = '. TPL '; Private ...

  • 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.