@arcjet/cache
Advanced tools
+32
-13
@@ -0,21 +1,31 @@ | ||
| /** | ||
| * Interface for a cache. | ||
| */ | ||
| export interface Cache<T = unknown> { | ||
| /** | ||
| * Attempts to retrieve a value from the cache. If a value exists, it will be | ||
| * returned with the remaining time-to-live (in seconds). | ||
| * Retrieve a value from the cache; | ||
| * it will be returned with the remaining time-to-live (in seconds) if it exists. | ||
| * | ||
| * @param namespace A isolated segement of the cache where keys are tracked. | ||
| * @param key The identifier used to retrieve the value. | ||
| * @returns A promise for a 2-element tuple containing the value and TTL in | ||
| * seconds. If no value is retrieved, the value will be `undefined` and the | ||
| * TTL will be `0`. | ||
| * @param namespace | ||
| * Isolated segment of the cache where keys are tracked. | ||
| * @param key | ||
| * Key. | ||
| * @returns | ||
| * Promise for a tuple with the value and TTL in seconds; | ||
| * value will be `undefined` and TTL will be `0` if not found. | ||
| */ | ||
| get(namespace: string, key: string): Promise<[T | undefined, number]>; | ||
| /** | ||
| * If the cache implementation supports storing values, `set` makes a best | ||
| * attempt at storing the value provided until the time-to-live specified. | ||
| * Store a value in the cache. | ||
| * | ||
| * @param namespace A isolated segement of the cache where keys are tracked. | ||
| * @param key The identifier used to store the value. | ||
| * @param value The value to be stored under the key. | ||
| * @param ttl The amount of seconds the value stays valid in the cache. | ||
| * @param namespace | ||
| * Isolated segment of the cache where keys are tracked. | ||
| * @param key | ||
| * Key. | ||
| * @param value | ||
| * Value. | ||
| * @param ttl | ||
| * Number of seconds the entry stays valid. | ||
| * @returns | ||
| * Nothing. | ||
| */ | ||
@@ -31,4 +41,13 @@ set(namespace: string, key: string, value: T, ttl: number): void; | ||
| } | ||
| /** | ||
| * In-memory cache. | ||
| */ | ||
| export declare class MemoryCache<T> implements Cache<T> { | ||
| /** | ||
| * Data. | ||
| */ | ||
| namespaces: Map<string, Bucket<T>>; | ||
| /** | ||
| * Create a new in-memory cache. | ||
| */ | ||
| constructor(); | ||
@@ -35,0 +54,0 @@ get(namespace: string, key: string): Promise<[T | undefined, number]>; |
+9
-0
@@ -31,4 +31,13 @@ function nowInSeconds() { | ||
| } | ||
| /** | ||
| * In-memory cache. | ||
| */ | ||
| class MemoryCache { | ||
| /** | ||
| * Data. | ||
| */ | ||
| namespaces; | ||
| /** | ||
| * Create a new in-memory cache. | ||
| */ | ||
| constructor() { | ||
@@ -35,0 +44,0 @@ this.namespaces = new Map(); |
+5
-6
| { | ||
| "name": "@arcjet/cache", | ||
| "version": "1.0.0-beta.10", | ||
| "version": "1.0.0-beta.11", | ||
| "description": "Arcjet cache interface and implementations", | ||
@@ -47,7 +47,6 @@ "keywords": [ | ||
| "devDependencies": { | ||
| "@arcjet/eslint-config": "1.0.0-beta.10", | ||
| "@arcjet/rollup-config": "1.0.0-beta.10", | ||
| "@arcjet/tsconfig": "1.0.0-beta.10", | ||
| "@rollup/wasm-node": "4.46.2", | ||
| "eslint": "9.32.0", | ||
| "@arcjet/eslint-config": "1.0.0-beta.11", | ||
| "@arcjet/rollup-config": "1.0.0-beta.11", | ||
| "@rollup/wasm-node": "4.50.0", | ||
| "eslint": "9.34.0", | ||
| "typescript": "5.9.2" | ||
@@ -54,0 +53,0 @@ }, |
5
-16.67%126
28.57%18790
-0.01%