欢迎来到好人卡资源网,专注网络技术资源收集,我们不仅是网络资源的搬运工,也生产原创资源。寻找资源请留言或关注公众号:烈日下的男人

Typecho主题“simplifier”全站静态缓存版本

网站源码 sky995 4年前 (2021-05-03) 1037次浏览 0个评论

本文及资源最后更新时间 2021-05-03 by sky995

前言

这个主题样式就那样,主要是给大家提供一个全站静态化思路!你可以参考这个主题去修改你现在的主题,然后全站自动化生成静态。

ps:我测试了所有主题都可以这样改,URL什么格式都可以!!!

Typecho主题“simplifier”全站静态缓存版本

使用

  1. 在网站 根目录 新建 文件夹 “cache” 作为缓存文件存放,记得设置777 权限
  2. chmod -Rf 777 /usr/themes/simplifier/简单暴力,让生成的缓存文件有写入权限
  3. 复制gengxinwenzhang.php 复制或者移动到根目录 这个是手动更新文章

Typecho主题“simplifier”全站静态缓存版本

说明

预览地址:https://9i3.cn/ 目前15w 文章都是缓存文件 不读取数据库

Typecho主题“simplifier”全站静态缓存版本

文章缓存核心代码

  1. <?php
  2. $pageURL = 'http';
  3. if ($_SERVER["HTTPS"] == "on")
  4. {
  5. $pageURL .= "s";
  6. }
  7. $pageURL .= "://";
  8. $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
  9. $shouye = $this->options->siteUrl;
  10. $post = str_replace($shouye,"",$pageURL);
  11. $post = str_replace(".html","",$post);
  12. $post = str_replace("/","",$post);
  13. $cache= "cache/" . $post;
  14. $filename = $cache;
  15. //定义缓存有效期
  16. $cachetime = 259200; //设置缓存三天,三天后有人访问再次生成最新 三天
  17. //判断缓存文件是否存在
  18. if(!file_exists($filename) || filemtime($filename)+$cachetime<time()) //filemtime($filename)获取文件修改时间,加上定义的缓存时间小于当前时间
  19. {
  20. //开启内存缓存
  21. ob_start();
  22. ?>
  23. <?php $this->need('post-html.php'); ?>
  24. <?php
  25. //从内存缓存中获取页面代码
  26. $content = ob_get_contents();
  27. $content .= "\n<!-- 缓存于: " . date( 'Y-m-d H:i:s' ) . " -->";
  28. //将获取到的内容存放到缓存文件
  29. file_put_contents($filename,$content);
  30. //清掉内存缓存
  31. ob_flush();
  32. }
  33. else
  34. {
  35. include($filename); //如果存在,调用缓存文件
  36. }
  37. ?>
  38. <?php $this->need('comments.php'); //不缓存评论 ?>
  39. <?php $this->need('footer.php'); ?>

首页核心代码

  1. <?php
  2. $filename = "index.htm";
  3. //定义缓存有效期
  4. $cachetime = 10800;
  5. //判断缓存文件是否存在
  6. if(!file_exists($filename) || filemtime($filename)+$cachetime<time()) //filemtime($filename)获取文件修改时间,加上定义的缓存时间小于当前时间
  7. {
  8. //开启内存缓存
  9. ob_start();
  10. ?>
  11. <?php $this->need('index-html.php'); ?>
  12. <?php
  13. //从内存缓存中获取页面代码
  14. $content = ob_get_contents();
  15. $content .= "\n<!-- 缓存于: " . date( 'Y-m-d H:i:s' ) . " -->";
  16. //将获取到的内容存放到缓存文件
  17. file_put_contents($filename,$content);
  18. //清掉内存缓存
  19. ob_flush();
  20. }
  21. else
  22. {
  23. include($filename); //如果存在,调用缓存文件
  24. }
  25. ?>
  26. <?php else: ?>
  27. <?php
  28. $pageURL = 'http';
  29. if ($_SERVER["HTTPS"] == "on")
  30. {
  31. $pageURL .= "s";
  32. }
  33. $pageURL .= "://";
  34. $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
  35. $shouye = $this->options->siteUrl;
  36. $post = str_replace($shouye,"",$pageURL);
  37. $post = str_replace(".html","",$post);
  38. $post = str_replace("/","",$post);
  39. $cache= "cache/" . $post;
  40. $filename = $cache;
  41. //定义缓存有效期
  42. $cachetime = 86400;
  43. //判断缓存文件是否存在
  44. if(!file_exists($filename) || filemtime($filename)+$cachetime<time()) //filemtime($filename)获取文件修改时间,加上定义的缓存时间小于当前时间
  45. {
  46. //开启内存缓存
  47. ob_start();
  48. ?>
  49. <?php $this->need('index-html.php'); ?>
  50. <?php
  51. //从内存缓存中获取页面代码
  52. $content = ob_get_contents();
  53. $content .= "\n<!-- 缓存于: " . date( 'Y-m-d H:i:s' ) . " -->";
  54. //将获取到的内容存放到缓存文件
  55. file_put_contents($filename,$content);
  56. //清掉内存缓存
  57. ob_flush();
  58. }
  59. else
  60. {
  61. include($filename); //如果存在,调用缓存文件
  62. }
  63. ?>
  64. <?php endif; ?>

下载地址

Typecho主题“simplifier”
说明:全站静态缓存版本,参考代码适合任程序

ps:好像后台没有附件上传的地方,需要折腾看看了。


好人卡资源网 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Typecho主题“simplifier”全站静态缓存版本
喜欢 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址