One the first things you learn as a web developer is to “find what can be cached and cache often”. This means that you should look for areas of your applications that are doing the same calculation or lookup over and over on data that doesn’t change much, and store it is a faster to access cache to make retrieval faster and speed up the overall user experience. It also helps reduce load on your servers and enables you to get more capacity out of your hardware by eliminating caching that doesn’t need to be done over and over. Common use cases for external caching including reducing database queries of largely static data ( such as configuration data ), storing the output of computationally heavy calculations, or caching the results of heavy system calls ( such as file system scans ).
Generally speaking, there are two types of external caches available to PHP applications. One is the type that is built into PHP as an extension, typically providing opcode caching as well. These include APC and Wincache, which both work with SugarCRM. The other type are external key/value stores, which interface with PHP via an extension or library. These include Memcache or Redis, both well supported by SugarCRM.
The choice of which setup to use depend upon your application; if you PHP application is fairly small and only resides on one web server, then APC or Wincache are great solutions as there is little to no configuration required to get up and going. However, if you have multiple web servers behind a load balancer, Memcache or Redis become a much better choice as you can have all the web services point to the same Memcache or Redis server/cluster and eliminate the duplication of cache data across web servers.
Sugar has a really easy to use cache layer that can tie into a number of different caching backends out of the box for storing such data in any easy to access manner. This backend works out of the box with the following caching backends…
- Zend Cache
- Memcache ( using either the memcache or memcached extension )
- Redis ( using the PHP Redis extension )
Even if you don’t have any of these backends available, the cache layer still works as an in-request cache of objects, which provides some speed up for commonly accessed items throughout the request. However, you don’t gain the advantage of cache data being available for future requests.
Setup of these extensions are quite simple. If you have APC, Wincache, or Zend Cache extensions installed, Sugar will recognize them and leverage them immediately. For Memcache and Redis, you’ll need to add a small bit to the config.php file to add the support needed…
The nice part about the support for Memcache and Redis is that you can still leave APC or Wincache enabled for opcode caching, and Sugar will automatically recognize that Memcache or Redis is available for external caching and use that instead of APC or Wincache.
Come back tomorrow and I’ll show a bit how the APIs works independent of the backend being used.