This is the documentation for LemonStand V1, which has been discontinued. You can learn more and upgrade your store here.

LemonStand Version 1 Has Been Discontinued

This documentation is for LemonStand Version 1. LemonStand is now offered as a cloud-based eCommerce platform.
You can try the new LemonStand and learn about upgrading here.

Caching API

LemonStand includes a flexible caching API, which allows you to store any items to the cache and restore them when you need. You can use the cache system in custom modules, or in pages. Caching is already integrated into the CMS module, so caching of partials and pages is automated. The cache system store cache items to files, or use APC or Memcache. Each method has its own benefits and drawbacks. File-based caching is available everywhere, because it does not require any additional PHP modules. On the other hand, it is slowest in comparison with two other methods. If you use a VPS or dedicated server, the memory-caching approaches (APC or Memcache) are recommended. However you should allocate enough memory for cache, in order to make the caching efficient. The cache size depends on objects which you cache.

Configuring the cache system

When the caching system is not configured LemonStand uses the dummy cache class. This class does not cache objects, but allows developers to add caching into their modules without the risk to break LemonStand installations which do not have the caching system configured. 

The cache configuration should be specified in the config.php file, with the CACHING parameter. General caching configuration:

  $CONFIG['CACHING'] = array(
    'CLASS_NAME'=>'Core_FileCache', // Caching class name
    'DISABLED'=>false,  // Allows to disable caching on the system level
    'PARAMS'=>array(   // Caching class specific configuration parameters
      'TTL'=>3600       // Default caching items time-to-live
    )
  );

Configuring the file-based caching

The file-based caching is a simplest caching approach. It does not require any additional PHP modules and server components. To configure the file-based caching, specify the Core_FileCache in the caching class name parameter, and path to the cache directory in the PARAMS parameter. The directory should be writable for PHP. Example: 

  $CONFIG['CACHING'] = array(
    'CLASS_NAME'=>'Core_FileCache',
    'DISABLED'=>false,
    'PARAMS'=>array(
      'CACHE_DIR'=>'/users/elf/temporary/ls_cache',
      'TTL'=>3600
    )
  );

Configuring the APC caching

The APC caching requires the APC PHP module to be installed on the server. To enable the APC caching, specify the Core_ApcCache in the caching class name parameter: 

  $CONFIG['CACHING'] = array(
    'CLASS_NAME'=>'Core_ApcCache',
    'DISABLED'=>false,
    'PARAMS'=>array(
      'TTL'=>3600
    )
  );

Configuring the Memcached caching

The Memcached caching requires the Memcache PHP module to be installed on the server. Also there should be at least one memcached daemon accessible from the server. To enable the Memached caching, specify the Core_MemCache in the caching class name parameter and specify a list of memcached servers in the SERVERS parameter: 

  $CONFIG['CACHING'] = array(
    'CLASS_NAME'=>'Core_MemCache',
    'DISABLED'=>false,
    'PARAMS'=>array(
      'TTL'=>3600,
      'SERVERS'=>array('192.168.2.24:11211', '192.168.2.25:11211')
    )
  );

Using the cache system

After configuring the cache system, you can begin using it. First you should request the caching provider object from the Core_CacheBase class with the create() method. This method loads the caching configuration and creates a corresponding caching object. Then you can use the set() and get() methods for saving and loading items. The set() method has 3 parameters - the key name, the value and the time-to-live (TTL) value (in seconds). The file-system caching does not support per-item TTL values, and always uses the value specified in the caching system configuration in the config.php script. The get() method has a single parameter - the item key. You can specify an array of keys of you want to fetch several items at a time. The function returns FALSE if the item has expired or not found in the cache. If you fetch several items with a single call, the function returns an array with keys corresponding the item keys and values corresponding the fetched item values. 

// Create the cache object
$cache = Core_CacheBase::create();

// Store a string value
$cache->set('key1', 'value');

// Store a value which will expire in 30 seconds
$cache->set('expire', 'value', 30);

// Store an array (or any other complex value)
$cache->set('key2', array('a'=>'b'));

// Restore values
echo $cache->get('key1'); // outputs "value"
print_r($cache->get('key2')); // outputs the array('a'=>'b')
print_r($cache->get(array('key1', 'key2'))); // outputs array('key1'=>'value', 'key2'=>array('a'=>'b'))

If you want to create different cache items for different conditions, or if you want your items to expire when catalog, blog or CMS objects update, use the create_key() method of the Core_CacheBase class. Please read the class description to learn about the available method parameters and usage examples.

Caching pages and partials

Using the cache system for caching partials and pages is described in this section.

Next: Third-Party Integrations
Previous: LemonStand Front-End JavaScript Framework
Return to LemonStand Back-End