cacheController

Provides an interface to a caching mechanism with multiple backends. Used to cache data to a file or database table (dependent on writer).

The controller is the primary interface to the cache layer. It requires a cacheWriter object, defaulting to the file writer if none is specified. The controller will pass calls into the writer to check if an item is cached or not.

It is a good idea to not rely on the generatedCacheId as this is only known after data has been stored and unless you store this separately there will be no way of retrieving the cache information. The generated cacheId is an SHA1 key of the serialised data.

As all writers serialise data before storing it, whatever you wish to store should be serialisable first. If your object requires connections then you should implement __sleep and __wakeup to remove / set these. Alternatively, you should implement the serializable interface.

Garbage Collection is handled by runGc(). This is based on a random number between 1 and $_GcInterval; if 1 is produced, GC is run. Therefore, if $_GcInterval is 100, there is a 1 in 100 chance of the GC running. This should be configured based on the application load (number of requests per second), too low and the cache will always be cleared, too high and it will never be cleared.

Some examples of usage:

// adding to the cache
$oObject = new stdClass();
$oObject->id = 'some id string';

$oCacheCtrl = cacheController::getInstance(
    new cacheWriterFile()
);
$oCacheCtrl->setCacheId(get_class($oObject).'_'.$oObject->id);
$oCacheCtrl->setData($oObject);
$oCacheCtrl->cache();

// checking the cache
$oCacheCtrl = cacheController::getInstance(
    new cacheWriterFile()
);
$oCacheCtrl->setCacheId('stdClass_some id string');
if ( $oCacheCtrl->isCached() ) {
    // do something
}

$oCacheCtrl = cacheController::getInstance(
    new cacheWriterFile()
);
$oCacheCtrl->setCacheId('stdClass_some id string');
if ( $oCacheCtrl->isCached() ) {
    $oCacheCtrl->clearCache();
}

Methods

public __construct($inWriter, [$inCacheId = null])

Returns new instance of cache controller

public static getInstance([$inWriter = null], [$inCacheId = null])

Returns the instance of the cache controller, if no writer is specified default to file writer

public reset()

Reset controller to defaults

public generateCacheId()

If no key has been set, creates an SHA1 hash of the current data set

public isCached()

Returns true if the current cache id has been cached

public isExpired()

Returns true if the cache needs a refresh

public getCache()

Attempts to load the cache data from the key

public cache([$inData = null])

Caches the data currently set, or $inData if supplied

public clearCache()

Clears the cache for the current key

public runGc()

Runs garbage collection at a random point configured by GcInterval

public setModified([$status = true])

Set the status of the object if it has been changed

public getCacheId()

Returns the cacheId

public setCacheId($inCacheId)

Sets the cacheId

public getData()

Returns $_Data

public setData($inData)

Set $_Data to $inData

public getWriter()

Returns $_Writer

public setWriter($inWriter)

Set $_Writer to $inWriter

public getLifetime()

Returns $_Lifetime

public setLifetime($inLifetime)

Set $_Lifetime to $inLifetime

public getGcInterval()

Returns $_GcInterval

public setGcInterval($inGcInterval)

Set $_GcInterval to $inGcInterval

<  1  >