A nice PHP file page cache class

Source: Internet
Author: User
Tags echo date

In PHP cache classification database cache, file cache and memory cache, let me give you a detailed introduction of the PHP file cache class implementation code, there is a need to understand the friend can refer to.

Page Cache class

The code is as follows Copy Code

<?php
/*
* Cache class Caches
* Author: More rookie
Instance
*/
/*include ("cache.php");

$cache = new cache (30);
$cache->cachecheck ();

echo Date ("y-m-d h:i:s");

$cache->caching (); */

Class Cache {
Cache Directory
var $cacheRoot = "./cache/";
Cache update time seconds, 0 for no cache
var $cacheLimitTime = 3;
Cache file Name
var $cacheFileName = "";
Cache extension
var $cacheFileExt = "PHP";

/*
* Constructor function
* int $cacheLimitTime Cache update Time
*/
function Cache ($cacheLimitTime) {
if (Intval ($cacheLimitTime))
$this->cachelimittime = $cacheLimitTime;
$this->cachefilename = $this->getcachefilename ();
Ob_start ();
}

/*
* Check if the cache file is within the set update time
* Return: If the contents of the file are returned within the update time, the reverse returns the failure
*/
function Cachecheck () {
if (file_exists ($this->cachefilename)) {
$cTime = $this->getfilecreatetime ($this->cachefilename);
if ($cTime + $this->cachelimittime > Time ()) {
Echo file_get_contents ($this->cachefilename);
Ob_end_flush ();
Exit
}
}
return false;
}

/*
* Cache files or output static
* String $staticFileName static file name (with relative path)
*/
function caching ($staticFileName = "") {
if ($this->cachefilename) {
$cacheContent = Ob_get_contents ();
Echo $cacheContent;
Ob_end_flush ();

if ($staticFileName) {
$this->savefile ($staticFileName, $cacheContent);
}

if ($this->cachelimittime)
$this->savefile ($this->cachefilename, $cacheContent);
}
}

/*
* Clear Cache files
* String $fileName Specify file name (with function) or all (all)
* Return: Clear successful return true, reverse return false
*/
function ClearCache ($fileName = "All") {
if ($fileName! = "All") {
$fileName = $this->cacheroot. Strtoupper (MD5 ($fileName)). ".". $this->cachefileext;
if (file_exists ($fileName)) {
Return @unlink ($fileName);
}else return false;
}
if (Is_dir ($this->cacheroot)) {
if ($dir = @opendir ($this->cacheroot)) {
while ($file = @readdir ($dir)) {
$check = Is_dir ($file);
if (! $check)
@unlink ($this->cacheroot. $file);
}
@closedir ($dir);
return true;
}else{
return false;
}
}else{
return false;
}
}

/*
* Generate cache file names based on current dynamic files
*/
function Getcachefilename () {
Return $this->cacheroot. Strtoupper (MD5 ($_server["Request_uri")). ".". $this->cachefileext;
}

/*
* Cache file Settling time
* String $fileName Cache file name (with relative path)
* Return: File generation time in seconds, file does not exist return 0
*/
function Getfilecreatetime ($fileName) {
if (! Trim ($fileName)) return 0;

if (file_exists ($fileName)) {
Return Intval (Filemtime ($fileName));
}else return 0;
}

/*
* Save File
* String $fileName file name (with relative path)
* String $text file contents
* Return: Successfully returned ture, failed return false
*/
function SaveFile ($fileName, $text) {
if (! $fileName | |! $text) return false;

if ($this->makedir (dirname ($fileName))) {
if ($fp = fopen ($fileName, "w")) {
if (@fwrite ($FP, $text)) {
Fclose ($FP);
return true;
}else {
Fclose ($FP);
return false;
}
}
}
return false;
}

/*
* Continuous construction of the catalogue
* String $dir directory string
* INT $mode Permission number
* Return: Successfully created or all built returns True, other methods return False
*/
function MakeDir ($dir, $mode = "0777") {
if (! $dir) return 0;
$dir = Str_replace ("", "/", $dir);

$mdir = "";
foreach (Explode ("/", $dir) as $val) {
$mdir. = $val. " /";
if ($val = = ":" | | $val = = "." | | Trim ($val) = = "") continue;

if (! file_exists ($mdir)) {
if ([email protected] ($mdir, $mode)) {
return false;
}
}
}
return true;
}
}
?>

The above use is a page cache, each time you visit the page, will first detect the corresponding cache page file exists, if not exist, connect to the database, get data, display the page and also generate a cache page file, so the next time the page file will play a role. (the template engine and some common caching classes on the Web typically have this feature)

Later we introduce a memcache cache, which is a memory cache.

Code

  code is as follows copy code

<?php
$memcache = new Memcache;
$memcache->connect (' localhost ', 11211) or Die ("Could not Connect");
$version = $memcache->getversion ();
echo "Server ' version:". $version. " n ";
$tmp _object = new StdClass;
$tmp _object->str_attr = ' Test ';
$tmp _object->int_attr = 123;
$memcache->set (' key ', $tmp _object, False, or Die ("Failed-to-save data at the server");
Echo Store data in the cache (data would expire in seconds) n ";
$get _result = $memcache->get (' key ');
Echo "Data from the Cache:n";
Var_dump ($get _result);
?

Memcached is a high-performance, distributed memory object caching system for reducing database load and increasing access speed in dynamic applications.

When everyone is tired, go to see the evil GIF animated chart bar, connotation without pornography, pleasure body and mind! Www.530312.net

Related Article

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.