PHP Class MatthiasMullie\Scrapbook\Adapters\SQL

This abstract class should be a "fits all DB engines" normalization. It's up to extending classes to optimize for that specific engine.
Author: Matthias Mullie ([email protected])
Inheritance: implements MatthiasMullie\Scrapbook\KeyValueStore
Show file Open project: matthiasmullie/scrapbook

Protected Properties

Property Type Description
$client PDO
$table string

Public Methods

Method Description
__construct ( PDO $client, string $table = 'cache' )
add ( $key, $value, $expire )
cas ( $token, $key, $value, $expire )
decrement ( $key, $offset = 1, $initial, $expire )
delete ( $key )
deleteMulti ( array $keys )
flush ( )
get ( $key, &$token = null )
getMulti ( array $keys, array &$tokens = null )
increment ( $key, $offset = 1, $initial, $expire )
replace ( $key, $value, $expire )
set ( $key, $value, $expire )
setMulti ( array $items, $expire )
touch ( $key, $expire )

Protected Methods

Method Description
clearExpired ( ) Expired entries shouldn't keep filling up the database. Additionally, we will want to remove those in order to properly rely on INSERT (for add) and UPDATE (for replace), which assume a column exists or not, not taking the expiration status into consideration.
doIncrement ( string $key, integer $offset, integer $initial, integer $expire ) : integer | boolean Shared between increment/decrement: both have mostly the same logic (decrement just increments a negative value), but need their validation & use of non-ttl native methods split up.
expire ( integer $expire ) : null | string Transforms expiration times into TIMESTAMP (Y-m-d H:i:s) format, which DB will understand and be able to compare with other dates.
init ( ) Create the database/indices if it does not already exist.
serialize ( mixed $value ) : string | integer I originally didn't want to serialize numeric values because I planned on incrementing them in the DB, but revisited that idea.
unserialize ( mixed $value ) : mixed | integer | float Numbers aren't serialized for storage size purposes.

Method Details

__construct() public method

public __construct ( PDO $client, string $table = 'cache' )
$client PDO
$table string

add() public method

public add ( $key, $value, $expire )

cas() public method

public cas ( $token, $key, $value, $expire )

clearExpired() protected method

An expired column should simply not exist.
protected clearExpired ( )

decrement() public method

public decrement ( $key, $offset = 1, $initial, $expire )

delete() public method

public delete ( $key )

deleteMulti() public method

public deleteMulti ( array $keys )
$keys array

doIncrement() protected method

Shared between increment/decrement: both have mostly the same logic (decrement just increments a negative value), but need their validation & use of non-ttl native methods split up.
protected doIncrement ( string $key, integer $offset, integer $initial, integer $expire ) : integer | boolean
$key string
$offset integer
$initial integer
$expire integer
return integer | boolean

expire() protected method

Transforms expiration times into TIMESTAMP (Y-m-d H:i:s) format, which DB will understand and be able to compare with other dates.
protected expire ( integer $expire ) : null | string
$expire integer
return null | string

flush() public method

public flush ( )

get() public method

public get ( $key, &$token = null )

getMulti() public method

public getMulti ( array $keys, array &$tokens = null )
$keys array
$tokens array

increment() public method

public increment ( $key, $offset = 1, $initial, $expire )

init() abstract protected method

Create the database/indices if it does not already exist.
abstract protected init ( )

replace() public method

public replace ( $key, $value, $expire )

serialize() protected method

However, not serializing numbers still causes some small DB storage gains and it's safe (serialized data can never be confused for an int).
protected serialize ( mixed $value ) : string | integer
$value mixed
return string | integer

set() public method

public set ( $key, $value, $expire )

setMulti() public method

public setMulti ( array $items, $expire )
$items array

touch() public method

public touch ( $key, $expire )

unserialize() protected method

Numbers aren't serialized for storage size purposes.
protected unserialize ( mixed $value ) : mixed | integer | float
$value mixed
return mixed | integer | float

Property Details

$client protected property

protected PDO $client
return PDO

$table protected property

protected string $table
return string