Typecho 侧边栏简易缓存方式

本文发布于 ,内容可能和实际情况存在出入。如果文章存在错误欢迎指正,我会根据情况对文章进行修改或做隐藏处理

之前一直尝试减少侧边栏的数据库查询次数,不过觉得费大力改完效果可能还没有缓存好

因为比较懒懒得弄Memcache什么的,就直接用了MySQL缓存
建表语句:

CREATE TABLE `typecho_sidebarcache` (
  `id` tinyint(1) unsigned NOT NULL DEFAULT '1',
  `code` varchar(10000) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MEMORY DEFAULT CHARSET=utf8;



实在懒得抠源码,缓存刷新就交给插件钩子吧
/usr/plugins/BBSidebarCache/Plugin.php:

<?php
/**
 * BILIBIBI侧边栏缓存
 * 
 * @category data
 * @package BBSidebarCache
 * @author BILIBIBI
 * @version 1.0.0
 * @link http://bilibibi.me
 */
class BBSidebarCache_Plugin implements Typecho_Plugin_Interface
{ 
    /**
     * 激活插件方法,如果激活失败,直接抛出异常
     * 
     * @access public
     * @return void
     * @throws Typecho_Plugin_Exception
     */
    public static function activate()
    {      
        Typecho_Plugin::factory('Widget_Feedback')->finishComment = array('BBSidebarCache_Plugin', 'delCache');
        Typecho_Plugin::factory('Widget_Contents_Page_Edit')->write = array('BBSidebarCache_Plugin', 'delCache');
        Typecho_Plugin::factory('Widget_Contents_Post_Edit')->write = array('BBSidebarCache_Plugin', 'delCache');
        Helper::addPanel(1, 'BBSidebarCache/panel.php', '刷新侧边栏缓存', '刷新侧边栏缓存',   'administrator');
    }
    
    /* 禁用插件方法 */
    public static function deactivate(){
        Helper::removePanel(1, 'BBSidebarCache/panel.php');
    }
 
    /* 插件配置方法 */
    public static function config(Typecho_Widget_Helper_Form $form){}
 
    /* 个人用户的配置方法 */
    public static function personalConfig(Typecho_Widget_Helper_Form $form){}
    
    /**
     * @清除缓存
     */
    public static function delCache($param,$param2){
        Typecho_Db::get()->query('DELETE FROM `typecho_sidebarcache`');
        return $param;#返回发布内容
    }

}



用来在后台删除文章评论时刷新缓存:(手动)
/usr/plugins/BBSidebarCache/panel.php:

<?php
include 'common.php';
include 'header.php';
include 'menu.php';
Typecho_Db::get()->query('DELETE FROM `typecho_sidebarcache`');
echo <<<EOF
<div class="main">
    <div class="body container">
EOF;
include 'page-title.php';
echo <<<EOF
        <div class="container typecho-page-main" role="form">
            <div id="MostCacheMain" class="col-mb-12 typecho-list">
                <h1 style="text-align:center;">缓存更新成功</h1>
            </div>
        </div>
    </div>
</div>
EOF;
include 'copyright.php';
include 'common-js.php';
include 'footer.php';
include 'table-js.php';

【其实当初是安装的MostCache,然后扒成这样的


在模板里自定义一个模板:(其实就是把Sidebar要缓存的地方复制下来)
/usr/themes/bilibibi/getsidebar.php:

<?php
/**
 * Sidebar API
 *
 * @package custom
 */
if (!empty($this->options->sidebarBlock) && in_array('ShowRecentPosts', $this->options->sidebarBlock)):
echo '<section class="widget"><h3 class="widget-title">'; _e('最新文章'); echo '</h3><ul class="widget-list">'; $this->widget('Widget_Contents_Post_Recent')->parse('<li><a href="{permalink}">{title}</a></li>'); echo '</ul></section>'; endif;if (!empty($this->options->sidebarBlock) && in_array('ShowRecentComments', $this->options->sidebarBlock)): echo '<section class="widget"><h3 class="widget-title">'; _e('最近回复'); echo '</h3><ul class="widget-list">'; $this->widget('Widget_Comments_Recent')->to($comments);while($comments->next()): echo '<li><a href="'; $comments->permalink(); echo '">'; $comments->author(false); echo '</a>: '; $comments->excerpt(35, '...'); echo '</li>'; endwhile; echo '</ul></section>'; endif;if (!empty($this->options->sidebarBlock) && in_array('ShowCategory', $this->options->sidebarBlock)): echo '<section class="widget"><h3 class="widget-title">'; _e('分类'); echo '</h3>'; $this->widget('Widget_Metas_Category_List')->listCategories('wrapClass=widget-list'); echo '</section>'; endif;if (!empty($this->options->sidebarBlock) && in_array('ShowArchive', $this->options->sidebarBlock)): echo '<section class="widget"><h3 class="widget-title">'; _e('归档'); echo '</h3><ul class="widget-list">'; $this->widget('Widget_Contents_Post_Date', 'type=month&format=F Y')->parse('<li><a href="{permalink}">{date}</a></li>'); echo '</ul></section>'; endif;

之后添加独立页面,模板选择Sidebar API,地址我填的是sbar


之后在模板里修改侧边栏调用:
/usr/themes/bilibibi/sidebar.php:(部分)

<?php
    $SDb = Typecho_Db::get();
    $SCode = $SDb->fetchRow('SELECT code FROM `typecho_sidebarcache` WHERE id = 1')['code'];
    if(!$SCode){
        $SCode = file_get_contents('http://bilibibi.me/sbar');
        $SDb->query('DELETE FROM `typecho_sidebarcache`');
        $SDb->query('INSERT INTO `typecho_sidebarcache` (id,code) VALUES (1,\''.addslashes($SCode).'\')');
    }
    echo $SCode;
?>
<?php/* if (!empty($this->options->sidebarBlock) && in_array('ShowRecentPosts', $this->options->sidebarBlock)): ?>
    <section class="widget">
        <h3 class="widget-title"><?php _e('最新文章'); ?></h3>
        <ul class="widget-list">
            <?php $this->widget('Widget_Contents_Post_Recent')
            ->parse('<li><a href="{permalink}">{title}</a></li>'); ?>
        </ul>
    </section>
    <?php endif; ?>

    <?php if (!empty($this->options->sidebarBlock) && in_array('ShowRecentComments', $this->options->sidebarBlock)): ?>
    <section class="widget">
        <h3 class="widget-title"><?php _e('最近回复'); ?></h3>
        <ul class="widget-list">
        <?php $this->widget('Widget_Comments_Recent')->to($comments); ?>
        <?php while($comments->next()): ?>
            <li><a href="<?php $comments->permalink(); ?>"><?php $comments->author(false); ?></a>: <?php $comments->excerpt(35, '...'); ?></li>
        <?php endwhile; ?>
        </ul>
    </section>
    <?php endif; ?>

    <?php if (!empty($this->options->sidebarBlock) && in_array('ShowCategory', $this->options->sidebarBlock)): ?>
    <section class="widget">
        <h3 class="widget-title"><?php _e('分类'); ?></h3>
        <?php $this->widget('Widget_Metas_Category_List')->listCategories('wrapClass=widget-list'); ?>
    </section>
    <?php endif; ?>

    <?php if (!empty($this->options->sidebarBlock) && in_array('ShowArchive', $this->options->sidebarBlock)): ?>
    <section class="widget">
        <h3 class="widget-title"><?php _e('归档'); ?></h3>
        <ul class="widget-list">
            <?php $this->widget('Widget_Contents_Post_Date', 'type=month&format=F Y')
            ->parse('<li><a href="{permalink}">{date}</a></li>'); ?>
        </ul>
    </section>
    <?php endif; */?>



感觉直接都弄成插件会简单点,不过懒得翻手册了

标签: none

添加新评论