class PdoStore implements StoreInterface

PdoStore is a StoreInterface implementation using a PDO connection.

Lock metadata are stored in a table. You can use createTable() to initialize a correctly defined table.

CAUTION: This store relies on all client and server nodes to have synchronized clocks for lock expiry to occur at the correct time. To ensure locks don't expire prematurely; the TTLs should be set with enough extra time to account for any clock drift between nodes.

Methods

__construct(PDO|Connection|string $connOrDsn, array $options = array(), float $gcProbability = 0.01, int $initialTtl = 300)

You can either pass an existing database connection as PDO instance or a Doctrine DBAL Connection or a DSN string that will be used to lazy-connect to the database when the lock is actually used.

save(Key $key)

Stores the resource if it's not locked by someone else.

waitAndSave(Key $key)

Waits until a key becomes free, then stores the resource.

putOffExpiration(Key $key, $ttl)

Extends the ttl of a resource.

delete(Key $key)

Removes a resource from the storage.

bool
exists(Key $key)

Returns whether or not the resource exists in the storage.

void
createTable()

Creates the table to store lock keys which can be called once for setup.

Details

__construct(PDO|Connection|string $connOrDsn, array $options = array(), float $gcProbability = 0.01, int $initialTtl = 300)

You can either pass an existing database connection as PDO instance or a Doctrine DBAL Connection or a DSN string that will be used to lazy-connect to the database when the lock is actually used.

List of available options: * db_table: The name of the table [default: lock_keys] * db_id_col: The column where to store the lock key [default: key_id] * db_token_col: The column where to store the lock token [default: key_token] * db_expiration_col: The column where to store the expiration [default: key_expiration] * db_username: The username when lazy-connect [default: ''] * db_password: The password when lazy-connect [default: ''] * db_connection_options: An array of driver-specific connection options [default: array()]

Parameters

PDO|Connection|string $connOrDsn A \PDO or Connection instance or DSN string or null
array $options An associative array of options
float $gcProbability Probability expressed as floating number between 0 and 1 to clean old locks
int $initialTtl The expiration delay of locks in seconds

Exceptions

InvalidArgumentException When first argument is not PDO nor Connection nor string
InvalidArgumentException When PDO error mode is not PDO::ERRMODE_EXCEPTION
InvalidArgumentException When namespace contains invalid characters
InvalidArgumentException When the initial ttl is not valid

save(Key $key)

Stores the resource if it's not locked by someone else.

waitAndSave(Key $key)

Waits until a key becomes free, then stores the resource.

If the store does not support this feature it should throw a NotSupportedException.

putOffExpiration(Key $key, $ttl)

Extends the ttl of a resource.

If the store does not support this feature it should throw a NotSupportedException.

Parameters

Key $key
$ttl

Exceptions

LockConflictedException
NotSupportedException

delete(Key $key)

Removes a resource from the storage.

Parameters

Key $key

Exceptions

LockReleasingException

bool exists(Key $key)

Returns whether or not the resource exists in the storage.

Parameters

Key $key

Return Value

bool

void createTable()

Creates the table to store lock keys which can be called once for setup.

Return Value

void

Exceptions

PDOException When the table already exists
DBALException When the table already exists
DomainException When an unsupported PDO driver is used