class DatabaseStore implements CanFlushLocks, LockProvider, Store (View source)

Traits

Properties

protected ConnectionInterface $connection

The database connection instance.

protected ConnectionInterface $lockConnection

The database connection instance that should be used to manage locks.

protected string $table

The name of the cache table.

protected string $prefix

A string that should be prepended to keys.

protected string $lockTable

The name of the cache locks table.

protected array $lockLottery

An array representation of the lock lottery odds.

protected int $defaultLockTimeoutInSeconds

The default number of seconds that a lock should be held.

protected array|bool|null $serializableClasses

The classes that should be allowed during unserialization.

Methods

int
secondsUntil(DateTimeInterface|DateInterval|int $delay)

Get the number of seconds until the given DateTime.

int
availableAt(DateTimeInterface|DateInterval|int $delay = 0)

Get the "available at" UNIX timestamp.

parseDateInterval(DateTimeInterface|DateInterval|int $delay)

If the given value is an interval, convert it to a DateTime instance.

int
currentTime()

Get the current system time as a UNIX timestamp.

string
runTimeForHumans(float $startTime, float|null $endTime = null)

Given a start time, format the total run time for human readability.

__construct(ConnectionInterface $connection, string $table, string $prefix = '', string $lockTable = 'cache_locks', array $lockLottery = [2, 100], int $defaultLockTimeoutInSeconds = 86400, array|bool|null $serializableClasses = null)

Create a new database store.

mixed
get(string $key)

Retrieve an item from the cache by key.

array
many(array $keys)

Retrieve multiple items from the cache by key.

bool
put(string $key, mixed $value, int $seconds)

Store an item in the cache for a given number of seconds.

bool
putMany(array $values, int $seconds)

Store multiple items in the cache for a given number of seconds.

bool
add(string $key, mixed $value, int $seconds)

Store an item in the cache if the key doesn't exist.

int|bool
increment(string $key, mixed $value = 1)

Increment the value of an item in the cache.

int|bool
decrement(string $key, mixed $value = 1)

Decrement the value of an item in the cache.

int|false
incrementOrDecrement(string $key, int|float $value, Closure $callback)

Increment or decrement an item in the cache.

int
getTime()

Get the current system time.

bool
forever(string $key, mixed $value)

Store an item in the cache indefinitely.

lock(string $name, int $seconds = 0, string|null $owner = null)

Get a lock instance.

restoreLock(string $name, string $owner)

Restore a lock instance using the owner identifier.

bool
touch(string $key, int $seconds)

Adjust the expiration time of a cached item.

bool
forget(string $key)

Remove an item from the cache.

bool
forgetIfExpired(string $key)

Remove an item from the cache if it is expired.

bool
forgetMany(array $keys)

Remove all items from the cache.

bool
forgetManyIfExpired(array $keys, bool $prefixed = false)

Remove all expired items from the given set from the cache.

bool
flush()

Remove all items from the cache.

bool
flushLocks()

Remove all locks from the store.

table()

Get a query builder for the cache table.

lockTable()

Get a query builder for the cache locks table.

getConnection()

Get the underlying database connection.

$this
setConnection(ConnectionInterface $connection)

Set the underlying database connection.

getLockConnection()

Get the connection used to manage locks.

$this
setLockConnection(ConnectionInterface $connection)

Specify the connection that should be used to manage locks.

string
getPrefix()

Get the cache key prefix.

void
setPrefix(string $prefix)

Set the cache key prefix.

string
serialize(mixed $value)

Serialize the given value.

mixed
unserialize(string $value)

Unserialize the given value.

bool
hasSeparateLockStore()

Determine if the lock store is separate from the cache store.

Details

protected int secondsUntil(DateTimeInterface|DateInterval|int $delay)

Get the number of seconds until the given DateTime.

Parameters

DateTimeInterface|DateInterval|int $delay

Return Value

int

protected int availableAt(DateTimeInterface|DateInterval|int $delay = 0)

Get the "available at" UNIX timestamp.

Parameters

DateTimeInterface|DateInterval|int $delay

Return Value

int

protected DateTimeInterface|int parseDateInterval(DateTimeInterface|DateInterval|int $delay)

If the given value is an interval, convert it to a DateTime instance.

Parameters

DateTimeInterface|DateInterval|int $delay

Return Value

DateTimeInterface|int

protected int currentTime()

Get the current system time as a UNIX timestamp.

