EMLOG优化用户和发文章缓存

分享

EMLOG是基于PHP+MYSQL的运行环境,那么缓存来源于MYSQL数据库,优化缓存速度,就是要加快MYSQL的查询速度。EMLOG的用户、文章、标签过多都会影响网站的运行速度和发布文章的速度

1.用户缓存优化

打开 include/lib/cache.php 第92行的用户数据缓存这里,代码如下:
/**
 * 用户信息缓存
 */
private function mc_user() {
$user_cache = array();
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "user");
while ($row = $this->db->fetch_array($query)) {
    $photo = array();
    $avatar = '';
    if(!empty($row['photo'])){
        $photosrc = str_replace("../", '', $row['photo']);
        $imgsize = chImageSize($row['photo'], Option::ICON_MAX_W, Option::ICON_MAX_H);
        $photo['src'] = htmlspecialchars($photosrc);
        $photo['width'] = $imgsize['w'];
        $photo['height'] = $imgsize['h'];
 
        $avatar = strstr($photosrc, 'thum') ? str_replace('thum', 'thum52', $photosrc) : preg_replace("/^(.*)\/(.*)$/", "\$1/thum52-\$2", $photosrc);
        $avatar = file_exists('../' . $avatar) ? $avatar : $photosrc;
    }
    $row['nickname'] = empty($row['nickname']) ? $row['username'] : $row['nickname'];
    $user_cache[$row['uid']] = array(
        'photo' => $photo,
        'avatar' => $avatar,
        'name_orig' => $row['nickname'],
        'name'

替换为

/**
 * 用户信息缓存
 */
private function mc_user() {
    $user_cache = array();
    $query = $this->db->query("SELECT uid,username,nickname,role,ischeck,photo,email,description FROM " . DB_PREFIX . "user");
    while ($row = $this->db->fetch_array($query)) {
        // $photo = array();
        /*
        $avatar = '';
        if(!empty($row['photo'])){
            $photosrc = str_replace("../", '', $row['photo']);
            $imgsize = chImageSize($row['photo'], Option::ICON_MAX_W, Option::ICON_MAX_H);
            $photo['src'] = htmlspecialchars($photosrc);
            $photo['width'] = $imgsize['w'];
            $photo['height'] = $imgsize['h'];
 
            $avatar = strstr($photosrc, 'thum') ? str_replace('thum', 'thum52', $photosrc) : preg_replace("/^(.*)\/(.*)$/", "\$1/thum52-\$2", $photosrc);
            $avatar = file_exists('../' . $avatar) ? $avatar : $photosrc;
        }*/
        $row['nickname'] = empty($row['nickname']) ? $row['username'] : $row['nickname'];
        $user_cache[$row['uid']] = array(
            'photo' => $row['photo'],
            'avatar' => $row['photo'],
            'name_orig' => $row['nickname'],
            'name' => htmlspecialchars($row['nickname']),
            'mail' => htmlspecialchars($row['email']),
            'des' => htmlClean($row['description']),
            'ischeck' => htmlspecialchars($row['ischeck']),
            'role' => $row['role'],
            );
    }
    $cacheData = serialize($user_cache);
    $this->cacheWrite($cacheData, 'user');
}

不过,我亲测了一下,发现前台的头像无法显示了。解决方法如下:

把下面这两段代码取消注释就行了

$photosrc = str_replace("../", '', $row['photo']);
$photo['src'] = htmlspecialchars($photosrc);

可以放在$row['nickname'] = empty($row['nickname']) ? $row['username'] : $row['nickname'];

的上面或者下面都可以!

2.发布文章优化

打开 admin/save_log.php 文件第66行的代码:
$CACHE->updateCache();

替换为

$CACHE->updateCache(array('sta', 'tags', 'newlog', 'logsort', 'logtags'));
提示信息

SQL语句执行错误: SELECT * from emlog_attachment_web where logid = 705
Table 's7937350.emlog_attachment_web' doesn't exist

«点击返回