Memcached is a general-purpose distributed memory caching system that is aimed at speeding up dynamic database-driven websites. It is used to reduce the number of times an external data source (like a database or API) is read by caching data and objects in RAM.
The Memcached system uses a client–server architecture. The server maintain a key–value associative array, while the clients populate this array and query it. Keys are up to 250 bytes long and values can be at most 1 megabyte in size.
The Memcached system is used by some of the most traffic heavy sites such as YouTube, Reddit, Facebook, Twitter. It is also supported by some popular CMSs such as Drupal, Joomla, and WordPress.
To use Memcached with PHP, go to PHP Configuration and enable memcached extension.
Related help topics
-
What is Memcached?
Memcached is an open source distributed memory caching system. It is used for speeding up dynamic web applications by reducing database load. In other words, every time a database request is made it adds additional load to the server.
Memcached reduces that load by storing data objects in dynamic memory (think of it as short-term memory for applications). Memcached stores data based on key-values for small arbitrary strings or objects including:
- Results of database calls
- API calls
- Page rendering
- Memcached is made up of four main components. These components allow the client and the server to work together in order to deliver cached data as efficiently as possible:- Client software - Which is given a list of available Memcached servers
- A client-based hashing algorithm - Chooses a server based on the "key"
- Server software - Stores values and their keys into an internal hash table
- LRU - Determines when to throw out old data or reuse memory -
How to use Memcached?
The Memcached system is used by some of the most traffic heavy sites such as YouTube, Facebook, Twitter.
It is also supported by some popular CMSs such as Drupal, Joomla, and WordPress.To use Memcached with PHP, please enable memcached extension from the control panel at Advanced -> PHP Settings -> Edit php.ini -> memcached On.
To have Memcached enabled your plan needs to have Memcached instances available. In case you need to add one or more, you can purchase it as an upgrade from the Add/Upgrade service(s) section in the hosting control panel.
Each instance goes with 16 MB of memory which is why the Memcached instances go with Memcached memory.
Important: many applications require host and port to link them with Memcached. On our system the Memcached works on a Socket instead of Port.
In this case you need to use
Memcached hostname(IP): unix:///home/sys/memcached.sock
Memcached Port: 0Example: $memcached->addServer('unix:///home/sys/memcached.sock', 0);
-
Enable Memcached on Joomla Website
You can speed up your Joomla site by using memcached.
To enable memcached on Joomla, you need to modify a few settings in your Joomla configuration.php file first.
You can find this file in your Joomla site’s root folder. Using your favorite FTP software, edit this file and find the following lines:
public $caching = '0';
public $cache_handler = 'file';Change these lines to the following:
public $caching = '2';
public $cache_handler = 'memcache';Then, immediately after the two lines you edited, add these two lines:
public $memcache_server_host = ‘/home/sys/memcached.sock’;
public $memcache_server_port = ‘0’;After you are done making all the changes, save the configuration file.
Configure Memcached in Joomla Settings
Now, we will configure memcached on our Joomla-based website.
Login to your Joomla site’s admin panel and navigate to System -> Global Configuration.
Click on the server tab at the top and scroll down to Cache Settings. From the drop-down menu select Persistent(Memcached).
Note: Make sure to enter the correct Memcached server host and port for your server.
Memcache(d) Server Host: /home/sys/memcached.sock
Memcache(d) Server Port: 0Save the settings.
You’ve successfully enabled memcached on your Joomla website.
-
Enable Memcached on Wordpress Website
In this guide, we’re going to use the free extension W3 Total Cache to configure WordPress with Memcached.
If you’re already using another extension such as WP Super Cache, you’ll need to deactivate this and install the W3 Cache in order to follow this guide.Here are the steps to follow to configure W3 Total Cache with Memcached:
1. Sign in to your WordPress dashboard
2. In the main menu, go to Performance then click on General Settings
3. Activate Memcached for the following options:- Page Cache Method
- Minify Cache method
- Database cache method-> Save the settings
For Memcached hostname:port / IP:port use: /home/sys/memcached.sock:0
Save the settings and you’ve successfully enabled memcached on your Wordpress website.
-
Memcached test script
For your convenience we would like to provide you with a sample working Memcached test script:
<?php
$memcached = new Memcached();
$memcached->addServer('unix:///home/sys/memcached.sock', 0);
$name = 'testkey';
$ttl = 10;
$data = sha1(time());
$memcached->set($name, $data, $ttl);
echo date('His') . ': key "' . $name . '" set to "' . $data . '" with ttl ' . $ttl . PHP_EOL;
for ($i = 0; $i < ($ttl + 5); $i ++) {
$res = $memcached->get($name);
echo date('His') . ': key "' . $name . '" data is "' . $res . '" and that is ' . ($res == $data ? 'a match' : 'not a match') . PHP_EOL;
sleep(1);
}
?>Please note that instead of $memcached->addServer('127.0.0.1", 11211); we use socket: $memcached->addServer('unix:///home/sys/memcached.sock', 0);