\BattisSimpleCache

An object to manage a simple cache

The cache is backed by a MySQL database, with data stored as key => data pairs. All data stored is must be serializable.

Summary

Methods
Properties
Constants
__construct()
setSqlInfo()
setSql()
setTableName()
setKeyName()
setCacheName()
setLifetime()
buildCache()
getCache()
purgeExpired()
setCache()
resetCache()
No public properties found
DEFAULT_LIFETIME
IMMORTAL_LIFETIME
MYSQL_TIMESTAMP
sqlInitialized()
$sql
$initialized
$table
$key
$cache
$lifetime
N/A
No private methods found
No private properties found
N/A

Constants

DEFAULT_LIFETIME

DEFAULT_LIFETIME

Default cache lifetime (1 hour)

IMMORTAL_LIFETIME

IMMORTAL_LIFETIME

Cache length never expires

MYSQL_TIMESTAMP

MYSQL_TIMESTAMP

MySQL Timestamp format

Properties

$sql

$sql : \mysqli

Type

\mysqli — MySQL database handle

$initialized

$initialized : boolean

Type

boolean — If the cache database tables have been created

$table

$table : string

Type

string — Cache table in database

$key

$key : string

Type

string — Cache key field in database

$cache

$cache : string

Type

string — Cache storage field in database

$lifetime

$lifetime : integer

Type

integer — Cache lifetime

Methods

__construct()

__construct(\mysqli  $sql = null, string  $table = null, string  $key = null, string  $cache = null, boolean  $purge = false) : void

Create a a new SimpleCache

Parameters

\mysqli $sql

(Optional)

string $table

(Optional) Cache table name in database

string $key

(Optional) Cache key field name in database

string $cache

(Optional) Cache storage field name in database

boolean $purge

(Optional) Automatically purge expired items upon construction (Defaults to FALSE)

setSqlInfo()

setSqlInfo(string  $host, string  $user, string  $password, string  $database) : boolean

Set up database connection

Parameters

string $host
string $user
string $password
string $database

Returns

boolean —

Returns TRUE on success, FALSE on failure

setSql()

setSql(\mysqli  $sql) : boolean

Set up database connection

Parameters

\mysqli $sql

Returns

boolean —

Returns TRUE on success, FALSE on failure

setTableName()

setTableName(string  $table) : boolean

Set cache table name

Parameters

string $table

Returns

boolean —

Returns TRUE on success, FALSE on failure

setKeyName()

setKeyName(string  $key) : boolean

Set cache key field name

Parameters

string $key

Returns

boolean —

Returns TRUE on success, FALSE on failure

setCacheName()

setCacheName(string  $cache) : boolean

Set cache storage field name

Parameters

string $cache

Returns

boolean —

Returns TRUE on success, FALSE on failure

setLifetime()

setLifetime(integer  $lifetimeInSeconds = self::DEFAULT_LIFETIME) : \Battis\void;

Set default lifetime of cached data

This lifetime will be used for all new and updated caches that do not explicitly override it.

Parameters

integer $lifetimeInSeconds

Defaults to DEFAULT_LIFETIME, values less than zero are treated as zero

Returns

\Battis\void;

buildCache()

buildCache(string  $table = null, string  $key = null, string  $cache = null) : boolean

Create cache table in database

Parameters

string $table

(Optional) Cache table name

string $key

(Optional) Cache key field name

string $cache

(Optional) Cache storage field name

Returns

boolean —

Returns TRUE on success, FALSE on failure

getCache()

getCache(string  $key) : mixed|boolean

Get available cached data

Parameters

string $key

Returns

mixed|boolean —

Returns the cached data or FALSE if no data cached

purgeExpired()

purgeExpired(boolean  $useLocalLifetime = false) : boolean

Purge expired cache data

By default, this purges only cached data that has its own expiration set explicitly, however, if the option to $useLocalLifetime is set to TRUE, the cache lifetime default (set by setLifetime(), defaulting to DEFAULT_LIFETIME) will be compared the timestamps as well, purging cache data without explicitly set expirations.

Parameters

boolean $useLocalLifetime

(Optional) Defaults to FALSE

Returns

boolean —

Returns TRUE on success, FALSE on failure

setCache()

setCache(string  $key, mixed  $data,   $lifetime = null) : boolean

Store data in cache

Parameters

string $key
mixed $data

Must be serializable

$lifetime

Returns

boolean —

Returns TRUE on success, FALSE on failure

resetCache()

resetCache(string  $key) : boolean

Reset (empty) cached data

Parameters

string $key

Returns

boolean —

Returns TRUE on success, FALSE on failure

sqlInitialized()

sqlInitialized() : boolean

Test for database connection initialization

If the database connection is not initalized, attempt to do so.

Throws

\Battis\SimpleCache_Exception

DATABASE_NOT_INITIALIZED If the backing database connection cannot be initialized

Returns

boolean —

Returns TRUE if database connection is ready