An extremely simple PHP Cache class code and an extremely powerful php Cache _ PHP Tutorial

Source: Internet
Author: User
A simple PHP Cache class code and php Cache. A simple PHP Cache class code, and a php Cache contains a lot of information about the PHP Cache class on the internet. However, this class should meet the requirements of functions I have seen, however, it is a simple PHP Cache code and a php Cache code.

There is a lot of information about the PHP Cache class on the Internet, but this class should be a simple one that I have seen to meet the requirements. Let's just look at the code!
Instructions for use:
1. instantiation
$ Cache = new Cache ();
2. set cache time and cache Directory
$ Cache = new Cache (60, '/any_other_path /');
The first parameter is the cache seconds, and the second parameter is the cache path, which can be configured as needed.
By default, the cache duration is 3600 seconds, and the cache directory is cache/
3. read cache
$ Value = $ cache-> get ('data _ key ');
4. write cache
$ Value = $ cache-> put ('data _ key', 'data _ value ');
Complete instance:

$ Cache = new Cache (); // read key value $ key data from the cache $ values = $ cache-> get ($ key ); // if no data is cached if ($ values = false) {// insert code here... // write $ key Data $ cache-> put ($ key, $ values);} else {// insert code here ...}

Cache. class. php

<?php class Cache { private $cache_path;//path for the cache private $cache_expire;//seconds that the cache expires  //cache constructor, optional expiring time and cache path public function Cache($exp_time=3600,$path="cache/"){ $this->cache_expire=$exp_time; $this->cache_path=$path; }  //returns the filename for the cache private function fileName($key){ return $this->cache_path.md5($key); }  //creates new cache files with the given data, $key== name of the cache, data the info/values to store public function put($key, $data){ $values = serialize($data); $filename = $this->fileName($key); $file = fopen($filename, 'w'); if ($file){//able to create the file fwrite($file, $values); fclose($file); } else return false; }  //returns cache for the given key public function get($key){ $filename = $this->fileName($key); if (!file_exists($filename) || !is_readable($filename)){//can't read the cache return false; } if ( time() < (filemtime($filename) + $this->cache_expire) ) {//cache for the key not expired $file = fopen($filename, "r");// read data file if ($file){//able to open the file $data = fread($file, filesize($filename)); fclose($file); return unserialize($data);//return the values } else return false; } else return false;//was expired you need to create new } } ?> 

I believe everyone will love this concise php Cache code and hope it will be helpful for everyone's learning.

I have a lot of information about the PHP Cache class on the idea website, but this class should be a simple one that I have seen to meet the requirements of the function...

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.