当前位置:首页 > PHP教程 > php高级应用 > 列表

php 部分缓存数据库返回数据的例子

发布:smiling 来源: PHP粉丝网  添加日期:2016-01-20 16:18:29 浏览: 评论:0 

php粉丝教程小编为各位介绍一篇php 部分缓存数据库返回数据的例子,这个例子其实是非常的实用了,希望能够帮助到大家.

  1. $cache = new FileCache(); 
  2. $new_arr = $cache->get('gsmcache');//yourkey是你为每一个要缓存的数据定义的缓存名字 
  3. if ($new_arr===false) { 
  4.  
  5. $new_arr="数据库返回的数据"; 
  6.  
  7. $cache->set('gsmcache',$new_arr,3600);//缓存3600秒 
  8.  
  9. } 
  10.  
  11. <?php 
  12. /** 
  13. * 文件缓存类 
  14. * 
  15. * @copyright blog.itiwin.cn 
  16. * @author  More 
  17. * @package cache 
  18. * @version v0.1 
  19. */ 
  20. class FileCache { 
  21. /** 
  22. * @var string $cachePath 缓存文件目录 
  23. * @access public 
  24. */ 
  25. public $cachePath = './'; 
  26.  
  27. /** 
  28. * 构造函数 
  29. * @param string $path 缓存文件目录 
  30. */ 
  31. function __construct($path = NULL) { 
  32. if ($path) { 
  33. $this->cachePath = $path; 
  34. } 
  35. } 
  36.  
  37. /** 
  38. * 析构函数 
  39. */ 
  40. function __destruct() { 
  41. //nothing 
  42. } 
  43.  
  44. /** 
  45. * 在cache中设置键为$key的项的值,如果该项不存在,则新建一个项 
  46. * @param string $key 键值 
  47. * @param mix $var 值 
  48. * @param int $expire 到期秒数 
  49. * @param int $flag 标志位 
  50. * @return bool 如果成功则返回 TRUE,失败则返回 FALSE。 
  51. * @access public 
  52. */ 
  53. public function set($key, $var, $expire = 36000, $flag = 0) { 
  54. $value = serialize($var); 
  55. $timeout = time() + $expire; 
  56. $result = safe_file_put_contents($this->cachePath . urlencode($key) .'.cache', 
  57. $timeout . '<<%-==-%>>' . $value); 
  58. return $result; 
  59. } 
  60.  
  61. /** 
  62. * 在cache中获取键为$key的项的值 
  63. * @param string $key 键值 
  64. * @return string 如果该项不存在,则返回false 
  65. * @access public 
  66. */ 
  67. public function get($key) { 
  68. $file = $this->cachePath . urlencode($key) .'.cache'; 
  69. if (file_exists($file)) { 
  70. $content = safe_file_get_contents($file); 
  71. if ($content===false) { 
  72. return false; 
  73. } 
  74. $tmp = explode('<<%-==-%>>', $content); 
  75. $timeout = $tmp[0]; 
  76. $value = $tmp[1]; 
  77. if (time()>$timeout) { 
  78.  
  79. $this->delete($key) ;//删除文件过期的 
  80. $result = false; 
  81. } else { 
  82. $result = unserialize($value); 
  83. } 
  84. } else { 
  85. $result = false; 
  86. } 
  87. return $result; 
  88. } 
  89.  
  90. /** 
  91. * 清空cache中所有项 
  92. * @return 如果成功则返回 TRUE,失败则返回 FALSE。 
  93. * @access public 
  94. */ 
  95. public function flush() { 
  96. $fileList = FileSystem::ls($this->cachePath,array(),'asc',true); 
  97. return FileSystem::rm($fileList); 
  98. } 
  99.  
  100. /** 
  101. * 删除在cache中键为$key的项的值 
  102. * @param string $key 键值 
  103. * @return 如果成功则返回 TRUE,失败则返回 FALSE。 
  104. * @access public 
  105. */ 
  106. public function delete($key) { 
  107. return FileSystem::rm($this->cachePath . $key .'.cache'); 
  108. } 
  109. } 
  110.  
  111. if (!function_exists('safe_file_put_contents')) { 
  112. function safe_file_put_contents($filename, $content) 
  113. { 
  114. $fp = fopen($filename, 'wb'); 
  115. if ($fp) { 
  116. flock($fp, LOCK_EX); 
  117. fwrite($fp, $content); 
  118. flock($fp, LOCK_UN); 
  119. fclose($fp); 
  120. return true; 
  121. } else { 
  122. return false; 
  123. } 
  124. } 
  125. } 
  126.  
  127. if (!function_exists('safe_file_get_contents')) { 
  128. function safe_file_get_contents($filename) 
  129. { 
  130. $fp = fopen($filename, 'rb'); 
  131. if ($fp) { 
  132. flock($fp, LOCK_SH); 
  133. clearstatcache(); 
  134. $filesize = filesize($filename); 
  135. if ($filesize > 0) { 
  136. $data = fread($fp, $filesize); 
  137. } 
  138. flock($fp, LOCK_UN); 
  139. fclose($fp); 
  140. return $data; 
  141. } else { 
  142. return false; 
  143. } 
  144. } 
  145. } 

Tags: php缓存数据库 php返回数据

分享到: