fCacheclassv1.0.0b

A simple interface to cache data using different backends

Changes:
1.0.0bThe initial implementation 4/28/09

Variables

->cacheprotected

The data cache, only used for file caches

The array structure is:

array(
    (string) {key} => array(
        'value'  => (mixed) {the key's value},
        'expire' => (integer) {the timestamp to expire at, 0 for none}
    )
)

Type

array

->data_storeprotected

The data store to use - the file path for a file cache, Memcache object for memcache

Type

mixed

->stateprotected

The data state, only used for file caches

The valid values are:

  • 'clean'
  • 'dirty'

Type

string

->typeprotected

The type of cache

The valid values are:

  • 'apc'
  • 'file'
  • 'memcache'
  • 'xcache'

Type

string

Methods

->__construct() public

Set the type and master key for the cache

A file cache uses a single file to store values in an associative array and is probably not suitable for a large number of keys.

Using an apc or xcache cache will have far better performance than a file or directory, however please remember that keys are shared server-wide.

Signature

fCache __construct( string $type, mixed $data_store=NULL )

Parameters

string $type The type of caching to use: 'apc', 'file', 'memcache', 'xcache'
mixed $data_store The path for a file cache, or an Memcache object for a memcache cache - not used for apc or xcache

->__destruct() internal public

Please note: this method is public, however it is primarily intended for internal use by Flourish and will normally not be useful in site/application code

Cleans up after the cache object

Signature

void __destruct( )

->add() public

Tries to set a value to the cache, but stops if a value already exists

Signature

boolean add( string $key, mixed $value, integer $ttl=0 )

Parameters

string $key The key to store as, this should not exceed 250 characters
mixed $value The value to store, this will be serialized
integer $ttl The number of seconds to keep the cache valid for, 0 for no limit

Returns

If the key/value pair were added successfully

->clear() public

Clears the WHOLE cache of every key, use with caution!

xcache may require a login or password depending on your ini settings.

Signature

void clear( )

->delete() public

Deletes a value from the cache

Signature

void delete( string $key )

Parameters

string $key The key to delete

->get() public

Returns a value from the cache

Signature

mixed get( string $key, mixed $default=NULL )

Parameters

string $key The key to return the value for
mixed $default The value to return if the key did not exist

Returns

The cached value or the default value if no cached value was found

->save() public

Only valid for file caches, saves the file to disk and will randomly clean up expired values

Signature

void save( )

->set() public

Sets a value to the cache, overriding any previous value

Signature

void set( string $key, mixed $value, integer $ttl=0 )

Parameters

string $key The key to store as, this should not exceed 250 characters
mixed $value The value to store, this will be serialized
integer $ttl The number of seconds to keep the cache valid for, 0 for no limit