找回密码
 加入我们

QQ登录

只需一步,快速开始

搜索
查看: 7|回复: 1

[NAS/Server] 为 WordPress 启用对象缓存

[复制链接]
发表于 2024-4-22 14:38 | 显示全部楼层 |阅读模式
https://www.iotheme.cn/wei-wordpress-qiyongduixianghuancun.html



WordPress 启用对象缓存,使用的工具一般是Redis和Memcached这两种。
什么是对象缓存?
对象缓存涉及存储数据库查询结果,以便下次需要结果时,可以从缓存中为它提供服务,而不必重复查询数据库。作为内容管理系统,WordPress自然且高度依赖于数据库。因此,数据库效率对于扩展WordPress至关重要。
如果您运行一个人流量大的站点,并且对页面的请求生成了大量的数据库查询,则您的服务器很快就会变得不堪重负,进而对站点的性能产生负面影响。因此,在站点上启用对象缓存后,它可以帮助减轻数据库和服务器的负载并更快地传递查询。
什么是Redis和Memcached?
这两种工具都是快速而强大的内存中数据存储,可以减少站点的MySQL数据库负载,同时还可以减少站点的响应时间并增强站点的扩展能力和处理增加的流量的能力。
长期以来,Memcached一直是流行的缓存选择,但是Redis可以执行Memcached可以做的所有事情,并且具有更大的功能集。另外,它更受欢迎并且得到更好的支持。
要深入了解Redis和Memcached的功能和优缺点,可以查看Stack Overflow 上的这篇文章
如何在WORDPRESS中使用对象缓存
默认情况下,内置于WordPress的对象缓存已在您的网站上运行,因此您无需执行任何操作即可启用它。
但是,如果您想将对象缓存提高到一个新的水平,以便在页面加载之间持久地缓存数据库查询,则有一些可用的选项很容易实现。
以下三种方法任选一种。
1.使用Redis
对于支持Redis的对象缓存,可以使用WordPress.org上提供的免费插件Redis Object Cache。它支持Predis、PhpRedis(PECL)、HHVM、复制、群集和WP-CLI。
使用此插件之前,您需要检查您的站点是否正在使用具有所需PHP Redis扩展名和正常工作的Redis服务器的PHP环境,宝塔可以安装下图在PHP管理里安装 redis 扩展,使用自编译环境的大神请自行解决。
如果一切就绪,则此插件的安装非常简单-只需激活该插件,转到“设置”>“ Redis”,然后单击“启用对象缓存”即可。

此外,您还可以选择另一个插件:WP Redis,它的设置有些复杂。该插件要求您创建一个名为 object-cache.php 的文件并将其添加到wp-content文件夹中,并编辑wp-config.php文件。
如果WP-CLI是开发工作流程的重要组成部分,则可能会发现使用WP Redis更为方便,因为它附带了各种命令。
2.使用Memcached
使用Memcached的对象缓存,请先确认PHP是否安装了memcached扩展,宝塔用户看下图,自编译环境的大佬请百度。
然后请下载 object-cache.php 文件,将其添加到wp-content文件夹中。
[color=var(--this-color)][size=1.25]下载文件

下载上面文件后解压,得到object-cache.php 文件,将其添加到wp-content文件夹中



最后,如果你的服务器上有多个wp站点,且数据库的表前缀都是相同的“wp_”,则需要编辑wp-config.php文件(站点根目录),在文件最后添加一行代码 define('WP_CACHE_KEY_SALT', 'www.iotheme.cn'); ,注意网址改为自己的。
3.询问您的主机商
如果您使用托管型WordPress主机,则您的主机可能会通过Redis提供对象缓存。因此,请查看主机的文档,以获取有关如何启用对象缓存的信息,或者与您的主机商联系以检查其是否可用。
如果您使用的是虚拟主机,一般是没有提供这些高级缓存服务的。如果您无法使用对象缓存,则可能要考虑升级为云服务器。
在这里,我们推荐大家购买 阿里云腾讯云 等大厂的云服务器,记住一点,不要再去使用虚拟主机了,虚拟主机是没有多少办法做优化的。

