Cookie Cache Provider
The CCookieCache provider utilizes HTTP Cookies to store cache data. It is shared between the Client Side and Server Side during SSR.
CCookieCache Example
The Cookie Cache Provider is useful when you need cache data to be readily available on both the client side and the server side without a separate payload sync.
TypeScript
import { CCookieCache } from '@katlux/providers'
// 1. Configure the singleton instance
const cookieCache = CCookieCache.getInstance()
cookieCache.configure({
sameSite: 'strict', // 'strict' | 'lax' | 'none' (default: 'lax')
secure: true, // Auto-detected from request protocol if omitted
httpOnly: true, // Effective on server-side SSR cookie writes
path: '/' // Cookie path scope (default: '/')
})
// 2. Or create an instance with custom options
const customCookieCache = new CCookieCache({
sameSite: 'lax',
path: '/portal'
})
// Note: Browser cookies are capped at ~4KB and sent with every request.
await cookieCache.set('cookie-key', { theme: 'dark' }, 86400000)
const value = await cookieCache.get('cookie-key')Constructor & Configuration
Configuration options passed when initializing or updating the provider
| Property / Method | Signature | Description |
|---|---|---|
| getInstance() | static getInstance(): CCookieCache | Retrieves the singleton instance of the Cookie Cache provider. |
| constructor | constructor(options?: ICookieCacheOptions) | Instantiates a new Cookie Cache provider with optional security settings. |
| configure | configure(options: ICookieCacheOptions): void | Updates sameSite, secure, httpOnly, or path settings on the instance. |
ICookieCacheOptions Interface
Security flags and parameters for cookie writes
| Option | Type | Default | Description |
|---|---|---|---|
| sameSite | 'strict' | 'lax' | 'none' | 'lax' | Baseline CSRF defense flag for browser requests. |
| secure | boolean | auto | When omitted, automatically detects https from protocol. Set explicitly to force on/off. |
| httpOnly | boolean | true | Effective for cookies written during server-side SSR execution. (Browsers ignore client-side HttpOnly). |
| path | string | '/' | Scope path for which the cookie is valid. |
Methods & Reactive Properties
Standard methods inherited from ACacheProvider
| Method | Parameters | Returns | Description |
|---|---|---|---|
| get<T> | key: string, nuxtApp?: any | Promise<T | null> | Retrieves the cached data if it exists and hasn't expired |
| set<T> | key: string, data: T, lifetime: number, nuxtApp?: any | Promise<void> | Stores the data with the specified lifetime in milliseconds |
| remove | key: string, nuxtApp?: any | Promise<void> | Removes a specific key from the cache |
| removeByPrefix | prefix: string, nuxtApp?: any | Promise<void> | Removes all keys starting with the specified prefix |
| getKeyList | nuxtApp?: any | Promise<Array<string>> | Returns a list of all active cache keys |
| getRemainingTime | key: string, nuxtApp?: any | Promise<number | null> | Returns the remaining lifetime of a key in milliseconds |
| cleanupExpired | Promise<void> | Forces cleanup of all expired cache entries |