class DatabaseStore implements CanFlushLocks, LockProvider, Store

Traits

Properties

protected ConnectionResolverInterface $resolver

The database connection resolver.

protected string|null $connectionName

The connection name.

protected string|null $lockConnectionName

The lock connection name.

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.

protected SerializableClassPolicy|null $serializableClassPolicy

The shared serializable class policy.

Methods

int
secondsUntil(DateInterval|DateTimeInterface|int $delay)

Get the number of seconds until the given DateTime.

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

Get the "available at" UNIX timestamp.

parseDateInterval(DateInterval|DateTimeInterface|int|null $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(ConnectionResolverInterface $resolver, string|null $connectionName, string $table, string $prefix = '', string $lockTable = 'cache_locks', array $lockLottery = [2, 100], int $defaultLockTimeoutInSeconds = 86400, array|bool|null $serializableClasses = null, SerializableClassPolicy|null $serializableClassPolicy = null)

Create a new database store.

connection()

Get a fresh connection from the pool.

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.

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

Increment the value of an item in the cache.

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

Decrement the value of an item in the cache.

bool|int
incrementOrDecrement(string $key, int $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 multiple 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
supportsFlushingLocks()

Determine if the store can currently flush locks.

bool
flushLocks()

Remove all locks from the store.

int
pruneExpired()

Remove all expired entries from the cache.

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.

setConnection(string $connectionName)

Set the connection name for the cache store.

getLockConnection()

Get the connection used to manage locks.

setLockConnection(string $connectionName)

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

in InteractsWithTime at line 17
protected int secondsUntil(DateInterval|DateTimeInterface|int $delay)

Get the number of seconds until the given DateTime.

Parameters

DateInterval|DateTimeInterface|int $delay

Return Value

int

in InteractsWithTime at line 29
protected int availableAt(DateInterval|DateTimeInterface|int|null $delay = 0)

Get the "available at" UNIX timestamp.

Parameters

DateInterval|DateTimeInterface|int|null $delay

Return Value

int

in InteractsWithTime at line 41
protected DateTimeInterface|int parseDateInterval(DateInterval|DateTimeInterface|int|null $delay)

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

Parameters

DateInterval|DateTimeInterface|int|null $delay

Return Value

DateTimeInterface|int

in InteractsWithTime at line 57
protected int currentTime()

Get the current system time as a UNIX timestamp.

Return Value

int

in InteractsWithTime at line 65
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

at line 79
__construct(ConnectionResolverInterface $resolver, string|null $connectionName, string $table, string $prefix = '', string $lockTable = 'cache_locks', array $lockLottery = [2, 100], int $defaultLockTimeoutInSeconds = 86400, array|bool|null $serializableClasses = null, SerializableClassPolicy|null $serializableClassPolicy = null)

Create a new database store.

Parameters

ConnectionResolverInterface $resolver
string|null $connectionName
string $table
string $prefix
string $lockTable
array $lockLottery
int $defaultLockTimeoutInSeconds
array|bool|null $serializableClasses
SerializableClassPolicy|null $serializableClassPolicy

at line 104
protected ConnectionInterface connection()

Get a fresh connection from the pool.

Return Value

ConnectionInterface

at line 112
mixed get(string $key)

Retrieve an item from the cache by key.

Parameters

string $key

Return Value

mixed

at line 122
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

at line 163
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

at line 171
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

at line 198
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

at line 214
bool|int increment(string $key, int $value = 1)

Increment the value of an item in the cache.

Parameters

string $key
int $value

Return Value

bool|int

at line 224
bool|int decrement(string $key, int $value = 1)

Decrement the value of an item in the cache.

Parameters

string $key
int $value

Return Value

bool|int

at line 234
protected bool|int incrementOrDecrement(string $key, int $value, Closure $callback)

Increment or decrement an item in the cache.

Parameters

string $key
int $value
Closure $callback

Return Value

bool|int

at line 276
protected int getTime()

Get the current system time.

Return Value

int

at line 284
bool forever(string $key, mixed $value)

Store an item in the cache indefinitely.

Parameters

string $key
mixed $value

Return Value

bool

at line 292
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

at line 309
Lock restoreLock(string $name, string $owner)

Restore a lock instance using the owner identifier.

Parameters

string $name
string $owner

Return Value

Lock

at line 317
bool touch(string $key, int $seconds)

Adjust the expiration time of a cached item.

Parameters

string $key
int $seconds

Return Value

bool

at line 328
bool forget(string $key)

Remove an item from the cache.

Parameters

string $key

Return Value

bool

at line 336
bool forgetIfExpired(string $key)

Remove an item from the cache if it is expired.

Parameters

string $key

Return Value

bool

at line 344
protected bool forgetMany(array $keys)

Remove multiple items from the cache.

Parameters

array $keys

Return Value

bool

at line 357
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

at line 376
bool flush()

Remove all items from the cache.

Return Value

bool

at line 386
bool supportsFlushingLocks()

Determine if the store can currently flush locks.

Return Value

bool

at line 396
bool flushLocks()

Remove all locks from the store.

Return Value

bool

Exceptions

RuntimeException

at line 410
int pruneExpired()

Remove all expired entries from the cache.

Return Value

int

at line 420
protected Builder table()

Get a query builder for the cache table.

Return Value

Builder

at line 428
protected Builder lockTable()

Get a query builder for the cache locks table.

Return Value

Builder

at line 436
ConnectionInterface getConnection()

Get the underlying database connection.

Return Value

ConnectionInterface

at line 447
DatabaseStore setConnection(string $connectionName)

Set the connection name for the cache store.

Boot-only. Persists on the cached store for the worker lifetime; per-request use races across coroutines.

Parameters

string $connectionName

Return Value

DatabaseStore

at line 457
ConnectionInterface getLockConnection()

Get the connection used to manage locks.

Return Value

ConnectionInterface

at line 468
DatabaseStore setLockConnection(string $connectionName)

Specify the connection that should be used to manage locks.

Boot-only. Persists on the cached store for the worker lifetime; per-request use races across coroutines.

Parameters

string $connectionName

Return Value

DatabaseStore

at line 478
string getPrefix()

Get the cache key prefix.

Return Value

string

at line 489
void setPrefix(string $prefix)

Set the cache key prefix.

Boot-only. Persists on the cached store for the worker lifetime; per-request use races across coroutines.

Parameters

string $prefix

Return Value

void

at line 497
protected string serialize(mixed $value)

Serialize the given value.

Parameters

mixed $value

Return Value

string

at line 514
protected mixed unserialize(string $value)

Unserialize the given value.

Parameters

string $value

Return Value

mixed

at line 537
bool hasSeparateLockStore()

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

Return Value

bool