Return Value

int

protected string runTimeForHumans(float $startTime, float|null $endTime = null)

Given a start time, format the total run time for human readability.

Parameters

float $startTime
float|null $endTime

Return Value

string

__construct(ConnectionInterface $connection, string $table, string $prefix = '', string $lockTable = 'cache_locks', array $lockLottery = [2, 100], int $defaultLockTimeoutInSeconds = 86400, array|bool|null $serializableClasses = null)

Create a new database store.

Parameters

ConnectionInterface $connection
string $table
string $prefix
string $lockTable
array $lockLottery
int $defaultLockTimeoutInSeconds
array|bool|null $serializableClasses

mixed get(string $key)

Retrieve an item from the cache by key.

Parameters

string $key

Return Value

mixed

array many(array $keys)

Retrieve multiple items from the cache by key.

Items not found in the cache will have a null value.

Parameters

array $keys

Return Value

array

bool put(string $key, mixed $value, int $seconds)

Store an item in the cache for a given number of seconds.

Parameters

string $key
mixed $value
int $seconds

Return Value

bool

bool putMany(array $values, int $seconds)

Store multiple items in the cache for a given number of seconds.

Parameters

array $values
int $seconds

Return Value

bool

bool add(string $key, mixed $value, int $seconds)

Store an item in the cache if the key doesn't exist.

Parameters

string $key
mixed $value
int $seconds

Return Value

bool

int|bool increment(string $key, mixed $value = 1)

Increment the value of an item in the cache.

Parameters

string $key
mixed $value

Return Value

int|bool

int|bool decrement(string $key, mixed $value = 1)

Decrement the value of an item in the cache.

Parameters

string $key
mixed $value

Return Value

int|bool

protected int|false incrementOrDecrement(string $key, int|float $value, Closure $callback)

Increment or decrement an item in the cache.

Parameters

string $key
int|float $value
Closure $callback

Return Value

int|false

protected int getTime()

Get the current system time.

Return Value

int

bool forever(string $key, mixed $value)

Store an item in the cache indefinitely.

Parameters

string $key
mixed $value

Return Value

bool

Lock lock(string $name, int $seconds = 0, string|null $owner = null)

Get a lock instance.

Parameters

string $name
int $seconds
string|null $owner

Return Value

Lock

Lock restoreLock(string $name, string $owner)

Restore a lock instance using the owner identifier.

Parameters

string $name
string $owner

Return Value

Lock

bool touch(string $key, int $seconds)

Adjust the expiration time of a cached item.

Parameters

string $key
int $seconds

Return Value

bool

bool forget(string $key)

Remove an item from the cache.

Parameters

string $key

Return Value

bool

bool forgetIfExpired(string $key)

Remove an item from the cache if it is expired.

Parameters

string $key

Return Value

bool

protected bool forgetMany(array $keys)

Remove all items from the cache.

Parameters

array $keys

Return Value

bool

protected bool forgetManyIfExpired(array $keys, bool $prefixed = false)

Remove all expired items from the given set from the cache.

Parameters

array $keys
bool $prefixed

Return Value

bool

bool flush()

Remove all items from the cache.

Return Value

bool

bool flushLocks()

Remove all locks from the store.

Return Value

bool

Exceptions

RuntimeException

protected Builder table()

Get a query builder for the cache table.

Return Value

Builder

protected Builder lockTable()

Get a query builder for the cache locks table.

Return Value

Builder

ConnectionInterface getConnection()

Get the underlying database connection.

Return Value

ConnectionInterface

$this setConnection(ConnectionInterface $connection)

Set the underlying database connection.

Parameters

ConnectionInterface $connection

Return Value

$this

ConnectionInterface getLockConnection()

Get the connection used to manage locks.

Return Value

ConnectionInterface

$this setLockConnection(ConnectionInterface $connection)

Specify the connection that should be used to manage locks.

Parameters

ConnectionInterface $connection

Return Value

$this

string getPrefix()

Get the cache key prefix.

Return Value

string

void setPrefix(string $prefix)

Set the cache key prefix.

Parameters

string $prefix

Return Value

void

protected string serialize(mixed $value)

Serialize the given value.

Parameters

mixed $value

Return Value

string

protected mixed unserialize(string $value)

Unserialize the given value.

Parameters

string $value

Return Value

mixed

bool hasSeparateLockStore()

Determine if the lock store is separate from the cache store.

Return Value

bool