class StoredValueCache

Shares a database store's stored values between servers through a cache store.

Each cached value records the partition and scope tokens that were current when it was read. Writes replace those tokens instead of deleting values, so a value read before a write but cached after it no longer matches and is ignored. The database stays authoritative, and an evicted value is read again rather than resolved again.

Constants

DEFAULT_TTL

The default number of seconds values and tokens are cached for.

Methods

__construct(string $table, array $targets, Repository|null $cache = null, int $ttl = self::DEFAULT_TTL)

Create a new stored value cache instance.

bool
storesValues()

Determine if values are read from and published to the cache.

array
read(string $connection, FeaturePartition|null $partition, array $pairs)

Read the cached values of the given features and scopes.

void
publish(string $connection, FeaturePartition|null $partition, array $tokens, array $stored)

Publish the stored JSON values of the given features and scopes under the tokens they were read with.

void
rotateScopes(string $connection, FeaturePartition|null $partition, array $scopes)

Invalidate the cached values of the given scopes.

void
rotatePartition(string $connection, FeaturePartition|null $partition)

Invalidate every cached value in the partition.

void
rotate(array $tokens)

Replace the given tokens in every target cache.

string
storage(string $connection, FeaturePartition|null $partition)

Get the identity of the rows read through the given connection and partition.

string
partitionTokenKey(string $storage)

Get the key of the partition token.

string
scopeTokenKey(string $storage, int|string $scope)

Get the key of a scope's token.

string
valueKey(string $storage, int|string $feature, int|string $scope)

Get the key of a feature's cached value for a scope.

string
key(string $kind, string $identity)

Get the fixed-length cache key of an entry.

string
segment(int|string $value)

Encode a key segment so adjacent segments can't run together.

Details

at line 36
__construct(string $table, array $targets, Repository|null $cache = null, int $ttl = self::DEFAULT_TTL)

Create a new stored value cache instance.

Parameters

string $table
array $targets

the caches and TTLs whose tokens are replaced on writes

Repository|null $cache

the cache values are read from and published to

int $ttl

at line 47
bool storesValues()

Determine if values are read from and published to the cache.

Return Value

bool

at line 61
array read(string $connection, FeaturePartition|null $partition, array $pairs)

Read the cached values of the given features and scopes.

Missing tokens are created before the database is read, so a write that commits afterwards replaces them. Without tokens, nothing read now may be published.

Parameters

string $connection
FeaturePartition|null $partition
array $pairs

scopes keyed by their serialized identifiers

Return Value

array

the cached values, and the tokens to publish the database values under

at line 120
void publish(string $connection, FeaturePartition|null $partition, array $tokens, array $stored)

Publish the stored JSON values of the given features and scopes under the tokens they were read with.

Parameters

string $connection
FeaturePartition|null $partition
array $tokens
array $stored

Return Value

void

at line 139
void rotateScopes(string $connection, FeaturePartition|null $partition, array $scopes)

Invalidate the cached values of the given scopes.

Parameters

string $connection
FeaturePartition|null $partition
array $scopes

Return Value

void

at line 154
void rotatePartition(string $connection, FeaturePartition|null $partition)

Invalidate every cached value in the partition.

Parameters

string $connection
FeaturePartition|null $partition

Return Value

void

at line 168
protected void rotate(array $tokens)

Replace the given tokens in every target cache.

Every target is attempted, since a failure in one doesn't make the others stale.

Parameters

array $tokens

Return Value

void

Exceptions

RuntimeException

at line 196
protected string storage(string $connection, FeaturePartition|null $partition)

Get the identity of the rows read through the given connection and partition.

Stores reading the same rows share entries, so the identity names the storage, not the store.

Parameters

string $connection
FeaturePartition|null $partition

Return Value

string

at line 206
protected string partitionTokenKey(string $storage)

Get the key of the partition token.

Parameters

string $storage

Return Value

string

at line 214
protected string scopeTokenKey(string $storage, int|string $scope)

Get the key of a scope's token.

Parameters

string $storage
int|string $scope

Return Value

string

at line 222
protected string valueKey(string $storage, int|string $feature, int|string $scope)

Get the key of a feature's cached value for a scope.

Parameters

string $storage
int|string $feature
int|string $scope

Return Value

string

at line 233
protected string key(string $kind, string $identity)

Get the fixed-length cache key of an entry.

Scopes and feature names can be longer than some cache stores allow in a key. SHA-256 keeps one scope from being crafted to share another scope's key.

Parameters

string $kind
string $identity

Return Value

string

at line 241
protected string segment(int|string $value)

Encode a key segment so adjacent segments can't run together.

Parameters

int|string $value

Return Value

string