[转]Redis Object Caching及如何使用

What is Redis Object Caching and How to Use It for Your WordPress Site

A Quick Refresher: What is Caching?

If a web page is cached, it means that elements of that page such as images, stylesheets, and other content is loaded once, then stored in what’s called a “cache.”

It memorizes what was loaded, creates a static version of it, then can serve that version much faster the next time that page is loaded.

The result is faster page load times and less of your server’s resources being used.

What is Object Caching?

Object caching is a type of server-side caching. This means the caching is administered at the server level, and isn’t controlled by the end user or a system they use for caching.

Object caching stores database query results that have been loaded. Then, it serves them up faster the next time they’re requested so the database doesn’t have to be queried again.

WordPress also has object caching built-in with the WP_Object_Cache class.

The trouble is, the inherent object caching WordPress has isn’t persistent by default. This means that cached data is only stored for as long as the request to the database lasts, which is ultimately no more than for one page load, and inefficient.

If you were to install and use a persistent object caching solution such as – ehem – Redis, for example, data could be cached for all subsequent page loads, giving your database more of a break..

Who Needs Object Caching?

If your WordPress site is static and all it needs to load are a stylesheet and some images, for example, you’re not going to see any difference in you use object caching.

Conversely, a dynamic site loads tons of data across pages that are stored in your database such as user details, taxonomies, links, and other similar data.

As previously mentioned, every time a page loads that content, it sends one (or often several!) database queries. If you use object caching, that data is stored in the cache and it’s ready to be displayed on the page in the flashes of flashes.

Your database can be queried much less often and retrieving content from the cache is a lot faster than sending queries to the database.

This results is page loading times that are a lot faster. Your server’s resources are also used more efficiently. This is an especially crucial factor if you’re looking to scale your WordPress website.

So, if your site gets a lot of traffic or you’re expecting it will soon, and it’s dynamic, you should consider using object caching.

Using Redis Object Caching on WordPress

To use Redis for object caching on WordPress sites, it starts with installing and configuring Redis, then installing a PHP extension, followed by the Redis Object Cache plugin.

Many managed WordPress hosting solutions already offer Redis object caching so you can check with your host to see if it’s an option. They may have a quick or one-click installation available.

If you find you need to install it yourself, you can do so manually.

Either way, it’s recommended you install Redis on a Linux server. While there’s no official support for Windows, there’s a Win-64 port of Redis that has been developed by Microsoft.

If you decided you want to manually install Redis for object caching on your Linux server, there are a few other prerequisites:

  • Root access to your Linux server
  • Ability to install Redis through SSH access
  • WordPress installed
  • PHP version 7.0 or higher
  • Server has both Wget and Sudo installed
  • Text editor is installed on your server such as Nano
  • Phpize is installed to prepare PHP extension for compiling

Be sure to also back up your entire site in case something goes wrong and you need to restore it. It’s also a good idea to test Redis before deploying it on a production server.

1. Installing Redis for Object Caching

While you could use the package manager of your specific Linux distribution such as apt or yum, it may not always be up-to-date with the latest version so it’s recommended you use wget with a link to the master archive.

On the command line, enter the following to install the latest stable release:

wget http://download.redis.io/redis-stable.tar.gz

Next, type the line below:

tar xvzf redis-stable.tar.gz

Then, follow it with this:

cd redis-stable

Finally, enter the following:

make

2. Configuring Redis as a Cache

It’s necessary to edit the configuration file now that Redis is installed to, well, configure it to use it specifically for object caching.

To get to the configuration file, use the line below:

sudo nano /etc/redis/redis.conf

Find where it says the following:

#maxmemory

Then, replace it with this:

maxmemory 128mb
maxmemory-policy allkeys-lfu

Keep in mind that you can change “128” to suit your needs.

For example, 50 MB is suitable for many WordPress installations, but if you run a high-traffic site, 128 MB or 256 MB may be a better fit. Use what works best for your site.

3. Edit the wp-config.php File

By default, Redis is now set up to have any stored data accessible to all apps on the server. Since this is a security risk, it’s important to edit the wp-config.php WordPress core file to include a cache key salt.

Even if you only plan on having WordPress on your server, it’s still crucial not to skip this step in case you decide to install Multisite or include other apps on your server in the future.

Open the wp-config.php file and above the “happy blogging” line, add the following, then save the file to your server:

define( 'WP_CACHE_KEY_SALT', 'example.com:' );

Don’t forget to replace “example.com” with whatever you want. It doesn’t have to be your site’s domain, but be sure it’s unique.

You can randomly generate a key value by using a free tool like Random Key Gen, or KeyGen.io. Be sure to safeguard it like you would your passwords.

4. Installing a PHP Extension

It’s also essential to let WordPress contact the Redis key-value store so you can enable object caching. To do this, you need to install a PHP extension such as PHP Redis.

Go back to the command line and enter the following:

wget https://github.com/nicolasff/phpredis/archive/master.zip

Follow it with the line below:

unzip master.zip

Then, this command:

cd phpredis-master

You now have the source code downloaded and extracted.

To install it, enter this:

phpize

Then, type the line below:

./configure

Follow it with the word below:

make

Also, enter the command below:

sudo make install

Now that PHP Redis is installed, you need to create a new INI file. You can do this with the line below:

sudo echo "extension=redis.so" > /etc/php/7.x/apache2/conf.d/redis.ini

Be sure to replace the “x” in “7.x” to reflect the version of PHP 7 you have installed.

Keep in mind that if you’re not using Apache, the file path will need to be updated to reflect the file hierarchy you have on your server.

5. Restart Redis and PHP

It’s critical you restart PHP and Redis to apply the changes you made above.

You can restart Redis by entering the command below:

sudo service redis-server restart

Then, restart PHP with this:

apache2ctl restart

6. Verify Redis is Working

Now that Redis is installed, it’s time to check that it’s working properly by running the following command:

redis-cli ping

If you see the “PONG” response, Redis is set up and working.

7. Install and Activate the Redis Object Caching Plugin

from:https://wp-rocket.me/blog/redis-object-caching/

[转]Redis Object Caching及如何使用
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Scroll to top
0
Would love your thoughts, please comment.x
()
x