如何给wordpress theme增加缓存

时间:2007-01-19 19:18:12      类别:PHP, Web技术|WEB Tech, Wordpress      本文链接:生活点滴Enjoy Life

可惜现在工作了,没有大学那么多空余的时间来写写php 或者js之类的(本人是学微波通信的哈),但是最近用上了WP,而且不喜欢千篇一律的风格,所以就自己写了一个简单的风格,使本站看这基本不像传统的blog风格了,不过由于此风格导致首页调用的数据量太大,导致首页会很慢,很容易把访客吓跑,因此不得不考虑文件Cache本站数据了,其实,我的cache机制也仅仅是基于模板,不过从wp自带的页面载入时间函数的结果看,至少比没有cache节约了1/4的时间。

smarty和PEAR也是我大学的时候经常用到的php库,突然想到smarty和PEAR的缓存功能是不是可以用到theme里面。由于wp的模板不是基于smarty,况且以前本人用smarty的时候也发现了它的缓存有点小小的问题,所以本次采用了PEAR的Cache_Lite.

进入正题,首先您得有PEAR库支持,您可以到pear.php.net 去下载。你也可以先下载我这里的。

我们看看好多theme都是那几大块组成的(http://codex.wordpress.org/Template_Hierarchy),header.php footer.php,那好,我们就把cache的头文件和footer文件分别包含进去,这样就可以每个页面进行缓存了。

这里说说我用的目录结构 theme.JPG其中我把PEAR放到lib文件夹下,方便今后再增加lib。cache的php文件放在我的目半cosbeta下面。即ThemeCache,如图:infolder.JPG

下面是cache_start.php的内容:

  1. < ?php
  2. /*
  3. author cosbeta http://www.storyday.com
  4. @version 1.0
  5. pls read the readme file to get more details
  6. */
  7. define('THEME_CACHE_DIR',$_SERVER["DOCUMENT_ROOT"]."/wordpress/wp-content/themes/cosbeta/ThemeCache"); //this is the cache path 这里是定义cache的dir,即cache的文件放在哪里
  8.  
  9. define('PEAR_DIR',$_SERVER["DOCUMENT_ROOT"].'/wordpress/lib/PEAR')//this is the pear path这里当然是定义PEAR的路径了
  10. $no_cache = array("/archives/36");//do not cache  my guestbook不需要cache的文件的URI,这里是我的留言本
  11. //set  the page with out cache fill the page URI into the array
  12.  
  13. $CustomLifeTime = array("0"=> "0");//set the custom life time of a spcialfied URI you may do like this  $CustomLifeTime = array("/index/"=> 20000,"/?p=23"=>140.....,"URI"=>lifetime);the URI is on the bottom of page source you can view from your browser这里是自定义的cache实效时间
  14.  
  15. $lifeTime = 36000 ;//default cache life time 这个是cache失效时间
  16.  
  17. /*
  18. you don't need to edit bellows
  19. */
  20. ini_set('include_path', PEAR_DIR . PATH_SEPARATOR . ini_get('include_path'));//设置php的include默认路径
  21.  
  22. require_once "Cache/Lite.php";
  23.  
  24. $options = array(
  25. 'cacheDir' => THEME_CACHE_DIR.'/cache/',
  26. 'lifeTime' => $lifeTime,
  27. 'hashedDirectoryLevel'=>2,//放置文件太多,所以设置为2如果不设置则所有cache文件都在同一个目录下,文件多了影响速度。具体可以参考pear manual
  28. 'pearErrorMode' => CACHE_LITE_ERROR_DIE
  29. );
  30. //设置cache的参数
  31. //
  32. //设置cache id
  33. //
  34.  
  35. //if this is search file do not cache 对于searh不使用cache
  36. if( $s != ""  ){
  37. $isCache = false;
  38. }
  39. else{
  40. $isCache = false;
  41. }
  42.  
  43. $cache_it0;
  44.  
  45. $id_of_the_pagemd5($_SERVER['REQUEST_URI']) ;//得到cache的id uri保证唯一,这里我偷懒了,如果人间随便建立URI。可以把我的服务器填满的,所以这里有bug存在。正在改进(准备加正则表达式判断URI是不是标准的URI)
  46.  
  47. if( $CustomLifeTime[$_SERVER['REQUEST_URI']] != ""){//查询自定义cache的life time
  48.  
  49. $options['lifeTime'] = $CustomLifeTime[$_SERVER['REQUEST_URI']] ;
  50.  
  51. }
  52.  
  53. $cache = new Cache_Lite($options);
  54.  
  55. if ($data = $cache->get( $id_of_the_page ) ) {
  56.  
  57. $cache_it = 1;
  58.  
  59. echo $data;//如果cache存在,直接输入
  60.  
  61. die();//由于这里的原因,因此本cache机制不能使用部分cache
  62. }
  63. else  {
  64. if( array_search( $_SERVER['REQUEST_URI'], $no_cache ) ){
  65. }
  66. else{
  67. include "Cache/Lite/Output.php";
  68. $cache2 = new Cache_Lite_Output($options);
  69. $cache2->start($id_of_the_page);//由于这里的原因,因此本cache机制不能使用部分cache,因为这里控制了输出流,如果输出未完成则不会cache的,这个也很好处理了用户中途关闭浏览器而导致cache不完整的情况。
  70. }
  71. }
  72. ?>

上面如果有不清楚的,参照pear mannual

下面是cache的cache_end.php文件

  1. < ?php
  2. if( (  $cache_it == 0 ) && $cache2  ){
  3. echo "<!-- cache at:".date("Y-m-d H:i:s")."(+8 TimeZone) AND Cache id is:".$_SERVER['REQUEST_URI']." the md5:".$id_of_the_page." AND this cache file will expire in  ".$options['lifeTime']."s-->";
  4. echo "\n<!-- open this url to remove cache (".get_settings('home')."/wp-content/themes/cosbeta/ThemeCache/clear_cache.php?id=".$id_of_the_page.") -->";
  5.  
  6. $cache2->end();
  7. }
  8. ?>

clearcache.php

clearcache很简单,点这里下载我全部源代码themecache.zip

自己点评:虽然说这个cache也仅仅是基于模板经过我自己不太准确的测试,至少能将页面载入时间减小到原来的1/4

另外,请将ThemeCache/cache的属性设置成 0777

该日志未加标签
发表于 2007-01-19 19:18:12 目录:PHP, Web技术|WEB Tech, Wordpress [RSS 2.0] 您可以评论. Pinging 不可用.
如果您喜欢本blog,欢迎你的feed订阅,谢谢你的支持

前7排已经被占据了 快抢好位置哦

  • 1楼 你好,上帝 在 2007.01.22 02:30发表评论如下:

    我没有电脑….
    口水.

  • 2楼 江 东 在 2007.01.23 10:00发表评论如下:

    what do you mean?

  • 3楼 luoo 在 2007.05.17 23:18发表评论如下:

    好东西,期待能有更好的改进先拿去测试了!

  • 4楼 cosβ 在 2007.05.18 08:21发表评论如下:

    好老的文章了,现在我已经有了插件

  • 5楼 luoo 在 2007.05.20 18:43发表评论如下:

    什么插件能 公开下吗?

  • 6楼 cosβ 在 2007.05.20 18:53发表评论如下:

    帅哥,左上角那么大的搜索框,很有用的,去搜索吧,我故意把搜索框放在那里,可是很多朋友都不喜欢搜索,其实那个东西很有用的

  • 7楼 未知 在 2008.06.17 06:54发表评论如下:

    为什么设置没效果呢 cache目录也没生成东西 php4.3.11可以使用吗 页面html代码也没显示生成时间什么的

  • 转到第
(Required)
(Required, not published)
如果留言未显示请不要重复留言,我将为你恢复!


生活点滴Enjoy Life is proudly powered by WordPress | admin| About Us | cosbeta| Bluehost| site map 0.464s & 26