PHP Data Cache

Source: Internet
Author: User
<? PHP // http://www.jb51.net/article/23093.htmfunction set_cache ($ name, $ value) {// set relative or absolute directory, do not add "/" $ cache_dir = "at the end ". /cache "; // set the extension $ cache_extension = ". PHP "; $ cache_str_begin =" <? PHP \ N // cache created at: ". date (" Y-m-d h: I: s ")." \ n "; if (! Is_array ($ value) {$ cache_str_middle = "\ $ name = \" $ value \ ";" ;}else {$ cache_str_middle = "\ $ name = ". arrayeval ($ value ). ";" ;}$ cache_str_end = "\ n?> "; $ Cache_str = $ cache_str_begin. $ cache_str_middle. $ cache_str_end; // cache file path $ cache_file = "$ cache_dir/$ name $ cache_extension"; if ($ fp = @ fopen ($ cache_file, "WB ")) {fwrite ($ FP, $ cache_str); fclose ($ FP); Return true;} else {echo $ cache_file; exit ("can not write to cache files, please check cache directory "); Return false ;}// convert array to a string, from discuz! Function arrayeval ($ array, $ level = 0) {If (! Is_array ($ array) {return "\" $ array \ "" ;}$ Space = ""; for ($ I = 0; $ I <=$ level; $ I ++) {$ space. = "\ t" ;}$ evaluate = "array \ N $ space (\ n"; $ comma = $ space; If (is_array ($ array )) {foreach ($ array as $ key => $ Val) {$ key = is_string ($ key )? "\" ". Addcslashes ($ key," \ "\"). "\" ": $ key; $ val =! Is_array ($ Val )&&(! Preg_match ("/^ \-? [1-9] \ D * $/", $ Val) | strlen ($ Val)> 12 )? "\"". Addcslashes ($ Val ,"\"\\"). "\" ": $ val; If (is_array ($ Val) {$ evaluate. = "$ comma $ key => ". arrayeval ($ Val, $ LEVEL + 1);} else {$ evaluate. = "$ comma $ key => $ Val" ;}$ comma = ", \ N $ space" ;}}$ evaluate. = "\ N $ space)"; return $ evaluate;} $ test_array = array ("6B" => "A \", "B", "C ", array ("C", "D"); $ fileandvarname = "newfile"; // when $ encode_str is generated, the original character content of the string remains unchanged, the system will Prefix the pre-defined characters by adding \ To keep the pre-defined characters in the string. However, when outputting or printing a string, only the pre-defined characters are printed, the \ $ encode_str = json_encode ($ test_array) before the pre-defined characters will not be printed; // The string must be printed as PHP code, pre-Defined Characters in the string will disrupt the program running. Therefore, you must add the escape characters before the original escape characters, enable escape characters before pre-defined characters in string output to output $ addslashes_str = addslashes ($ encode_str ); // addslashes adds \ before the pre-defined characters in the string so that the pre-defined characters can be stored in the string without function. It does not participate in the ECHO stripslashes ($ addslashes_str) program running. // reverse the yundun function, removes the backslash from the string. If there are two backslash lines in a row, remove one and leave one. If there is only one backslash, remove it directly. Echo "<br>"; // you can upload an array object or a JSON string or a josn string. The cached page variable stores the josn string and transmits the array, the cached page variables are stored in arrays, which need to be converted into arrays when using JSON strings. Set_cache ("$ fileandvarname", $ addslashes_str); var_dump ($ addslashes_str); echo "<br/>"; include_once ". /Cache/$ fileandvarname. PHP "; var_dump ($ fileandvarname); echo" <br/> "; $ decode_arr = (array) json_decode ($ fileandvarname); var_dump ($ decode_arr ); echo "<br/>"; // cache another method. Use serialize to convert the serial number of the array into a string and store it in any extension file, use fopen to open and read the string content, and then use unserialize to deserialize the original data $ serialize_str = serialize ($ test_array ); Echo $ serialize_str; // This is the described array, but it is only a string here echo "<br/>"; $ unserialize_str = unserialize ($ serialize_str ); // restores the described data to var_dump ($ unserialize_str); // restores it to $ test_array, And the array structure is not lost.?>

 

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.