PHP self-use data cache

Source: Internet
Author: User

I just changed my job and now I have nothing to do. I wrote a data cache class.

You can cache arrays, characters, objects, and so on. The execution efficiency has not been tested yet. Release it first.

Example:

 

* @ Example

* Require 'mycache. Class. php ';

* $ MC = new mycache ("./test/cache ");

* $ A = "Hello world111 ";

* $ Mc-> set ("SS", $ );

* $ Mc-> set ("DD", $ MC );

* $ Mc-> set ("2 s", $ );

* Echo $ Mc-> get ("SS ");

* $ Mc-> Delete ("2 S ");

* $ Mc-> edit ("SS", "hello ");

* Echo $ Mc-> get_type ("DD ");

* Echo "<PRE> ";

* Print_r ($ C );

 

<? Php <br/>/** <br/> * Self-Use PHP cache class <br/> * function, which includes variables, array Write File <br/> * the file name is the cache variable name <br/> * @ example <br/> * require 'mycache. class. PHP '; <br/> * $ MC = new mycache (". /test/cache "); <br/> * $ A =" Hello world111 "; <br/> * $ Mc-> set (" SS ", $ ); <br/> * $ Mc-> set ("DD", $ MC); <br/> * $ Mc-> set ("2 s", $ ); <br/> * echo $ Mc-> get ("SS"); <br/> * echo $ Mc-> get_type ("DD "); <br/> * $ c = $ Mc-> get_all (); <br/> * echo "<PRE> "; <Br/> * print_r ($ C); <br/> * @ Author: Bruce Wang 2010-03-08 <br/> * @ name: mycache. class. php <br/> */<br/> class mycache {<br/> private $ cachepath; // cache directory <br/> private $ lifetime; // cache validity period <br/> private $ pathseq; // delimiter of the current system <br/>/** <br/> * constructor, initialization <br/> * @ Param: String 'cachepath ', 'lifetime' <br/> * @ return NULL <br/> */</P> <p> function mycache ($ cachepath = "datacache", $ lifetime = 0) {<br/> // Determine whether the cached folder exists. Otherwise, create <br/> $ this-> create_dirs ($ cachepath); </P> <p> $ this-> pathseq = directory_separator; <br/> $ this-> lifetime = $ lifetime; <br/> $ this-> cachepath = realpath ($ cachepath ); <br/>}</P> <p>/** <br/> * Create a cache file and write data. <br/> * @ Param: string $ var variable name <br/> * @ Param: mixed $ data variable data <br/> * @ return NULL <br/> */<br/> function set ($ var, $ data) {<br/> $ filefullname = $ this-> get_full_name ($ var); <br/> If (! File_exists ($ filefullname) {<br/> $ this-> write_cache_file ($ var, $ data ); <br/>}< br/> else {<br/> If (! $ This-> is_file_live ($ var) {// not valid <br/> unlink ($ filefullname ); // Delete cache files that are not in the validity period <br/> $ this-> write_cache_file ($ var, $ data ); <br/>}< br/> else {<br/> echo ('cache data of the same name '. $ var. 'Already exists. Please change the cache name <br/> '); <br/>}</P> <p>/** <br/> * Get cache <br/> * @ Param string $ var // cache name <br/> * @ return mixed <br/> */<br/> function get ($ var) {<br/> return $ this-> read_cache_file ($ var); <br/>}</P> <p>/** <br/> * slow down Storage Data Type <br/> * @ Param string $ var <br/> * @ return string <br/> */</P> <p> function get_type ($ var) {<br/> $ DATA = $ this-> get ($ var); <br/> return GetType ($ data ); <br/>}</P> <p>/** <br/> * display all cache data that has not expired <br/> * @ Param null <br/> * @ return array <br/> */<br/> function get_all () {<br/> $ DH = opendir ($ this-> cachepath); <br/> $ filearr = array (); <br/> while ($ file = readdir ($ DH) {<br/> if ($ file! = '.' & $ File! = '..' & (Filetype ($ this-> cachepath. $ this-> pathseq. $ file )! = 'Dir') {<br/> $ file = substr ($ file, 0,-4 ); <br/> if ($ this-> is_file_live ($ file) {<br/> // $ filecontent = $ this-> get ($ file ); <br/> $ type = $ this-> get_type ($ file); <br/> array_push ($ filearr, array ("name" => $ file, "type" = >$ type); <br/>}< br/> return $ filearr; <br/>}</P> <p>/** <br/> * Create a cache file <br/> * @ Param $ var <br/> * @ Param $ data <br/> * @ return bool <br/> */<br/> private function write_cache _ File ($ var, $ data) {<br/> $ STR = serialize ($ data); <br/> $ filefullname = $ this-> get_full_name ($ var ); <br/> If (file_put_contents ($ filefullname, $ Str) {<br/> return true; <br/>}< br/> else {<br/> return false; <br/>}</P> <p>/** <br/> * read cache data <br/> * @ Param string $ var <br /> * @ return mixed <br/> */<br/> private function read_cache_file ($ var) {<br/> $ fullname = $ this-> get_full_name ($ var); <br/> If (Fi Le_exists ($ fullname) {<br/> If (! $ This-> is_file_live ($ var) {<br/> unlink ($ fullname); <br/> echo ('cached data '. $ var. 'Expired <br/> '); <br/>}< br/> else {<br/> $ STR = file_get_contents ($ fullname ); <br/> return unserialize ($ Str); <br/>}< br/> else {<br/> echo ('cached data '. $ var. 'does not exist. <br/> '); <br/>}</P> <p>/** <br/> * determine whether the cached file is within its validity period <br/> * @ Param: string $ var variable name <br/> * @ return bool <br/> */<br/> function is_file_live ($ var) {<br/> $ filef Ullname = $ this-> get_full_name ($ var); <br/> If (file_exists ($ filefullname) {<br/> if ($ this-> lifetime! = 0) {<br/> If (Time ()-filemtime ($ filefullname) <$ this-> lifetime) {<br/> return true; <br/>}< br/> else {<br/> return false; <br/>}< br/> else {<br/> return true; <br/>}< br/> else {<br/> return false; <br/>}</P> <p>/** <br/> * obtain the full path of the cached file <br/> * @ Param: string $ var // variable name <br/> * @ return string <br/> */</P> <p> function get_full_name ($ var) {<br/> $ filename = $ this-> vartofilename ($ var); <br /> Return $ this-> cachepath. $ this-> pathseq. $ filename; <br/>}</P> <p>/** <br/> * Name of the file for variable conversion <br/> * @ Param: string $ var // variable name <br/> * @ return string <br/> */<br/> private function vartofilename ($ var) {<br/> return $ var. ". CA "; <br/>}</P> <p>/** <br/> * Create a folder Based on the path <br/> * @ Param strin $ PATH <br/> *@ return bool | null <br/> */<br/> private function create_dirs ($ PATH) {<br/> if ($ Path = ''|! Isset ($ PATH) {<br/> return false; <br/>}< br/> else {<br/> If (! File_exists ($ PATH) {<br/> $ this-> create_dirs (dirname ($ PATH )); // iteration <br/> try {<br/> mkdir ($ path, 0777); // create a folder <br/>}< br/> catch (exception $ E) {<br/> echo $ e-> getmessage (); exit; <br/>}</P> <p >}< br/>}</P> <p>} 

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.