Skip to content

Latest commit

 

History

History
165 lines (120 loc) · 6.4 KB

cache.md

File metadata and controls

165 lines (120 loc) · 6.4 KB

\arc\cache

This component contains utility methods to ease working with PHP hashes.

\arc\cache::proxy

(\arc\cache\Proxy) \arc\cache::proxy( (mixed) $target, (mixed) $cacheControl )

This method creates a \arc\cache\Proxy object for a target object or callable function. The cacheControl argument is optional. If set and it is an integer, the cache images the proxy generates will be valid for that amount of seconds after creation. If cacheControl is a callable function, for every cache request the cacheControl function will be called with the target object, method name, arguments and the captured result. The cacheControl function must then return an integer specifying the amount of seconds the cache image will be valid.

<?php
    $cachedObject = \arc\cache::proxy( new heavyClass() );
    $result = $cachedObject->aHeavyCall();

The example above will simply cache all calls and property accesses to heavyClass for 7200 seconds in a file. But you can influence the cache time per call:

<?php
    $cachedHTTPClient = \arc\cache::proxy( \arc\http::client(), function($params) {
        return ( \arc\http\headers::parseCacheTime( $params['target']->responseHeaders ) );
    });

This will create a caching http client that reads the cache-control and expires headers from each request and caches the result as long as is allowed by those.

For each call to a cached object, the cacheControl closure is called with an array with four parameters:

  • target The target object that is cached, so you can directly access it for more information, as is done here to get the responseHeaders
  • method The name of the method called
  • arguments An array of arguments passed to the method
  • output Any output generated by the method
  • result The return value of the method

The cache proxy uses locks to add 'Stampede' protection. This means that if many requests to the same object, with the same method and arguments occur at once, only one call will be made to the proxied object. The remainder will wait for this call to finish and fill the cache and then use the cached data. Only if the wait fails, because it takes too long or the cache couldn't be saved somehow, the waiting processes will try to call the target object themselves.

\arc\cache::cache

(mixed) \arc\cache::cache( (string) $name, (callable) $calculateCallback )

This method will return the cached image with the given name, if it is still fresh, or if not call the callable method to generate a new value, store it and return that.

\arc\cache::cd

(\ar\cache\Store) \arc\cache::cd( (string) $path )

Returns a new Store with the given path to store/retrieve cache images.

\arc\cache::create

(\arc\cache\Store) \arc\cache::create( (string) $name )

Creates a new cache store and returns it. The name is used to create a prefix or subdirectory in the default location for the cache store.

\arc\cache::get

(mixed) \arc\cache::get( (string) $name )

Returns the contents of the cache image, even if no longer fresh.

\arc\cache::getCacheStore

(\arc\cache\Store) \arc\cache::getCacheStore()

Returns the cache store from the context object ( \arc\context::$context ) if availabe. Creates a new one if not and stores it in the context.

\arc\cache::getIfFresh

(mixed) \arc\cache::getIfFresh($name, $freshness = 0)

This method returns the value associated with the given name if it is still fresh, otherwise it will return null. You can set a minimum freshness time, either in seconds or as a string parsable by strtotime().

\arc\cache::getInfo

(array|null) \arc\cache::get( (string) $name )

Returns information about the cached image, if available or null otherwise. The information contains the ctime, the mtime and the size of the cache image.

\arc\cache::isFresh

(bool) \arc\cache::isFresh( (string) $name, (int|string) $freshness = 0 )

Returns true if the cache image exists and is still fresh. If a freshness time is given, the cache image must be fresh for at least the given amount of time. A negative time means that a cache image that is no longer strictly fresh, will still be considered fresh enough if it was fresh at the current time minus the freshness argument. Freshness can be passed as either an int with the number of seconds or a string parsable by strtotime.

\arc\cache::lock

(bool) \arc\cache::lock($name, $blocking = false)

This method locks the store for the given name. If blocking is set to true, other processes are blocked from reading any current value. Returns true if the lock succeeded.

\arc\cache::ls

(array) \arc\cache::ls()

Returns an array with the names of cache images in the current path.

\arc\cache::purge

(bool) \arc\cache::purge( (string) $name=null )

This method removes any value for a given name from the store. If the name is a path, it will also remove any values for any child paths.

\arc\cache::remove

(bool) \arc\cache::remove( (string) $name )

This method removes the value for the given name from the store. Returns true if a cache images was actually deleted, false if no cache image was there.

\arc\cache::set

(mixed) \arc\cache::set( (string) $name, (mixed) $value, (mixed) $timeout = null )

This method stores a name - value pair, with either a given timeout or the default timeout for this Store. The $timeout argument can either be a string, in which case it is parsed with strtotime, or an integer with the number of seconds to store the value as 'fresh'.

\arc\cache::timeout

(object) \arc\cache::timeout( (mixed) $timeout )

This method returns a new cache store with the given timeout. In all other respects the new store is a copy of the current one.

\arc\cache::unlock

(bool) \arc\cache::unlock( (string) $name )

This method unlocks the cache store for the given name. Returns true on succes, false on failure.

\arc\cache::wait

(bool) \arc\cache::wait( (string) $name )

Waits for a lock to be unlocked, returns true on success, false on failure. Failure means that the cache image isn't unlocked within the systems timeout period.