Memcached, LSMCD and Redis (Object Cache) Support in LSCWP

Please Note: This wiki is valid for v2.9.x and below of the LiteSpeed Cache Plugin for WordPress. If you are using v3.0 or above, please see the new documentation.

As of version 1.8, LiteSpeed Cache for WordPress supports Object Cache.

An object cache stores the results of expensive and/or frequent database queries in a way that makes them easy to retrieve, and eliminates the need for repeated access to the database. Object caching greatly reduces the time it takes to retrieve query results.

For example, your WordPress site's options are stored in the database. Site options include things like the site's name and URL. Every time a page is assembled for a visitor, it is necessary to access the database to read the site options. As you can imagine, these repeated queries for the same information represent wasted resources. With an object cache, you can query the database once, and save the results for a set period of time. During that time, whenever a page must be assembled, WordPress can get the site information from the cache. Accessing object cache is a much less resource-intensive prospect than accessing a database.

Some queries are time-consuming, and other queries are repeated frequently. Both of these scenarios can be improved by storing the query results in object cache.

Note: If you have a site that is fully-cached by LSCWP, you won't use object cache very often. Object cache is only necessary when WordPress is building a page through PHP. If PHP is not being invoked (and minimizing PHP usage is the goal with LSCache) then there are no queries to process and therefore nothing to look up in object cache.

LSCWP doesn't provide object caching directly. Rather, it supports your use of an external object cache such as Memcached or LiteSpeed's drop-in Memcached replacement, LSMCD.

Install Memcached, LSCMD or Redis and PHP Extension

You will need a working and fully tested installation of Redis, Memcached, or LSMCD, as well as the related PHP extension (i.e. php-memcached or php-redis) in order to make your object cache work properly with WordPress.

Installation of the above software is outside of the scope of this wiki, but we have other wikis that may help:

Config Object Cache in LSCWP

If you are using LSMCD, Memcached or Redis, you can set up LSCWP support in the Cache Settings tab. Navigate to LiteSpeed Cache > Settings > Advanced and scroll down to Object Cache. You will need to give LSCWP some parameters, including where your Memcached or LSMCD lives, which objects you'd like to have cached, and how long you want objects to remain in cache, among other things.

Before enabling Object Cache, the default values with already be filled in for you, like so:

After enabling Object Cache, the LSCache plugin will automatically run both connection testing and Memcached/Redis extension detection.

Detailed instructions for all of these settings can be found here.

There are not too many methods to check the Object Cache log, but if you set debug to ON or Admin IP, and view your page source code, you should see something like this at the bottom of the code:

<!-- Object Cache  [total] 5190 [hit_incall] 5056 [hit] 6 [miss_incall] 21 [miss] 107 [set] 171 -->

total is the total number of objects the page requested.

hit_incall is the number of objects that did not hit Memcached but hit the runtime data from above.

hit is the number of objects retrieved from Memcached.

miss_incall is the number of objects not set in runtime. That is to say, when php ran into the current line, no data was set before.

miss is the number of objects not found in Memcached.

set is the number of objects set in Memcached.

If your Connection Test shows Failed like in the following image, there are a few things you can try.

  1. Try service memcached status, to make sure the service is active (running).
  2. Try ss -lptun | grep 11211, to make sure the Memcached port is listening.
  3. Try telnet localhost 11211, to make sure you can connect to localhost successfully.

You can create test PHP files to test connection

For Memcached:

<?php

$conn = new Memcached ;
$address = '/path/to/memcached.sock' ; // set the address here
$port = 0 ;                            // set the port
$conn->addServer(  $address, $port ) ;
var_dump( $address ) ;
var_dump( $port ) ;
var_dump( $conn->getStats() ) ;
echo '<hr>';
var_dump($conn->getServerList());
?>

For redis:

<?php

$cfg_host = 'redis address' ;
$cfg_port = '6379' ; // or 0 if use socket
$cfg_pswd = '' ; // Set if has
$cfg_db = 0 ;


$conn = new Redis() ;
$conn->connect( $cfg_host, $cfg_port ) ;
if ( $cfg_pswd ) $conn->auth( $cfg_pswd ) ;
if ( $cfg_db ) $conn->select( $cfg_db ) ;

var_dump( $conn->ping() ) ; // Should give a `+PONG`
?>
  • Admin
  • Last modified: 2020/05/04 13:55
  • by Shivam Saluja