LemonStand Documentation

Core_CacheBase

The Core_CacheBase class is the interface class for the caching API. The caching API supports 3 caching providers - file-based, memcached and APC. Pleas read the caching API section to learn more about LemonStand caching features and configuration.

Class methods

  • static create() - creates an instance of the cache management class. The Core_CacheBase class is a singleton class. This means that if you call the create() method several times, you will get a reference to a same object. Depending on the configured caching provider, the method returns an object of the Core_FileCache, Core_MemCache or Core_ApcCache class. All these classes has same methods. Please see the usage examples below.
  • static create_key($prefix, &$recache, $vary_by = array(), $versions = array()) - helper function, which allows to create item keys, which depend on different current conditions. You can use this function for creating keys, which will expire when store catalog content updates, or different keys for different customer groups.
    The $prefix parameter is just a prefix to distinguish key. If you omit the the $vary_by and $versions parameters, the method will always be returning the same key value, based on the prefix value. 
    The $recache parameter indicates whether the item should be recached. This can happen if you specify any content type in the $versions parameter. Please see examples below.
    The $vary_by parameter allows you to specify which conditions the key value should depend on. The condition list could include system conditions, or custom conditions. The known system vary-by parameters are:
    - url - generates different keys for different page URLs
    - customer - generates different keys for different customers
    - customer_group - generates different keys for different customer group
    - customer_presence - generates different keys depending on whether a customer is logged in or not
    The system vary-by parameters should be specified as strings. You can also specify any other parameters you want the key to depend on. These parameters should be specified as key-value pairs (see the sort order example below).
    The $versions parameter allows you force the item recaching when the store content updates. There are 3 content types which you can specify in the parameter value. If any of the specified content types updates, the function assigns the TRUE value to the $recache parameter. You can use the following version content types:
    - cms - forces recaching if any CMS object (page, partial or template) has been added, updated or deleted
    - catalog - forces recaching if any catalog object (product, category, etc.) has been added, updated or deleted
    - blog - forces recaching if any blog object (post, category or comment) has been added, updated or deleted
    Examples of the create_key() method usage:
    // Create key, which does not depend on any parameter.
    // There is no real sense in using the create_key() function in this case.
    
    $recache = false;
    $key = Core_CacheBase::create_key('some_item', $recache);
    
    // Create key, which depends on the page URL and the CMS content version.
    // Notice that if you use only a single value in the $vary_by and $versions parameter,
    // you can specify values as string.
    
    $recache = false;
    $key = Core_CacheBase::create_key('some_item', $recache, 'url', 'cms');
    
    // Create key, which depends on the page URL, and customer group. Thus, the key will be
    // different for different pages and different customer groups. Also, the key will
    // expire on the CMS or catalog updates.
    
    $recache = false;
    $key = Core_CacheBase::create_key('some_item', $recache, array('url', 'customer_group'), array('cms','catalog'));
    
    // Create key, which depends on the sort order variable. We will be getting different
    // keys for different sort order values. 
    
    $recache = false;
    $sort_order = 'name asc';
    $key = Core_CacheBase::create_key('some_item', $recache, array('sort_order'=>$sort_order));
    
    $sort_order = 'name desc';
    $key = Core_CacheBase::create_key('some_item', $recache, array('sort_order'=>$sort_order));
    

The following example demonstrates a typical usage of the caching feature:

$cache = Core_CacheBase::create();

// Try to load the item value from the cache

$recache = false;
$key = Core_CacheBase::create_key('some_item', $recache, array('url'), array('cms','catalog'));
$value = $cache->get($key);

// If the key has expired, or if there is no item with this key stored (or if it has expired),
// generate new item value and cache it

if ($recache || $value === false)
{
  $value = 'value';
  $cache->set($key, $value);
}
 

Next: shop:payment_profile
Previous: Phpr_Request
Return to Reference