Understand Caching in OpenCart

Nowadays, you can’t afford to go offline even for a couple of minutes if you’re dealing with a global audience as it gives your competitor a chance to prove that they are ahead of you.

Caching is an important factor if you’re dealing with web development. It really helps offload the heavy lifting a web server has to do to serve thousands of requests if not millions concurrently. There are loads of options available nowadays that provide a performance boost to your web application, but the basic concept of caching remains the same.

The Purpose of Caching

Gone are the days when you just needed to build a static HTML website that contained a couple of pages and you were done. In that case, you didn’t have to worry about the concurrent requests and heavy traffic generated on your website, as it’s so much faster to send those static HTML pages compared to the dynamic web pages which take quite a bit of server resources to build themselves.

A typical dynamic page does a lot, from querying a database to fetching data from third-party service providers. It requires a lot to display a page which includes lots of information in different formats. Specifically, you can’t afford to query the database to fetch the same complex information for every request as the traffic increases. It’ll soon make your servers cramped for resources, and before you can do something they'll be gone!

So you need something in-between that speeds things up, and that’s where caching comes into the picture. The idea of caching is very simple—you store the calculated/formatted results somewhere and fetch them when needed so that you don’t have to do it again. That “somewhere” could be anywhere: the filesystem, memory, or the database itself.

Also, there are lots of options available for caching—memcached, Redis, Varnish, and more. You could use those as per your requirements, and sometimes the combination of multiple components also helps improve the performance exponentially.

As it would take a complete series to explain the ins and outs of caching, and also it’s something out of the scope of this article, we’ll get back to the OpenCart context and continue with it in the next section.

We’ll use the latest version of OpenCart, so make sure that you’ve installed that to follow the code.

How Basic Caching Works in OpenCart

The caching library is provided in the core itself, so let’s straight away explore it. Go ahead and open the system/library/cache.php file in your favorite text editor.

Starting with the constructor, it initializes the cache driver passed in the constructor argument. If the class is available for the requested caching adapter, it’ll be initialized to the $this->cache property, otherwise it’ll exit with the error message. We’ll see the different caching adapters in the next section.

Also, there are three wrapper functions which are used to perform all the operations related to caching.

The get method is used to retrieve the value from the cache.

$this->cache->get($key);

The set method is used to store the value in the cache.

$this->cache->set($key, $value);

The delete method is used to delete the key/value mapping from the cache.

$this->cache->delete($key);

So it's really straightforward to use caching functions in your modules as well.

There are lots of places in the front-end where the data is fetched from the cache store. Let's list some of them:

latest products

bestseller products

list of manufacturers

list of countries

list of currencies

list of zones

store settings

In the case of file caching, you'll find all the cache files stored under the system/cache directory. Although OpenCart clears the cache at appropriate events, you could also clear these files manually to fetch the latest data.

So that's it as far as the caching wrapper is concerned. In the next section, we'll see the different caching adapters available in the core and the actual heavy lifting that is done by them.

Caching Adapters

There are three caching adapters available in the core of OpenCart—file, memcache, and apc. The default caching adapter used in OpenCart is file.

Here’s the snippet from index.php which initializes the $cache object with the default caching adapter.

// Cache
$cache = new Cache('file');
$registry->set('cache', $cache);

Unfortunately, there’s no configurable way that allows you to switch the caching adapter as it’s hard-coded. Having said that, you could use OCMOD to change the default caching adapter without altering the core file.

As the implementation of each caching adapter is almost the same, we’ll only look at one of the caching adapters to see what’s going on. Let’s take memcache, for example. Go ahead and open system/library/cache/mem.php. You could also explore the other two adapters file.php and apc.php in the same directory.

Each caching adapter is defined under the “Cache” namespace to avoid conflicts.

Recall those methods that we discussed in the last section—they actually end up here. So, when you call the get method in the Cache class, it actually calls the get method defined in the actual adapter class—in our case it’s the Mem class.

The actual logic of manipulating the cache entries happens in the adapter class methods. As you can see, in the constructor of the Mem class we initialized the Memcache object and established the connection using the pconnect method. Finally, we’re using the get, set and delete methods of the Memcache object to manipulate the cache entries.

On the other hand, if you take a look at the file cache handler implementation, it takes a bit of effort to store and retrieve cache entries using file system functions. Apart from that, there’s no difference in the implementation.

So that’s it as far as caching adapters are concerned in OpenCart. Of course, you could go ahead and make your own custom caching handler if needed. You just need to implement the required methods and you’re done.

That’s it for today. I hope that you’ve enjoyed the tutorial and that it’ll encourage you to use caching in your custom modules as needed.

Conclusion

Today, we’ve discussed caching in OpenCart. We started with the basics of caching, and as we moved on we explored how caching is used in the front-end. Finally, we went through the different caching adapters available in OpenCart.

Feel free to express your thoughts in the form of queries and suggestions using the feed below.