Cache
Cache
Cache-backed session backend for authentication state.
class Cache implements \Hazaar\Auth\Interface\SessionBackendImplements: Hazaar\Auth\Interface\SessionBackend
Implements Hazaar\Auth\Interface\SessionBackend using Hazaar\Cache\Adapter 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
public string $identityType: string
data
public array $dataType: array
cache
Cache adapter instance used for session storage operations.
private CacheAdapter $cacheType: Hazaar\Cache\Adapter
config
private Options $configType: Hazaar\Auth\Session\Backend\Cache\Options
token
public ExpiringToken $tokenType: Hazaar\Auth\ExpiringToken
refreshToken
public ExpiringToken $refreshTokenType: Hazaar\Auth\ExpiringToken
Methods
__construct
Constructs a new Cache session backend instance.
public __construct(array|Options $config): voidInitializes the session backend with the provided configuration options and sets up the cache adapter for session storage.
Parameters
| Parameter | Type | Description |
|---|---|---|
$config | array | Hazaar\Auth\Session\Backend\Cache\Options |
isEmpty
Checks if the session is empty.
public isEmpty(): boolReturns: bool
Returns 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(): arrayReturns: array
has
Checks if the specified session key exists.
public has(string $key): boolReturns: bool
Parameters
| 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): mixedReturns: mixed
Parameters
| 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): voidReturns: void
Parameters
| 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): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | the key identifying the session value to remove |
clear
Clears all data from the current session.
public clear(): voidReturns: void
This method invokes the clear operation on the session object, if it exists, effectively removing all session data stored in the backend cache.
reset
Resets in-memory identity/token state without touching the underlying cache.
public reset(): voidReturns: void
Unlike Hazaar\Auth\Session\Backend\Cache::clear, the persisted session entries are left in the cache (they'll simply expire on their own, or be picked up again by a subsequent Hazaar\Auth\Session\Backend\Cache::load call). This is called at the end of every request to prevent a reused backend instance from leaking one request's identity into the next, while keeping the underlying Hazaar\Cache\Adapter connection warm.
getToken
Retrieves the current session token.
public getToken(): ?ExpiringTokenReturns: Hazaar\Auth\ExpiringToken
getRefreshToken
Retrieves the refresh token associated with the session.
public getRefreshToken(): ?ExpiringTokenReturns: Hazaar\Auth\ExpiringToken
create
Creates a new session for the given identity and initializes it with optional data.
public create(string $identity, array $data): ExpiringTokenReturns: Hazaar\Auth\ExpiringToken
Generates 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 |
assumeIdentity
Establishes in-memory identity/data without issuing a token or writing to the cache.
public assumeIdentity(string $identity, array $data): voidReturns: void
Unlike Hazaar\Auth\Session\Backend\Cache::create, this does not generate an access/refresh token pair or write anything to the underlying Hazaar\Cache\Adapter — it only sets this instance's in-memory identity/data for the current request. Hazaar\Auth\Session\Backend\Cache::getToken continues to return null afterwards, since no token was issued.
Parameters
| Parameter | Type | Description |
|---|---|---|
$identity | string | |
$data | array |
load
Loads the session data for the given token.
public load(string $tokenString, ?array $sessionData, string $tokenType = 'access', ?string $passphrase): boolReturns: bool
Initializes 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): boolReturns: bool
Parameters
| Parameter | Type | Description |
|---|---|---|
$token | string | the session token to refresh |
getIdentity
Retrieves the identity associated with the current session.
public getIdentity(): ?stringReturns: string
generateTokenString
Generates a unique token string for session authentication.
private generateTokenString(string $identity): stringReturns: string
Uses 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 Fri, 04 Sep 2026 23:32:17 +0000