Shm
Shm
System V shared-memory cache backend.
class Shm extends \Hazaar\Cache\BackendExtends: Hazaar\Cache\Backend
Provides very low-latency in-process cache access using SysV shared memory segments and semaphores for synchronization.
This backend supports:
- object storage
- per-value expiry
- keepalive-based expiry extension
- array export
- key-level locking
Namespace mapping is maintained in a dedicated shared segment to resolve namespace-to-segment addresses consistently across processes.
Constants
GC_KEY
private const GC_KEY = '__garbage_collection__'Properties
weight
Backend priority weight used during backend chain ordering.
protected int $weightType: int
config
Typed view of this backend's options.
private Shm\Options $configType: Hazaar\Cache\Backend\Shm\Options
sem
Semaphore guarding namespace/index and shared-memory operations.
private false|\SysvSemaphore $semType: false|SysvSemaphore
shm
Attached shared-memory segment for this namespace.
private false|\SysvSharedMemory $shmType: false|SysvSharedMemory
keepalive
Whether reads should extend item expiry when timeout metadata exists.
private bool $keepaliveType: bool
indexKey
Shared-memory variable key storing namespace index metadata.
private int $indexKeyType: int
locks
private array $locksType: array
Methods
available
Check if the shared memory cache backend is available.
public available(): boolReturns: bool
init
Initializes the shared memory cache backend for a given namespace.
public init(string $namespace): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$namespace | string | the namespace for the cache backend |
close
Closes the shared memory cache.
public close(): boolReturns: bool
has
Check if a key exists in the shared memory cache.
public has(string $key, bool $checkEmpty): boolReturns: bool
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | the key to check |
$checkEmpty | bool | whether to check if the data associated with the key is empty |
get
Retrieves the value associated with the given key from the cache.
public get(string $key): mixedReturns: mixed
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | the key to retrieve the value for |
set
Sets a value in the shared memory cache.
public set(string $key, mixed $value, int $timeout): boolReturns: bool
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | the key to store the value under |
$value | mixed | the value to be stored |
$timeout | int | The timeout for the value in seconds. Default is 0 (no timeout). |
remove
Remove a value from the shared memory cache.
public remove(string $key): boolReturns: bool
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | the key of the value to remove |
clear
Clears the shared memory cache.
public clear(): boolReturns: bool
toArray
Converts the cache data to an array.
public toArray(): arrayReturns: array
count
Returns the number of items in the cache.
public count(): intReturns: int
lock
Locks a cache entry for exclusive access.
public lock(string $key): boolReturns: bool
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | the key of the cache entry |
unlock
Unlock a lock for a given key.
public unlock(string $key): boolReturns: bool
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | the key of the lock to unlock |
findFirstAvailableAddress
private findFirstAvailableAddress(array $index): intReturns: int
Parameters
| Parameter | Type | Description |
|---|---|---|
$index | array |
getIndex
Retrieves the index from the shared memory.
private getIndex(): arrayReturns: array
getAddr
Retrieves the address of a key in the cache.
private getAddr(string $key, bool $create): false|intReturns: false|int
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | the key to retrieve the address for |
$create | bool | whether to create the key if it doesn't exist |
addIndex
Adds an index for the given key in the shared memory cache backend.
private addIndex(string $key): false|intReturns: false|int
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | the key to add an index for |
removeIndex
Remove an entry from the cache index.
private removeIndex(array|string $key): boolReturns: bool
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | array | string |
infoByAddr
Retrieves information stored in shared memory by address.
private infoByAddr(int $addr, bool $noKeepalive): array|boolReturns: array|bool
Parameters
| Parameter | Type | Description |
|---|---|---|
$addr | int | the address of the shared memory variable |
$noKeepalive | bool |
infoByKey
Retrieves information about a cache entry by its key.
private infoByKey(string $key): array|boolReturns: array|bool
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | the key of the cache entry |
Generated by Hazaar API Doc Generator on Fri, 04 Sep 2026 23:32:16 +0000