Memory-Based Caching in ExpressionEngine 2.8

by: Kevin Cupp on: 2/13/2014

ExpressionEngine has always stored cached data on the disk. This is fine for most sites, but for the sites that get heavy traffic, caching to the filesystem can become a bottleneck. The solution is to store cached data in a memory-based cache store for faster retrieval.

In ExpressionEngine 2.8, we’ve brought over caching drivers from CodeIgniter, allowing cached data to be stored in the filesystem or in a memory-based store such as Memcached or Redis. The effects of this change are quite staggering. Our hosting partner, Nexcess, has done extensive testing and benchmarking of memory-based caching versus filesystem caching in ExpressionEngine and has found a huge boost in the amount of concurrent transactions ExpressionEngine can provide when using a memory-based cache store:

We’ve made it a seamless transition to go from file-based caching to memory-based caching. All that’s needed is to select one of the new caching drivers from a setting in the control panel, assuming Memcached or Redis are running on their default ports on the local server. Otherwise, alternative server settings can be specified. It’s even possible to use a Memcached or Redis server used by multiple ExpressionEngine sites without conflicts.

Developer API

Add-on developers manually writing to the cache directory are encouraged to use the new Cache class in ExpressionEngine 2.8. The new Cache class provides an easy interface for storing and retrieving items from the cache without having to worry about which caching driver the site is configured to use.

For example, to save an item to the cache, this line is all that’s needed:

ee()->cache->save('mydata', $data, 300);

That line will save the contents of $data, whether it be a string, array, or even an object, to the cache with a key name of “mydata”. The third parameter specifies how long the item should be cached for in seconds.

Retrieving the data later is just as simple:

$data = ee()->cache->get('mydata');

There are many other ways to work with the cache, such as scoping, namespacing and deleting cache items, all of which will be explained in the documentation for the Cache class.

As add-on developers switch to using the Cache class, site developers can rest assured their site won’t be bogged down by file locks or disk I/O if they need to switch to a memory-based caching option.

Graph provided by Nexcess’s ExpressionEngine, Caching, and Best Practices For Performance whitepaper which is licensed under CC BY-NC-ND 3.0.

.(JavaScript must be enabled to view this email address) or share your feedback on this entry with @eecms on Twitter.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases