One area of the app that got a bit of focus during the Sugar 6.2 development cycle is the SugarCache layer. SugarCache is designed to provide a consistent API to the various third party caches available for use with PHP, such as APC, Memcache, Wincache, and Zend Cache. This allows us to easily ( and most often, transparently ) support any of those caches if they are available on your system with little to no configuration needed. We do this by having a factory interface which will detect what caching mechanism is available, and then call the correct backend to add, retrieve, or delete items from the cache.
In Sugar 6.2, we refactored this interface to provide some new abilities, namely
- Added an Object-oriented API in addition to the existing procedural API. Here’s a quick mapping of what the existing procedural methods are and how to do the same with the OOP API.
|Procedural API||OOP API|
|sugar_cache_put($key, $value)||SugarCache::instance()->$key = $value|
- Improved the existing backends for better detection of availability and more efficient use of the given backend APIs.
- Added sugar config setting ‘external_cache_force_backend’ where you can skip the backend detection logic and instead always use the specified caching backend.
- Added an API to Redis using the PHP Redis extension.
- Enable developers to build their own caching backends to use with Sugar. To do this, you’ll need to implement the 4 methods that handle dealing with the external cache itself ( _setExternal(), _getExternal(), _clearExternal(), and _resetExternal() ) along with adding any needed rules for detecting your backend being available to Sugar in the useBackend() method and setting the priority for detection with the $_priority property. Below is the code used for the Redis backend which illustrates this.
These enhancements now provide a much more robust and cleaner way to integrate new and upcoming caching platforms into the product in the future.