Home  >  Article  >  Backend Development  >  PHP data file cache class code example

PHP data file cache class code example

WBOY
WBOYOriginal
2016-07-25 08:52:151109browse
  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. $space2 = str_repeat("t", $level + 1);
  24. $comma = $space2;
  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. // 最后才需要一个“;”
  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. ?>
复制代码

调用方法:

  1. /**
  2. * Generate file cache
  3. *
  4. * @param string $filePath The save path of the 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);
复制代码

memcache is used to cache data. The class of this file cache:

  1. /**
  2. * File cache class
  3. * Provides file caching
  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 (unit: seconds). If it is 0, it 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 writing error");
  25. $chmodReslut = @chmod($file,0777) or self::error(__line__," Failed to set file permissions");
  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. * @param $line line number
  90. * @param $msg information
  91. */
  92. public static function error($line,$msg)
  93. {
  94. die("Error file: ".__file__."/nError line: $line/nError message: $msg");
  95. }
  96. }
  97. ?>
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn