Example of a PHP partial cache database return data

Source: Internet
Author: User
Tags flock urlencode

$cache = new Filecache ();
$new _arr = $cache->get (' Gsmcache ');//yourkey is the cache name that you define for each data that you want to cache
if ($new _arr===false) {

$new _arr= "Data returned by the database";

$cache->set (' Gsmcache ', $new _arr,3600);/cache 3,600 seconds

}

<?php
/**
* File Cache class
*
* @copyright blog.itiwin.cn
* @author More
* @package Cache
* @version v0.1
*/
Class Filecache {
/**
* @var string $cachePath cache file directory
* @access Public
*/
Public $cachePath = './';

/**
* Constructor
* @param string $path cache file directory
*/
function __construct ($path = NULL) {
if ($path) {
$this->cachepath = $path;
}
}

/**
* destructor
*/
function __destruct () {
Nothing
}

/**
* Set the value of the key to $key in the cache, and if the entry does not exist, create a new item
* @param string $key key value
* @param mix $var value
* @param int $expire expiration number of seconds
* @param int $flag logo bit
* @return BOOL Returns TRUE if successful, and returns FALSE if it fails.
* @access Public
*/
Public function set ($key, $var, $expire = 36000, $flag = 0) {
$value = serialize ($var);
$timeout = time () + $expire;
$result = safe_file_put_contents ($this->cachepath. UrlEncode ($key). Cache ',
$timeout. ' <<%-==-%>> '. $value);
return $result;
}

/**
* Get the value of the item with key $key in cache
* @param string $key key value
* @return String returns False if the item does not exist
* @access Public
*/
Public function Get ($key) {
$file = $this->cachepath. UrlEncode ($key). Cache ';
if (file_exists ($file)) {
$content = Safe_file_get_contents ($file);
if ($content ===false) {
return false;
}
$tmp = Explode (' <<%-==-%>> ', $content);
$timeout = $tmp [0];
$value = $tmp [1];
if (Time () > $timeout) {

$this->delete ($key);//delete file expired
$result = false;
} else {
$result = Unserialize ($value);
}
} else {
$result = false;
}
return $result;
}

/**
* Empty all items in cache
* @return returns TRUE if successful and FALSE if failed.
* @access Public
*/
Public Function flush () {
$fileList = Filesystem::ls ($this->cachepath,array (), ' ASC ', true);
Return Filesystem::rm ($fileList);
}

/**
* Delete the value of the key in cache that is $key
* @param string $key key value
* @return returns TRUE if successful and FALSE if failed.
* @access Public
*/
Public Function Delete ($key) {
Return Filesystem::rm ($this->cachepath. $key. Cache ");
}
}

if (!function_exists (' safe_file_put_contents ')) {
function safe_file_put_contents ($filename, $content)
{
$fp = fopen ($filename, ' WB ');
if ($fp) {
Flock ($FP, LOCK_EX);
Fwrite ($fp, $content);
Flock ($FP, lock_un);
Fclose ($FP);
return true;
} else {
return false;
}
}
}

if (!function_exists (' safe_file_get_contents ')) {
function Safe_file_get_contents ($filename)
{
$fp = fopen ($filename, ' RB ');
if ($fp) {
Flock ($FP, lock_sh);
Clearstatcache ();
$filesize = FileSize ($filename);
if ($filesize > 0) {
$data = Fread ($fp, $filesize);
}
Flock ($FP, lock_un);
Fclose ($FP);
return $data;
} else {
return false;
}
}
}

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.