@solomonai/cache
Version:
<div align="center"> <h1 align="center">@solomonai/cache</h1> <h5>Cache all the things</h5> </div>
75 lines (70 loc) • 2.6 kB
TypeScript
import { S as Store, E as Entry, C as CacheError } from './interface-D4FnSgmS.js';
import { M as Metrics } from './metrics-PJjLxZ-U.js';
import { Result } from '@unkey/error';
type StoreMiddleware<TNamespace extends string, TValue = any> = {
wrap: (store: Store<TNamespace, TValue>) => Store<TNamespace, TValue>;
};
type Metric = {
metric: "metric.cache.read";
key: string;
hit: boolean;
status?: "fresh" | "stale";
latency: number;
tier: string;
namespace: string;
} | {
metric: "metric.cache.write";
key: string;
latency: number;
tier: string;
namespace: string;
} | {
metric: "metric.cache.remove";
key: string;
latency: number;
tier: string;
namespace: string;
};
declare function withMetrics(metrics: Metrics<Metric>): StoreMiddleware<any, any>;
/**
* EncryptedStore is a wrapper around a Store that encrypts and decrypts values using the Web Crypto API.
*
* The encryption key is stored in memory and a hash is injected into the cache key to allow rolling the key
* without causing issues with decryption errors.
*
* Rolling the encryption key will therefore invalidate all cache entries.
*
* @example
* ```ts
* let store // some existing store implementation
*
* // generate a key with `openssl rand -base64 32` and load it from the environment
* const encryptionKey = ""
* store = EncryptedStore.fromBase64Key(store, encryptionKey)
* ```
*/
declare class EncryptedStore<TNamespace extends string, TValue = any> implements Store<TNamespace, TValue> {
name: string;
private readonly encryptionKey;
private readonly encryptionKeyHash;
private readonly store;
constructor(opts: {
store: Store<TNamespace, TValue>;
encryptionKey: CryptoKey;
encryptionKeyHash: string;
});
/**
*
*/
private buildCacheKey;
get(namespace: TNamespace, key: string): Promise<Result<Entry<TValue> | undefined, CacheError>>;
set(namespace: TNamespace, key: string, value: Entry<TValue>): Promise<Result<void, CacheError>>;
remove(namespace: TNamespace, key: string): Promise<Result<void, CacheError>>;
private encrypt;
private decrypt;
static fromBase64Key<TNamespace extends string, TValue>(base64EncodedKey: string): Promise<{
wrap: (store: Store<TNamespace, TValue>) => Store<TNamespace, TValue>;
}>;
}
declare function withEncryption<TNamespace extends string, TValue = any>(base64Key: string): Promise<StoreMiddleware<TNamespace, TValue>>;
export { EncryptedStore, type StoreMiddleware, withEncryption, withMetrics };