PHP data File cache class code instance

Source: Internet
Author: User
  1. Class Datacache
  2. {
  3. /**
  4. * Array Conversion
  5. *
  6. * @param array $array
  7. * @param string $arrayName
  8. * @param array $level
  9. *
  10. * @return String
  11. */
  12. Private Function Arrayeval ($array, $arrayName = ", $level = 0)
  13. {
  14. $space = Str_repeat ("T", $level);
  15. if (Emptyempty ($arrayName))
  16. {
  17. $evaluate = "Arrayn$space (n";
  18. }
  19. Else
  20. {
  21. $evaluate = "${$arrayName} = Arrayn$space (n";
  22. }
  23. $space 2 = str_repeat ("T", $level + 1);
  24. $comma = $space 2;
  25. if (!emptyempty ($array))
  26. {
  27. foreach ($array as $key = $val)
  28. {
  29. $key = is_string ($key)? ''' . Addcslashes ($key, "\"). ": $key;
  30. $val =!is_array ($val) && (!preg_match ('/^-?[ 1-9]d*$/', $val) | | strlen ($val) > 12)? ''' . Addcslashes ($val, "\"). ": $val;
  31. if (Is_array ($val))
  32. {
  33. $evaluate. = "$comma $key =". Arrayeval ($val, ", $level + 1);
  34. }
  35. Else
  36. {
  37. $evaluate. = "$comma $key = $val";
  38. }
  39. $comma = ", N$space2";
  40. }
  41. }
  42. $evaluate. = "N$space)";
  43. Finally, we need a ";"
  44. if ($level = = 0)
  45. {
  46. $evaluate. = ";";
  47. }
  48. return $evaluate;
  49. }
  50. /**
  51. * Write Cache
  52. *
  53. * @param string $path
  54. * @param string $arrayName
  55. * @param array $data
  56. *
  57. * @return Boolean
  58. */
  59. public static function Writecache ($path, $arrayName, $data)
  60. {
  61. if ($handle = fopen ($path, ' w+ '))
  62. {
  63. $data = Self::arrayeval ($data, $arrayName);
  64. $dataConvert = "
  65. Flock ($handle, LOCK_EX);
  66. $rs = fputs ($handle, $dataConvert);
  67. Flock ($handle, lock_un);
  68. Fclose ($handle);
  69. if ($rs!== false)
  70. {
  71. return true;
  72. }
  73. }
  74. return false;
  75. }
  76. }
  77. ?>
Copy Code

Call Method:

    1. /**
    2. * Generate file Cache
    3. *
    4. * @param string $filePath save path to cache file
    5. * @param string $arrayName The name of the array stored in the cache file
    6. * @param array $data data
    7. *
    8. * @return Boolean
    9. */
    10. Datacache::writecache ($filePath, $arrayName, $data);
Copy Code

Memcache to cache the data, this file caches the class:

  1. /**
  2. * File Cache class
  3. * Provide file cache
  4. */
  5. Class cache_filecache{
  6. /**
  7. * Set Cache
  8. * @param $key Cached keyword key
  9. * @param $data Cached content
  10. * @param $cacheLife Cache time (in seconds) if 0 means unlimited time
  11. * @return Bool
  12. */
  13. public static function Setcache ($key, $data, $cacheLife)
  14. {
  15. if (file_exists (__site_file_cache))
  16. {
  17. @ $file = __site_file_cache. "/" . $key. ". PHP ";
  18. $cache = Array ();
  19. $time = __sys_time;
  20. $cache [' content '] = $data;
  21. $cache [' expire '] = $cacheLife = = = 0? 0: $time + $cacheLife;
  22. $cache [' mtime '] = $time;
  23. $cache = serialize ($cache);
  24. $setReslut = @file_put_contents ($file, $cache) or Self::error (__line__, "File write error");
  25. $chmodReslut = @chmod ($file, 0777) or Self::error (__line__, "Set file permissions failed");
  26. if ($setReslut && $chmodReslut)
  27. {
  28. return true;
  29. }
  30. Else
  31. {
  32. return false;
  33. }
  34. }
  35. }
  36. /**
  37. * Get Cached data
  38. * @param $key Cached keyword key
  39. * @return Array
  40. */
  41. public static function GetCache ($key)
  42. {
  43. @ $file = __site_file_cache. "/" . $key. ". PHP ";
  44. if (file_exists ($file))
  45. {
  46. $data = @file_get_contents ($file);
  47. $data = Unserialize ($data);
  48. if ($data [' Expire ']==0 | | $data [' expire '] > __sys_time)
  49. {
  50. return $data [' content '];
  51. }
  52. Else
  53. {
  54. Unlink ($file);
  55. return Array ();
  56. }
  57. }
  58. }
  59. /**
  60. * Delete cache files
  61. * @param $key Cache $key
  62. * @return Bool
  63. */
  64. public static function Delcache ($key)
  65. {
  66. if (@unlink (__site_file_cache. /". $key.". PHP "))
  67. {
  68. return true;
  69. }
  70. Else
  71. {
  72. return false;
  73. }
  74. }
  75. /**
  76. * Clear All cache files
  77. * @return Bool
  78. */
  79. public static function Clearallcache ()
  80. {
  81. $files = Scandir (__site_file_cache);
  82. foreach ($files as $val)
  83. {
  84. @unlink (__site_file_cache. " /". $val);
  85. }
  86. }
  87. /**
  88. * Error handling function
  89. * Number of lines @param $line
  90. * @param $msg Information
  91. */
  92. public static function error ($line, $msg)
  93. {
  94. Die ("Error File:". __file__. " /N Error Line: $line/N error message: $msg ");
  95. }
  96. }
  97. ?>
Copy Code
  • 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.