找回密码
 立即注册

QQ登录

只需一步,快速开始

emblog 缓存强化配置方案

1.memcache缓存配置
(1)mcache.php 目录include/lib
内容:
  1. <?php
  2. class MCache{
  3. private static $_instance;
  4. private static $_connect_type = '';
  5. private $_memcache;

  6. /**
  7.   * 私有化构造函数,禁止使用关键字new来实例Mcache类
  8.   */
  9. private function __construct() {
  10.   if (!class_exists('Memcache')) {
  11.    throw new Exception('Class Memcache not exists');
  12.   }
  13.   $conn = self::$_connect_type;
  14.   $this->_memcache = new Memcache();
  15.   $this->_memcache->$conn('localhost', '11211');
  16. }

  17. /**
  18.   * 克隆私有化,禁止克隆实例
  19.   */
  20. private function __clone() {}

  21. /**
  22.   * 类入口,通过此静态方法对类进行实例化
  23.   */
  24. public static function getInstance($type = 'connect'){
  25.   self::$_connect_type = ($type == 'connect') ? $type : 'pconnect';
  26.   if (!self::$_instance instanceof self) {
  27.    self::$_instance = new self();
  28.   }
  29.   return self::$_instance;
  30. }

  31. /**
  32.   * 把数据添加到缓存
  33.   * @param string $key 缓存的key
  34.   * @param string|array|int... $value 缓存的数据
  35.   * @param int $flag  使用zlib MEMCACHE_COMPRESSED
  36.   * @param int $expire_time  缓存时间
  37.   */
  38. public function set($key, $value,$flag = 0 ,$expire_time = 0){
  39.    $this->_memcache->set($key, $value, $flag, $expire_time);   
  40. }
  41. /**
  42.   * 替换缓存数据
  43.   * @param string $key 缓存的key
  44.   * @param string|array|int... $value 缓存的数据
  45.   * @param int $flag  使用zlib MEMCACHE_COMPRESSED
  46.   * @param int $expire_time  缓存时间
  47.   */
  48. public function replace($key, $value,$flag = 0 , $expire_time = 0){
  49.    $this->_memcache->replace($key, $value, $flag, $expire_time);   
  50. }

  51. /**
  52.   * 从缓存读取数据
  53.   * @param string|array|int... $key
  54.   */
  55. public function get($key){
  56.   return $this->_memcache->get($key);
  57. }

  58. /**
  59.   * 从缓存删除数据
  60.   * @param string|array|int... $key
  61.   */
  62. public function del($key,$expire_time = 0){
  63.   $this->_memcache->delete($key, $expire_time);  
  64. }
  65. public function close(){
  66. return $this->_memcache->close();
  67. }
  68. }
复制代码

(2)修改include/lib/cache.php
搜索
  1. private function __construct() {
  2.                 $this->db = Database::getInstance();
  3.         }
复制代码

改为:
  1. private function __construct() {
  2.   $this->db = Database::getInstance();
  3.   $this->memcache = MCache::getInstance();
  4. }
复制代码

搜索
  1.         function cacheWrite ($cacheData, $cacheName) {
  2.                 $cachefile = EMLOG_ROOT . '/content/cache/' . $cacheName . '.php';
  3.                 $cacheData = "<?php exit;//" . $cacheData;
  4.                 @ $fp = fopen($cachefile, 'wb') OR emMsg('读取缓存失败。如果您使用的是Unix/Linux主机,请修改缓存目录 (content/cache) 下所有文件的权限为777。如果您使用的是Windows主机,请联系管理员,将该目录下所有文件设为可写');
  5.                 @ $fw = fwrite($fp, $cacheData) OR emMsg('写入缓存失败,缓存目录 (content/cache) 不可写');
  6.                 $this->{$cacheName.'_cache'} = null;
  7.                 fclose($fp);
  8.         }
复制代码


改为:
  1. function cacheWrite ($cacheData, $cacheName) {
  2.   $this->memcache->set($cacheName,$cacheData);
  3. }
复制代码


搜索
  1.         function readCache($cacheName) {
  2.                 if ($this->{$cacheName.'_cache'} != null) {
  3.                         return $this->{$cacheName.'_cache'};
  4.                 } else {
  5.                         $cachefile = EMLOG_ROOT . '/content/cache/' . $cacheName . '.php';
  6.                         // 如果缓存文件不存在则自动生成缓存文件
  7.                         if (!is_file($cachefile) || filesize($cachefile) <= 0) {
  8.                                 if (method_exists($this, 'mc_' . $cacheName)) {
  9.                                         call_user_func(array($this, 'mc_' . $cacheName));
  10.                                 }
  11.                         }
  12.                         if ($fp = fopen($cachefile, 'r')) {
  13.                                 $data = fread($fp, filesize($cachefile));
  14.                                 fclose($fp);
  15.                 clearstatcache();
  16.                                 $this->{$cacheName.'_cache'} = unserialize(str_replace("<?php exit;//", '', $data));
  17.                                 return $this->{$cacheName.'_cache'};
  18.                         }
  19.                 }
  20.         }
复制代码

改为:
  1. function readCache($cacheName) {
  2.   if($this->memcache->get($cacheName)===false){call_user_func(array($this, 'mc_' . $cacheName));}
  3.   $data = $this->memcache->get($cacheName);
  4.   $this->{$cacheName.'_cache'} = unserialize($data);
  5.   return $this->{$cacheName.'_cache'};
  6. }
复制代码


2.文件缓存配置

本教程由无限星辰工作室CRX349独家整理和提供,转载请注明地址,谢谢。本文地址:https://www.xmspace.net/thread-768-1-1.html
无限星辰工作室  好集导航 Discuz全集下载  星辰站长网  集热爱361  一品文学  手机小游戏合集   海外空间网 星辰api  星辰支付二维码管理平台 阿里云服务器 腾讯云服务器
服务Discuz!建站|DiscuzQ配置|二开|小程序|APP|搬家|挂马清理|防护|Win/Linux环境搭建|优化|运维|
服务理念:专业 诚信 友好QQ842062626 服务项目 Q群315524225

发表于 2019-7-1 14:49:10 | 显示全部楼层 |阅读模式

回复 | 使用道具 举报

该帖共收到 0 条回复!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

美图秀

    • fastadmin 后台界面使用字段数组类型
    • Discuz!x3.5 修改标题高亮颜色
    • Discuz!x3.5 应用中心 下载应用一直下载中
    • 帖子定时显示
    • 论坛辅助审核
拖动客服框
Online Service
点击这里给我发消息
点击这里联系我们
微信扫一扫
在线客服
快速回复 返回顶部 返回列表