Home  >  Article  >  Backend Development  >  PHP implements file caching function code_PHP tutorial

PHP implements file caching function code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:40:32833browse

  1. /**
  2. * Read or set cache
  3. *
  4. * @access public
  5. * @param string $name Cache name
  6. * @param mixed $value cache content, null delete cache
  7. * @param string $path cache path
  8. * @return mixed
  9. */
  10. function cache($name, $value = , $path = )
  11. {
  12. return false; //During debugging phase, no caching is performed
  13. $path = empty($path) ? ROOT_PATH . /Runtime/Data/ : $path;
  14. $file = $path . $name . .php;
  15. if (empty($value)) {
  16. //Cache does not exist
  17. if (!is_file($file)) {
  18. return false;
  19. }
  20. // Delete cache
  21. If (is_null($value)) {
  22. unlink($file);
  23. return true;
  24. }
  25. $data = include $file;
  26. return $data;
  27. }
  28. $value = var_export($value, true);
  29. $value = "";
  30. return file_put_contents($file, $value);
  31. }
  32. //Function call
  33. cache(name, array(a, b, c)); //The write cache name is the cache name, and the following array is the cache content
  34. cache(name); //Read the cache
  35. cache(name, null); //Delete cache
  36. ?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486194.htmlTechArticle?php /** * Read or set cache* * @access public * @param string $name cache name * @param mixed $value cache content, null delete cache* @param string $path cache path* @re...
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