PHP File cache Data _php Tutorial

Source: Internet
Author: User
In the Internet cafes to look at the time, due to the presence of the movie in the page search function (user input)

This function because can not be made static, then only can be dynamic, with the dynamic time to the database, server pressure brought a great test

So we can only use the way the data is cached.

The data cache forms include:

1, the data cache to memory, I believe everyone this will think of memcached.memcached is a high-performance distributed memory cache server. The general purpose is to reduce the number of database accesses by caching database query results to improve the speed and scalability of dynamic Web applications.

2. Use files to cache data. Save the data to a file, save it in key=>value form, and key refers to the file name. This place has to ensure that key uniqueness

Set the cache time for the file, and if it is obsolete, get the data from the database and save it to a file.

Here is a file cache class:

1. Cache data

2. Get the data

3. Determine if the cached data exists

4. Delete a cached data

5. Purge Stale cache data

6, clear the cached data

Class inc_filecache{

Private $cacheTime = 3600; Default Cache time

Private $cacheDir = Cache_dir; Cache absolute Path

Private $MD 5 = true; Whether the key is encrypted

Private $suffix = ". php"; Set file suffix

Public function __construct ($config) {

if (Is_array ($config)) {

foreach ($config as $key = = $val) {

$this $key = $val;

}

}

}

Setting up the cache

Public function set ($key, $val, $leftTime =null) {

$key = $this->md5? MD5 ($key): $key;

$leftTime = $leftTime? $leftTime: $this->cachetime;

!file_exists ($this->cachedir) && mkdir ($this->cachedir,0777);

$file = $this->cachedir. ' /'. $key. $this->suffix;

$val = serialize ($val);

@file_put_contents ($file, $val) or $this->error (__line__, "file write Failed");

@chmod ($file, 0777) or $this->error (__line__, "Set file permissions failed");

@touch ($file, Time () + $leftTime) or $this->error (__line__, "failed to change file times");

}

Get Cached

Public function Get ($key) {

$this->clear ();

if ($this->_isset ($key)) {

$key _md5 = $this->md5? MD5 ($key): $key;

$file = $this->cachedir. ' /'. $key _md5. $this->suffix;

$val = file_get_contents ($file);

Return Unserialize ($val);

}

return null;

}

Determine if the question is valid

Public Function _isset ($key) {

$key = $this->md5? MD5 ($key): $key;

$file = $this->cachedir. ' /'. $key. $this->suffix;

if (file_exists ($file)) {

if (@filemtime ($file) >= time ()) {

return true;

}else{

@unlink ($file);

return false;

}

}

return false;

}

deleting files

Public Function _unset ($key) {

if ($this->_isset ($key)) {

$key _md5 = $this->md5? MD5 ($key): $key;

$file = $this->cachedir. ' /'. $key _md5. $this->suffix;

Return @unlink ($file);

}

return false;

}

Purge Expired cache files

Public Function Clear () {

$files = Scandir ($this->cachedir);

foreach ($files as $val) {

if (@filemtime ($this->cachedir. /". $val) < time ()) {

@unlink ($this->cachedir. " /". $val);

}

}

}

Clear all cache files

Public Function Clear_all () {

$files = Scandir ($this->cachedir);

foreach ($files as $val) {

@unlink ($this->cachedir. " /". $val);

}

}

Private Function Error ($line, $msg) {

Die ("Error File:". __file__. " /N Error Line: $line/N error message: $msg ");

}

}

The calling method in the page is as follows:

$cacheFile = new Inc_filecache (Array (' CacheTime ' =>60, ' suffix ' = '. php '));

Get the hit list of the movie

$where = "where pid=75";

$moviehotModel = $this->getm (' moviehot ');

$moviehotCount = $moviehotModel->getcount ($where);

if (! $cacheFile->_isset ($where. $moviehotCount. ' Moviehot ')) {

$moviehotResult = $moviehotModel->getlist ("WHERE pid=75", ' 0,10 ', "desc");

if (count ($moviehotResult) > 0) {

$cacheFile->set ($where. $moviehotCount. ' Moviehot ', $moviehotResult);

}

}else{

$moviehotResult = $cacheFile->get ($where. $moviehotCount. ' Moviehot ');

}

$this->tpl[' moviehotresult ') = $moviehotResult;

Everybody, if there's any good. File cache code can be shared

Excerpt from ms_x0828 's column

http://www.bkjia.com/PHPjc/478509.html www.bkjia.com true http://www.bkjia.com/PHPjc/478509.html techarticle in the Internet cafes to look at the time, due to the presence of the movie in the page search function (user input) This function because can not be made static, then can only dynamic, with dynamic time will be on the data ...

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