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) | config | Configure default settings, e.g. the default cache store |
(object) | create | Returns a new cache store. |
(mixed) | get | Returns the cached image or null if not available. |
(mixed) | getIfFresh | Returns the cached image only if it is still fresh, as defined when the image was last saved. |
(bool) | lock | Tries to acquire a non-blocking lock on the cache image. |
(bool) | wait | Waits for the cache file to become unlocked or a system timeout. |
(bool) | set | Stores an image with the given name and freshness. |
(mixed) | info | Returns information about a cache image. |
(bool) | clear | Removes a given cache image. |
(bool) | purge | Removes a given cache image and all cache images that are children of it. |
(object) | proxy | Returns a new cache proxy object for the given object. |