PHP Database Cache: PHP File cache data

Source: Internet
Author: User
Tags config key md5 memcached php file php database file permissions

When doing a look at the Internet café, because the page has a movie search function (user input)
This function can not be made static, then only dynamic, with the dynamic of the database, server pressure will bring a great test
So there's only a way to cache the data.
The forms of data caching 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 purpose of the general use is to reduce the number of database visits by caching database query results to improve the speed and scalability of dynamic Web applications.
2, the use of files to cache data. Save the data to a file, save it in key=>value form, key refers to the filename. This place has to be a guarantee of 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.
The following is a file caching class:
1. Cache data
2. Get the data
3, to determine whether the cache data exist
4. Delete a cached data
5. Erase obsolete Cached 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 to encrypt keys
Private $suffix = ". php"; Set file suffix
Public function __construct ($config) {
if (Is_array ($config)) {
foreach ($config as $key => $val) {
$this-> $key = $val;
}
}
}
Setting up caching
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__, "Change file times failed");
}
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); This article links http://www.cxybl.com/html/wlbc/Php/20121222/35116.html

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.