国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php 部分緩存數據庫返回數據的例子

2024-05-04 21:49:56
字體:
來源:轉載
供稿:網友

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. <?php 
  11. /** 
  12. * 文件緩存類 
  13. * 
  14. * @copyright blog.itiwin.cn 
  15. * @author  More 
  16. * @package cache 
  17. * @version v0.1 
  18. */ 
  19. class FileCache { 
  20. /** 
  21. * @var string $cachePath 緩存文件目錄 
  22. * @access public 
  23. */ 
  24. public $cachePath = './'
  25.  
  26. /** 
  27. * 構造函數 
  28. * @param string $path 緩存文件目錄 
  29. */ 
  30. function __construct($path = NULL) { 
  31. if ($path) { 
  32. $this->cachePath = $path
  33.  
  34. /** 
  35. * 析構函數 
  36. */ 
  37. function __destruct() { 
  38. //nothing 
  39.  
  40. /** 
  41. * 在cache中設置鍵為$key的項的值,如果該項不存在,則新建一個項 
  42. * @param string $key 鍵值 
  43. * @param mix $var 值 
  44. * @param int $expire 到期秒數 
  45. * @param int $flag 標志位 
  46. * @return bool 如果成功則返回 TRUE,失敗則返回 FALSE。 
  47. * @access public 
  48. */ 
  49. public function set($key$var$expire = 36000, $flag = 0) { 
  50. $value = serialize($var); 
  51. $timeout = time() + $expire
  52. $result = safe_file_put_contents($this->cachePath . urlencode($key) .'.cache'
  53. $timeout . '<<%-==-%>>' . $value); 
  54. return $result
  55.  
  56. /** 
  57. * 在cache中獲取鍵為$key的項的值 
  58. * @param string $key 鍵值 
  59. * @return string 如果該項不存在,則返回false 
  60. * @access public 
  61. */ 
  62. public function get($key) { 
  63. $file = $this->cachePath . urlencode($key) .'.cache'
  64. if (file_exists($file)) { 
  65. $content = safe_file_get_contents($file); 
  66. if ($content===false) { 
  67. return false; 
  68. $tmp = explode('<<%-==-%>>'$content); 
  69. $timeout = $tmp[0]; 
  70. $value = $tmp[1]; 
  71. if (time()>$timeout) { 
  72.  
  73. $this->delete($key) ;//刪除文件過期的 
  74. $result = false; 
  75. else { 
  76. $result = unserialize($value); 
  77. else { 
  78. $result = false; 
  79. return $result
  80.  
  81. /** 
  82. * 清空cache中所有項 
  83. * @return 如果成功則返回 TRUE,失敗則返回 FALSE。 
  84. * @access public 
  85. */ 
  86. public function flush() { 
  87. $fileList = FileSystem::ls($this->cachePath,array(),'asc',true); 
  88. return FileSystem::rm($fileList); 
  89.  
  90. /** 
  91. * 刪除在cache中鍵為$key的項的值 
  92. * @param string $key 鍵值 
  93. * @return 如果成功則返回 TRUE,失敗則返回 FALSE。 
  94. * @access public 
  95. */ 
  96. public function delete($key) { 
  97. return FileSystem::rm($this->cachePath . $key .'.cache'); 
  98.  
  99. if (!function_exists('safe_file_put_contents')) { 
  100. function safe_file_put_contents($filename$content
  101. $fp = fopen($filename'wb'); 
  102. if ($fp) { 
  103. flock($fp, LOCK_EX); 
  104. fwrite($fp$content); 
  105. flock($fp, LOCK_UN); 
  106. fclose($fp); 
  107. return true; 
  108. else { 
  109. return false; 
  110.  
  111. if (!function_exists('safe_file_get_contents')) { 
  112. function safe_file_get_contents($filename
  113. $fp = fopen($filename'rb'); 
  114. if ($fp) { 
  115. flock($fp, LOCK_SH); 
  116. clearstatcache(); 
  117. $filesize = filesize($filename); 
  118. if ($filesize > 0) { 
  119. $data = fread($fp$filesize); 
  120. flock($fp, LOCK_UN); 
  121. fclose($fp); 
  122. return $data
  123. else { 
  124. return false; 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 区。| 万盛区| 清原| 若羌县| 海口市| 墨脱县| 天峻县| 湘乡市| 昌平区| 萍乡市| 休宁县| 长治市| 伊金霍洛旗| 嘉定区| 永顺县| 平和县| 乌拉特后旗| 喀什市| 平顶山市| 弋阳县| 册亨县| 辽源市| 城口县| 化德县| 西乌| 江都市| 黄石市| 邹平县| 澄江县| 衡南县| 宜黄县| 专栏| 九龙县| 安西县| 南充市| 方正县| 五指山市| 都江堰市| 余庆县| 松滋市| 衡阳县|