AtomicCache
in package
uses
ForbidCloning, ForbidSerialization
Tags
Table of Contents
Properties
- $cache : Cache
- $mutex : KeyedMutex
Methods
- __construct() : mixed
- __serialize() : never
- __unserialize() : never
- compute() : TValue
- Obtains the lock for the given key, then invokes the {@code $compute} callback with the current cached value (which may be {@code null} if the key did not exist in the cache). The value returned from the callback is stored in the cache and returned from this method.
- computeIfAbsent() : TValue
- Attempts to get the value for the given key. If the key is not found, the key is locked, the $compute callback is invoked with the key as the first parameter. The value returned from the callback is stored in the cache and returned from this method.
- computeIfPresent() : TValue
- Attempts to get the value for the given key. If the key exists, the key is locked, the $compute callback is invoked with the key as the first parameter and the current key value as the second parameter. The value returned from the callback is stored in the cache and returned from this method.
- delete() : bool|null
- The lock is obtained for the key before deleting the key.
- get() : TValue|TDefault
- Returns the cached value for the key or the given default value if the key does not exist.
- set() : void
- The lock is obtained for the key before setting the value.
- __clone() : mixed
- create() : mixed
- lock() : Lock
Properties
$cache read-only
private
Cache
$cache
$mutex read-only
private
KeyedMutex
$mutex
Methods
__construct()
public
__construct(Cache<string|int, TValue> $cache, KeyedMutex $mutex) : mixed
Parameters
- $cache : Cache<string|int, TValue>
- $mutex : KeyedMutex
__serialize()
public
final __serialize() : never
Return values
never__unserialize()
public
final __unserialize(array<string|int, mixed> $data) : never
Parameters
- $data : array<string|int, mixed>
Return values
nevercompute()
Obtains the lock for the given key, then invokes the {@code $compute} callback with the current cached value (which may be {@code null} if the key did not exist in the cache). The value returned from the callback is stored in the cache and returned from this method.
public
compute(string $key, callable(string, Array): TValue $compute[, int|null $ttl = null ]) : TValue
Parameters
- $key : string
- $compute : callable(string, Array): TValue
-
Receives $key and $value as parameters.
- $ttl : int|null = null
-
Timeout in seconds. The default null $ttl value indicates no timeout.
Tags
Return values
TValuecomputeIfAbsent()
Attempts to get the value for the given key. If the key is not found, the key is locked, the $compute callback is invoked with the key as the first parameter. The value returned from the callback is stored in the cache and returned from this method.
public
computeIfAbsent(string $key, callable(string, null): TValue $compute[, int|null $ttl = null ]) : TValue
Parameters
- $key : string
-
Cache key.
- $compute : callable(string, null): TValue
-
Receives $key as parameter.
- $ttl : int|null = null
-
Timeout in seconds. The default
null$ttl value indicates no timeout.
Tags
Return values
TValuecomputeIfPresent()
Attempts to get the value for the given key. If the key exists, the key is locked, the $compute callback is invoked with the key as the first parameter and the current key value as the second parameter. The value returned from the callback is stored in the cache and returned from this method.
public
computeIfPresent(string $key, callable(string, TValue): TValue $compute[, int|null $ttl = null ]) : TValue
Parameters
- $key : string
-
Cache key.
- $compute : callable(string, TValue): TValue
-
Receives $key and $value as parameters.
- $ttl : int|null = null
-
Timeout in seconds. The default null $ttl value indicates no timeout.
Tags
Return values
TValuedelete()
The lock is obtained for the key before deleting the key.
public
delete(string $key) : bool|null
Parameters
- $key : string
Tags
Return values
bool|nullget()
Returns the cached value for the key or the given default value if the key does not exist.
public
get(string $key[, TDefault $default = null ]) : TValue|TDefault
Parameters
- $key : string
-
Cache key.
- $default : TDefault = null
-
Default value returned if the key does not exist. Null by default.
Tags
Return values
TValue|TDefault —Resolved with null iff $default is null.
set()
The lock is obtained for the key before setting the value.
public
set(string $key, TValue $value[, int|null $ttl = null ]) : void
Parameters
- $key : string
-
Cache key.
- $value : TValue
-
Value to cache.
- $ttl : int|null = null
-
Timeout in seconds. The default
null$ttl value indicates no timeout.
Tags
__clone()
protected
final __clone() : mixed
create()
private
create(Closure $compute, string $key, TValue|null $value, int|null $ttl) : mixed
Parameters
- $compute : Closure
- $key : string
- $value : TValue|null
- $ttl : int|null
Tags
lock()
private
lock(string $key) : Lock
Parameters
- $key : string