ar\cache

This module provides a low-level data cache as well as a cache proxy class that can wrap a caching layer around any type of object.

Example 1: simple data caching:

<pinp>
  if ( !$image = ar('cache')->getIfFresh( 'cached' ) ) {
    $image = expensiveOperation();
    ar('cache')->set( 'cached', $image );
  }
  echo $image;
</pinp>

Example 2: caching http client:

<pinp>
  $cachingHTTPClient = ar('cache')->proxy( 
    ar('http')->client(), 
    '30 minutes' 
  );
</pinp>

Example 3: data caching with horde protection:

<pinp>
  $cacheName = 'cached_image_name';
  if ( !$image = ar('cache')->getIfFresh( $cacheName ) ) {
    if ( ar('cache')->lock( $cacheName ) ) {
      $image = expensiveOperation();
      ar('cache')->set( $cacheName, $image, '2 hours' );
    } else if ( ar('cache')->wait( $cacheName ) ) {
      // lock failed, so another process is generating the cache
      // continue here when the lock is released
      $image = ar('cache')->get( $cacheName );
    } else {
      // lock isn't released in a reasonable time
      // generate an error, reload or in this case use the stale image:
      $image = ar('cache')->get( $cacheName );
    }
  }
  echo $image;
</pinp>

methods

(void)configConfigure default settings, e.g. the default cache store
(object)createReturns a new cache store.
(mixed)getReturns the cached image or null if not available.
(mixed)getIfFreshReturns the cached image only if it is still fresh, as defined when the image was last saved.
(bool)lockTries to acquire a non-blocking lock on the cache image.
(bool)waitWaits for the cache file to become unlocked or a system timeout.
(bool)setStores an image with the given name and freshness.
(mixed)infoReturns information about a cache image.
(bool)clearRemoves a given cache image.
(bool)purgeRemoves a given cache image and all cache images that are children of it.
(object)proxyReturns a new cache proxy object for the given object.

config