回复

使用道具 举报

 楼主| 发表于 2024-4-22 14:41 | 显示全部楼层
https://docs.directadmin.com/oth ... db-mysql/redis.html


Redis (PRO PACK)
This is a ** PRO-PACK FEATURE **
Redis functions as a simple database/cache for Users. This can be useful for offloading some of the stress from your database into memory, if you have the memory available to support it.
To install redis:

  1. cd /usr/local/directadmin/custombuild
  2. ./build set redis yes
  3. ./build redis
复制代码

#Enabling for users
All Reseller/User packages and reseller.conf/user.conf files will have redis=OFF by default.
The Admin account reseller.conf will have redis=ON by default.
All accounts that need redis must have it enabled like so:
  • Enable redis in the packages and re-save as needed
  • Enable redis in the accounts and save as needed.
#CMD_REDIS JSON#Checking the status for a user
To check the current status for the current User, use a GET request with no options. The return should look similar to this:
  1. {
  2.         "enabled": "0",
  3.         "global": "1"
  4. }
复制代码


  • "enabled" refers to redis=ON in the user.conf
  • "global" is the service check to ensure it's installed on the system
#Enabling Redis for a user
Submit a POST request with the following:
  1. action=enable
复制代码

#Disabling Redis for a user
Submit a POST request with the following:
  1. action=disable
复制代码

#API usage
CMD_API_REDIS does exist and will always be use JSON mode for output.
#Relative Skin changes
The following files were modified:
  • /usr/local/directadmin/data/skins/enhanced/admin/create_customized_reseller.html
  • /usr/local/directadmin/data/skins/enhanced/admin/modify_reseller.html
  • /usr/local/directadmin/data/skins/enhanced/admin/show_reseller_package.html
  • /usr/local/directadmin/data/skins/enhanced/lang/en/lf_standard.html
  • /usr/local/directadmin/data/skins/enhanced/reseller/create_customized_user.html
  • /usr/local/directadmin/data/skins/enhanced/reseller/modify_user.html
  • /usr/local/directadmin/data/skins/enhanced/reseller/show_user_package.html

to support the relative global tokens including:
    1. <li>RESELLERREDIS=ON</li><li>USERREDIS=ON</li><li>HAVE_REDIS=ON (which is enabled in license and installed on system)</li>
    复制代码

POST values for packages/reseller/user include redis=ON or redis=OFF, and omission altogether implies OFF.
#How to Use Redis With WordPress
  • Install Redis and php_redis extension:
  1. <font color="rgb(44, 62, 80)"><font face="-apple-system, BlinkMacSystemFont, " "=""><font style="font-size: 16px">cd /usr/local/directadmin/custombuild</font></font></font>
  2. <div><font color="rgb(44, 62, 80)"><font face="-apple-system, BlinkMacSystemFont, " "=""><font style="font-size: 16px">./build set redis ye</font></font></font><span style="font-size: 16px; font-family: -apple-system, BlinkMacSystemFont, " ";="" color:="" rgb(44,="" 62,="" 80);"="">s</span>
  3. <span style="font-size: 16px; font-family: -apple-system, BlinkMacSystemFont, " ";="" color:="" rgb(44,="" 62,="" 80);"="">./build redis</span>
  4. <span style="font-size: 16px; font-family: -apple-system, BlinkMacSystemFont, " ";="" color:="" rgb(44,="" 62,="" 80);"="">./build php_redis</span></div>
复制代码



to /home/<user>/domains/<domain>/public_html/wp-config.php BEFORE the last line which includes wp-settings.php.
**Make sure to replace <user> ** with the DirectAdmin user you're trying to configure. Note that the path defined by WP_REDIS_PATH should be the path mentioned in step 2.

After refreshing the wordpress admin page, you'll notice that the connection status changed to "Connected".

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 加入我们

本版积分规则

QQ|Archiver|手机版|小黑屋|吹友吧 ( 京ICP备05078561号 )

GMT+8, 2024-6-6 22:06 , Processed in 0.299174 second(s), 16 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表