Analysis on necessity of PHP Data caching. If you have a large access volume, it will cause a great burden on the database. Therefore, it is necessary to cache PHP Data for infrequently changed content, I made an example.
If the traffic volume is large, it will cause a great burden on the database. Therefore, it is necessary to cache PHP Data for infrequently changed content, I made a simple PHP Data cache class, hoping to help you.
The idea is as follows:
For a common variable, convert the variable to the PHP format and write it to the file. when you include the file, it is equivalent to loading the PHP Data cache class;
For array-type variables, convert the array into a PHP-defined array string and write it to the file. if you use include, it is equivalent to loading the cache;
PHP Data cache time control, by obtaining the cache file creation time and the current time for comparison, if the update time is not reached, directly read the cache, if the update time is reached, query the database, return data, and update the cache. (Not implemented yet)
Below is my PHP-kcache class (PHP_kcache_class.PHP ):
Note: If it is a cache string, write one more ''as the escape character, that is," n "must be written as" \ n ".
- /*
- // PHP-kcache class v_0.1
- // Author: kangzj
- // Email: kangzj@mail.bnu.edu.cn
- // Blog: http://kangzj.net.ru
- // The author does not guarantee that this program has no bugs.
- // You are not liable for any problems arising therefrom.
- */
- Class PHP_kcache {
- // Relative or absolute directory, do not add '/' at the end '/'
- Var $ cache_dir = './cache ';
- Var $ cache_extension = '. cache. php ';
- Function set_cache ($ name, $ value ){
- $ Pre = "<? N // Cache Created :"
. Date ('Y-m-d H: I: s'). "n ";
- If (! Is_array ($ value )){
- $ Value = $ value;
- $ Str = "$ name = '$ value ';";
- } Else {
- $ Str = "$ name =". $ this->
Arrayeval ($ value ).';';
- }
- $ End = "n?> ";
- Echo $ cache = $ pre. $ str. $ end;
- $ Cache_file = $ this->Cache_dir.
'/'. $ Name. $ this->Cache_extension;
- If ($ fp = @ fopen ($ cache_file, 'WB ')){
- Fwrite ($ fp, $ cache );
- Fclose ($ fp );
- Return true;
- } Else {
- Echo $ cache_file;
- Exit ('Can not write to cache files,
Please check cache directory ');
- Return false;
- }
- }
- // Convert array into 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 = "Arrayn $ 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;
- }
- }
The simplest PHP Data cache class call method:
- include './PHP_kcache_class.PHP';
- $pc = new PHP_kcache;
- $a = array('a', 'b', 'c');
- $pc->set_cache('a', addslashes($a));
If the volume of traffic is large, it will cause a great burden on the database. Therefore, it is necessary to cache PHP Data for infrequently changed content, I made...