I have added a new cache mechanism in Sugar 4.5. It allows the caching and retrieval of arbitrary information within the same round trip, and if you have Zend Platform installed, it will share data with all requests on the server. This API will connect with more accelerators in the future. The user's of the API do not need to worry about whether or not an external cache is present. It is automatically detected and used. If necessary, the external cache can be disabled.
I have used the API in several of the functions in include/utils.php. Many of them were being called multiple times with the same arguments.
The implementation is located: include/utils/external_cache.php
(all code subject to the SPL)
JSON is one of the modules that received the most benefit from the cache.PHP Code:/**
* Retrieve a key from cache. For the Zend Platform, a maximum age of 5 minutes is assumed.
*
* @param String $key -- The item to retrieve.
* @return The item unserialized
*/
function sugar_cache_retrieve($key)
/**
* Put a value in the cache under a key
*
* @param String $key -- Global namespace cache. Key for the data.
* @param Serializable $value -- The value to store in the cache.
*/
function sugar_cache_put($key, $value)
/**
* Clear a key from the cache. This is used to invalidate a single key.
*
* @param String $key -- Key from global namespace
*/
function sugar_cache_clear($key)
/**
* Turn off external caching for the rest of this round trip and for all round
* trips for the next cache timeout. This function should be called when global arrays
* are affected (studio, module loader, upgrade wizard, ... ) and it is not ok to
* wait for the cache to expire in order to see the change.
*/
function sugar_cache_reset()
at the top of the encode method, I added:
When a value is about to be returned, I put the calculated value in the cache.PHP Code:$cache_key = 'JSON_encode_'.$var;
if(is_array($var) || is_object($var))
{
$cache_key = 'JSON_encode_'.md5(serialize($var));
}
// Use the global cache
if($cache_value = sugar_cache_retrieve($cache_key))
{
return $cache_value;
}
JacobPHP Code:$value = json_encode($var);
sugar_cache_put($cache_key, $value);
return $value;


LinkBack URL
About LinkBacks





Bookmarks