• Uncle Liu's Blog
  • 追忆曾经逝去的岁月,在此存放我容易丢失的记忆!
  • 首页
  • 碎语
  • 留言板
  • 登录
  • PHP一个小巧的缓存类

    [PHP+MYSQL]
    post by Uncle Liu / 2009-7-8 9:42 Wednesday

    功能很简单,就是缓存整个页面,可以设定缓存时间,可以缓存特定的URL,例如:test.php?id=12,当目标文件更新时,如test.php,缓存文件也会更新,即使仍处于缓存期内。

     

    PHP代码
    1. <?php   
    2.     class cache   
    3. {   
    4. var $cache_dir = './cache/';//This is the directory where the cache files will be stored;   
    5. var $cache_time = 180;//How much time will keep the cache files in seconds.   
    6.   
    7. var $caching = false;   
    8. var $file = '';   
    9.   
    10. function cache()   
    11. {   
    12.     if(!file_exists($this->cache_dir))   
    13.     {   
    14.         mkdir($this->cache_dir);   
    15.     }   
    16. //Constructor of the class   
    17. $this->file = $this->cache_dir . urlencode( $_SERVER['REQUEST_URI'] );   
    18. if(file_exists($this->file)) $expired = $this->check_expire();   
    19. else $expired = false;   
    20. if ( file_exists ( $this->file ) && ( filemtime ( $this->file ) + $this->cache_time ) > time() && !$expired )   
    21. {   
    22. //Grab the cache:   
    23. $handle = fopen( $this->file , "r");   
    24. do {   
    25. $data = fread($handle, 8192);   
    26. if (strlen($data) == 0) {   
    27. break;   
    28. }   
    29. echo $data;   
    30. } while (true);   
    31. fclose($handle);   
    32. exit();   
    33. }   
    34. else  
    35. {   
    36. //create cache :   
    37. $this->caching = true;   
    38. ob_start();   
    39. $now = time();   
    40. //echo "<!--last modified:".$now."-->\n";   
    41. }   
    42. }   
    43.   
    44. function close()   
    45. {   
    46. //You should have this at the end of each page   
    47. if ( $this->caching )   
    48. {   
    49. //You were caching the contents so display them, and write the cache file   
    50. $data = ob_get_clean();   
    51. echo $data;   
    52. $fp = fopen( $this->file , 'w' );   
    53. fwrite ( $fp , $data );   
    54. fclose ( $fp );   
    55. }   
    56. }   
    57. function check_expire(){   
    58.   
    59. $modify_time = filemtime($this->file);   
    60. if($modify_time<filemtime($_SERVER['SCRIPT_FILENAME'])){   
    61. return true;   
    62. }   
    63. else{   
    64. return false;   
    65. }   
    66.   
    67. }   
    68. }   
    69.   
    70.   
    71.   
    72. //Example :   
    73. $ch = new cache();   
    74. echo 'test';   
    75. $ch->close();   
    « php删除文件夹及其文件夹下所有文件 | php禁止IP访问代码»

    引用地址:

    发表评论:

  • 搜索

  • 岁月如梭

  • 分类

    • 心情记事(40) 订阅该分类
    • 经典网文(30) 订阅该分类
    • PHP+MYSQL(40) 订阅该分类
    • Javascript(21) 订阅该分类
    • 电脑网络(33) 订阅该分类
    • emlog(7) 订阅该分类
    • Linux(2) 订阅该分类
  • 日历

  • 随机日志

    • 鼠标移过自动打开连接
    • 地震
    • 情侠
    • Lonely people are shameful
    • 烽火辞
  • 链接

    • TangJun's blog
    • vb.vc个性域名
    • 点滴记忆
    • 奇遇——心情随笔
    • Sfan@Live
    • 乐小北
    • 游戏毁我半生
    • 铁观音茶叶
    • ═╬簡箪√嗳
    • KLLER's Blog
    • Web前端开发
    • 零度自由
    • PHP100中文站论坛
    • Api.im
    • emlog
  • 最新日志

    • 茶汤
    • 操蛋的一年终于过去了
    • 域名城第16届域名大赛 - “非6勿扰”双拼.com域名大赛
    • 永远的兄弟
    • 夜半孤寂茶当酒,一壶独饮到天明
  • soft

    • 分类信息
订阅Rss
Powered by emlog | Loekman 主题 | 沪ICP备09020591号