Simple php Cache class sharing php Cache mechanism

Source: Internet
Author: User
Tags flock
This article mainly introduces a php Cache class that uses md5 encryption for file names. The code is as follows:
Class Cache
{
Private $ dir = "data/cache/"; // defines the cache Directory
Private $ key = 'c _ a_sss '; // file name md5 encryption key

Function set_dir ($ dirpath)
{
$ This-> dir = $ dirpath;
$ This-> make_dir ($ this-> dir );
}
Function read ($ key, $ minutes = 1)
{
$ Filename = $ this-> get_filename ($ key );
If ($ datas = @ file_get_contents ($ filename ))
{
$ Datas = unserialize ($ datas );
If (time ()-$ datas ['Time'] <$ minutes * 60)
{
Return $ datas ['data'];
}
}
Return false;
}

Function write ($ key, $ data)
{
$ Filename = $ this-> get_filename ($ key );
If ($ handle = fopen ($ filename, 'W + '))
{
$ Datas = array ('data' => $ data, 'Time' => time ());
Flock ($ handle, LOCK_EX );
$ Rs = fputs ($ handle, serialize ($ datas ));
Flock ($ handle, LOCK_UN );
Fclose ($ handle );
If ($ rs! = False) {return true ;}
}
Return false;
}
Function clear_all ()
{
$ Dir = $ this-> dir;
$ This-> del_file ($ dir );
}

Private function get_filename ($ key)
{
Return $ this-> dir. $ key. '_'. md5 ($ key. $ this-> key );
}
Private function make_dir ($ path)
{
If (! File_exists ($ path ))
{
$ This-> make_dir (dirname ($ path ));
If (! Mkdir ($ path, 0777 ))
Die ('unable to create cache folders '. $ path );
}
}
Private function del_file ($ dir)
{
If (is_dir ($ dir ))
{
$ Dh = opendir ($ dir); // open the Directory // list all files in the directory and remove ..
While (false! ==( $ File = readdir ($ dh ))){
If ($ file! = "." & $ File! = ".."){
$ Fullpath = $ dir. "/". $ file;
If (! Is_dir ($ fullpath )){
Unlink ($ fullpath );
} Else {
$ This-> del_file ($ fullpath );
}
}
}
Closedir ($ dh );
}
}
}

$ Cache = new cache ();
$ Cache-> set_dir ('data/cache_dir /');
$ Data = $ cache-> read ('sys ', 1 );
If (empty ($ data ))
{
$ Data = array ('a' => 1111, 'BB '=> 2222, 'date' => date ('Y-m-d H: I: s '));
$ Cache-> write ('sys ', $ data );
}
Print_r ($ 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.