PHP Data cache necessity analysis _ PHP Tutorial

Source: Internet
Author: User
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 ".

  1. /*
  2. // PHP-kcache class v_0.1
  3. // Author: kangzj
  4. // Email: kangzj@mail.bnu.edu.cn
  5. // Blog: http://kangzj.net.ru
  6. // The author does not guarantee that this program has no bugs.
  7. // You are not liable for any problems arising therefrom.
  8. */
  9. Class PHP_kcache {
  10. // Relative or absolute directory, do not add '/' at the end '/'
  11. Var $ cache_dir = './cache ';
  12. Var $ cache_extension = '. cache. php ';
  13. Function set_cache ($ name, $ value ){
  14. $ Pre = "<? N // Cache Created :"
    . Date ('Y-m-d H: I: s'). "n ";
  15. If (! Is_array ($ value )){
  16. $ Value = $ value;
  17. $ Str = "$ name = '$ value ';";
  18. } Else {
  19. $ Str = "$ name =". $ this->
    Arrayeval ($ value ).';';
  20. }
  21. $ End = "n?> ";
  22. Echo $ cache = $ pre. $ str. $ end;
  23. $ Cache_file = $ this->Cache_dir.
    '/'. $ Name. $ this->Cache_extension;
  24. If ($ fp = @ fopen ($ cache_file, 'WB ')){
  25. Fwrite ($ fp, $ cache );
  26. Fclose ($ fp );
  27. Return true;
  28. } Else {
  29. Echo $ cache_file;
  30. Exit ('Can not write to cache files,
    Please check cache directory ');
  31. Return false;
  32. }
  33. }
  34. // Convert array into a string from discuz!
  35. Function arrayeval ($ array, $ level = 0 ){
  36. If (! Is_array ($ array )){
  37. Return "'". $ array ."'";
  38. }
  39. $ Space = '';
  40. For ($ I = 0; $ I<= $ Level; $ I ++ ){
  41. $ Space. = "t ";
  42. }
  43. $ Evaluate = "Arrayn $ space (n ";
  44. $ Comma = $ space;
  45. If (is_array ($ array )){
  46. Foreach ($ array as $ key =>$ Val ){
  47. $ Key = is_string ($ key )? '''. Addcslashes
    ($ Key, ''\ '). ''': $ key;
  48. $ Val =! Is_array ($ val )&&
    (! Preg_match ("/^ -? [1-9] d * $/", $ val)
    | Strlen ($ val)>12 )? '''. Addcslashes
    ($ Val, ''\ '). ''': $ val;
  49. If (is_array ($ val )){
  50. $ Evaluate. = "$ comma $ key => ".
    Arrayeval ($ val, $ level + 1 );
  51. } Else {
  52. $ Evaluate. = "$ comma $ key => $ val ";
  53. }
  54. $ Comma = ", n $ space ";
  55. }
  56. }
  57. $ Evaluate. = "n $ space )";
  58. Return $ evaluate;
  59. }
  60. }


The simplest PHP Data cache class call method:

 
 
  1. include './PHP_kcache_class.PHP';
  2. $pc = new PHP_kcache;
  3. $a = array('a', 'b', 'c');
  4. $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...

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.