Cache
Cache
Cache-backed session backend for authentication state.
class Cache implements \Hazaar\Auth\Session\Backend\SessionBackendImplements {@see SessionBackend} using {@see CacheAdapter} to persist auth session payloads keyed by generated tokens. The backend stores identity and arbitrary session data, and supports token-based load/read/write semantics used by auth middleware and adapters.
Notes:
- Session token refresh is not supported by this backend.
- Storage adapter and path are configurable, with sensible defaults.
Properties
identity
Stores the identity of the authenticated user.
private string $identitycache
Cache adapter instance used for session storage operations.
private CacheAdapter $cacheconfig
private array $configtoken
public ExpiringToken $tokenrefreshToken
public ExpiringToken $refreshTokenMethods
__construct
Constructs a new Cache session backend instance.
public __construct(array $config): voidInitializes the session backend with the provided configuration options, merging them with the default values. Sets up the cache adapter for session storage.
Configuration options:
- adapter: Cache adapter type (default: 'file')
- use_pragma: Whether to use pragma headers (default: false)
- expire: Session expiration time in seconds (default: 3600)
- refresh: Session refresh interval in seconds (default: 86400)
- idle: Idle timeout in seconds (default: 0)
- path: Path to session cache storage (default: runtime cache path)
- namespace: Cache namespace (default: 'hazaar-auth')
- keysize: Session key size (default: 64)
- slotsize: Session slot size (default: 32)
Parameters
| Parameter | Type | Description |
|---|---|---|
$config | array | configuration options for the session backend |
isEmpty
Checks if the session is empty.
public isEmpty(): boolReturns true if the session ID is not set or the session data is null, indicating that there is no active session.
read
Reads the current session data and returns it as an array.
public read(): voidhas
Checks if the specified session key exists.
public has(string $key): boolParameters
| Parameter | Type | Description |
|---|---|---|
$key | string | the session key to check for existence |
get
Retrieves a value from the session using the specified key.
public get(string $key): mixedParameters
| Parameter | Type | Description |
|---|---|---|
$key | string | the key identifying the session value to retrieve |
set
Sets a value in the session using the provided key.
public set(string $key, mixed $value): voidParameters
| Parameter | Type | Description |
|---|---|---|
$key | string | the key under which the value will be stored in the session |
$value | mixed | the value to store in the session |
Removes a value from the session storage by its key.
public (string $key): voidParameters
| Parameter | Type | Description |
|---|---|---|
$key | string | the key identifying the session value to remove |
clear
Clears all data from the current session.
public clear(): voidThis method invokes the clear operation on the session object, if it exists, effectively removing all session data stored in the backend cache.
getToken
Retrieves the current session token.
public getToken(): ?ExpiringTokengetRefreshToken
Retrieves the refresh token associated with the session.
public getRefreshToken(): ?ExpiringTokencreate
Creates a new session for the given identity and initializes it with optional data.
public create(string $identity, array $data): ExpiringTokenGenerates a unique session ID using a SHA-256 hash of a unique string based on the identity. Instantiates a new CacheAdapter for session storage and populates it with the provided data.
Parameters
| Parameter | Type | Description |
|---|---|---|
$identity | string | The unique identifier for the session (e.g., user ID). |
$data | array | optional associative array of session data to initialize the session with |
load
Loads the session data for the given token.
public load(string $tokenString, ?array $sessionData, string $tokenType = 'access', ?string $passphrase): boolInitializes the session using the specified cache adapter and configuration, then reads the session data into the provided reference variable.
Parameters
| Parameter | Type | Description |
|---|---|---|
$tokenString | string | the session token string to load |
$sessionData | array | |
$tokenType | string | |
$passphrase | string |
refresh
Refreshes the session token.
public refresh(string $token): boolParameters
| Parameter | Type | Description |
|---|---|---|
$token | string | the session token to refresh |
getIdentity
Retrieves the identity associated with the current session.
public getIdentity(): ?stringgenerateTokenString
Generates a unique token string for session authentication.
private generateTokenString(string $identity): stringUses the SHA-256 hashing algorithm on a unique identifier based on the user's identity.
Parameters
| Parameter | Type | Description |
|---|---|---|
$identity | string |
Generated by Hazaar API Doc Generator on Tue, 21 Apr 2026 04:00:24 +0000