Heim  >  Artikel  >  Backend-Entwicklung  >  php数据文件缓存类代码实例

php数据文件缓存类代码实例

WBOY
WBOYOriginal
2016-07-25 08:52:151057Durchsuche
  1. class DataCache
  2. {
  3. /**
  4. * 数组转换
  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. * 写入缓存
  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. * 生成文件缓存
  3. *
  4. * @param string $filePath 缓存文件的保存路径
  5. * @param string $arrayName 存放在缓存文件中的数组名称
  6. * @param array $data 数据
  7. *
  8. * @return boolean
  9. */
  10. DataCache::writeCache($filePath, $arrayName, $data);
复制代码

memcache来缓存数据,这个文件缓存的类:

  1. /**
  2. * 文件缓存类
  3. * 提供文件缓存
  4. */
  5. class Cache_FileCache{
  6. /**
  7. * 设置缓存
  8. * @param $key 缓存的关键字key
  9. * @param $data 缓存的内容
  10. * @param $cacheLife 缓存时间(单位为秒)如果为0 则表示无限时间
  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__,"文件写入出错");
  25. $chmodReslut = @chmod($file,0777) or self::error(__line__,"设定文件权限失败");
  26. if($setReslut && $chmodReslut)
  27. {
  28. return true;
  29. }
  30. else
  31. {
  32. return false;
  33. }
  34. }
  35. }
  36. /**
  37. * 得到缓存数据
  38. * @param $key 缓存的关键字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. * 删除缓存文件
  61. * @param $key 缓存$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. * 清除所有缓存文件
  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. * 出错处理函数
  89. * @param $line 行数
  90. * @param $msg 信息
  91. */
  92. public static function error($line,$msg)
  93. {
  94. die("出错文件:".__file__."/n出错行:$line/n错误信息:$msg");
  95. }
  96. }
  97. ?>
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn