
A simple interface to cache data using different backends
1.0.0b6 | Fixed a bug with add() setting a value when it shouldn't if no ttl was given for the file backend 1/12/12 |
---|---|
1.0.0b5 | Added missing documentation for using Redis as a backend 8/25/11 |
1.0.0b4 | Added the database, directory and redis types, added support for the memcached extention and support for custom serialization callbacks 6/21/11 |
1.0.0b3 | Added 0 to the memcache delete method call since otherwise the method triggers notices on some installs 5/10/11 |
1.0.0b2 | Fixed API calls to the memcache extension to pass the TTL as the correct parameter 2/1/11 |
1.0.0b | The initial implementation 4/28/09 |
The cache configuration, used for database, directory and file caches
The array structure for database caches is:
array(
'table' => (string) {the database table to use},
'key_column' => (string) {the varchar column to store the key in, should be able to handle at least 250 characters},
'value_column' => (string) {the text/varchar column to store the value in},
'ttl_column' => (string) {the integer column to store the expiration time in}
)
The array structure for directory caches:
array(
'path' => (string) {the directory path with trailing slash}
)
The array structure for file caches:
array(
'path' => (string) {the file path},
'state' => (string) {clean or dirty, used to appropriately save}
)
array
The data store to use
Either the:
Not used for apc, directory or xcache
mixed
The type of cache
The valid values are:
string
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.
$config is an associative array of configuration options for the various backends. Some backend require additional configuration, while others provide provide optional settings.
The following $config options must be set for the database backend:
The following $config for the following items can be set for all backends:
Common serialization callbacks include:
Please note that using JSON for serialization will exclude all non-public properties of objects from being serialized.
A custom serialize and unserialze option is string, which will cast all values to a string when storing, instead of serializing them. If a __toString() method is provided for objects, it will be called.
fCache __construct( string $type, mixed $data_store=NULL, array $config=array() )
string | $type | The type of caching to use: 'apc', 'database', 'directory', 'file', 'memcache', 'redis', 'xcache' |
mixed | $data_store | The path for a file or directory cache, an Memcache or Memcached object for a memcache cache, an fDatabase object for a database cache or a Redis object for a redis cache - not used for apc or xcache |
array | $config | Configuration options - see method description for details |
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
void __destruct( )
Tries to set a value to the cache, but stops if a value already exists
boolean add( string $key, mixed $value, integer $ttl=0 )
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 |
If the key/value pair were added successfully
Removes all cache entries that have expired
void clean( )
Clears the WHOLE cache of every key, use with caution!
xcache may require a login or password depending on your ini settings.
boolean clear( )
If the cache was successfully cleared
Deletes a value from the cache
boolean delete( string $key )
string | $key | The key to delete |
If the delete succeeded
Returns a value from the cache
mixed get( string $key, mixed $default=NULL )
string | $key | The key to return the value for |
mixed | $default | The value to return if the key did not exist |
The cached value or the default value if no cached value was found
Only valid for file caches, saves the file to disk
void save( )
Serializes a value before storing it in the cache
string serialize( mixed $value )
mixed | $value | The value to serialize |
The serialized value
Sets a value to the cache, overriding any previous value
boolean set( string $key, mixed $value, integer $ttl=0 )
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 |
If the value was successfully saved
Unserializes a value before returning it
mixed unserialize( string $value )
string | $value | The serialized value |
The PHP value