PHP Data Cache class Necessity Analysis _php Tutorial

Source: Internet
Author: User
Everyone through the

If the traffic is large, it will be a huge burden on the database, so it is very necessary to do a good job of PHP data cache (cache) for the content of infrequently changing, I have made a simple PHP data caching class, I hope to help you.

The idea is this:

For the general variable, the variable into the PHP language format, write to the file, the time as long as the include this file is equivalent to loading the PHP data cache class;

For the variant of the array type, the array is converted to the PHP language definition array string, written to the file, as long as the include is equivalent to loading the cache;

PHP Data cache class time control, by obtaining the cache file creation time and the current time to compare, if not to update time, directly read the cache, if the update time, query the database, return data, and then update the cache. (not yet implemented)

Here is my Php-kcache class (Php_kcache_class. PHP):

Note: If the string is cached, write the escape character one more ", that is," n "to 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 does not have a bug, for the use of this program
  7. Without any responsibility for any problems arising.
  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 at:"
    . 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. Converts an 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 method to invoke the PHP data cache class:

 
  
  
  1. Include './php_kcache_class. PHP ';
  2. $ PC new php_kcache;
  3. $ a Array(' A ', ' B ', ' C ');
  4. $pc- > Set_cache (' A ', addslashes ($a));

http://www.bkjia.com/PHPjc/445945.html www.bkjia.com true http://www.bkjia.com/PHPjc/445945.html techarticle everyone through to if the volume of access to the database will be a great burden, so for the change of the content of the PHP data cache is very necessary, I made a ...

  • 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.