/** * A (very) simple wrapper for localStorage and sessionStorage, written with expirable cachability in mind. * * Author: McNielsen * Copyright Alert Logic Inc., 2019. */ import { AlStopwatch } from './al-stopwatch'; /** * @public * * AlCabinet provides a simple, generic interface for caching arbitrary data. It supports optional serialization to localStorage * and sessionStorage, where available, but will gracefully fallback to in-memory-cache mode in their absence. */ export declare class AlCabinet { name: string; data: any; type: number; /** * The following three constants indicate which type of storage should be used for a given Cabinet. */ static LOCAL: number; static EPHEMERAL: number; static PERSISTENT: number; static openCabinets: { [cabinetName: string]: AlCabinet; }; syncronizer?: AlStopwatch; noStorage: boolean; constructor(name: string, data?: any, type?: number); /** * Instantiates a persistent information cache (uses localStorage), deserializing data from the provided name if it exists. * * @param rawName - The name of the data cluster. * * @returns A cabinet instance that can be used to interrogate/update the data. */ static persistent(rawName: string): AlCabinet; /** * Instantiates a temporary information cache (uses sessionStorage), deserializing data from the provided name if it exists. * * @param rawName - The name of the data cluster. * * @returns A cabinet instance that can be used to interrogate/update the data. */ static ephemeral(rawName: string): AlCabinet; /** * Instantiates a local cache (uses no storage or persistence). * * @param name - The name of the data cluster * * @returns A cabinet instance that can be used just to hold arbitrary data. */ static local(name: string): AlCabinet; /** * Retrieves a property from the cabinet. * * @param property - The name of the property. * @param defaultValue - The value to return if the property doesn't exist (defaults to `null`). * @param disableExpiration - Indicates whether or not time-based expiration rules should be honored. * * @returns The value of the property (or provided default) */ get(property: string, defaultValue?: any, disableExpiration?: boolean): any; /** * Checks to see if a property is present in the cabinet. * * @param property - The name of the property. * * @returns `true` if the property exists, `false` otherwise. */ exists(property: string): boolean; /** * Checks to see if a given property is expired. * * @param property - The name of the property to check expiration for. * * @returns `true` if the property either does not exist or has expired, `false` otherwise. */ expired(property: string): boolean; /** * Sets a property in the cabinet (and schedules synchronization) * * @param property - The name of the property. * @param value - The value to set it to. * @param ttl - The number of seconds the data should be retained for. Defaults to `0` (indefinite). * * @returns A reference to the cabinet instance, so that calls to it may be chained. */ set(property: string, value: any, ttl?: number): this; /** * Touches a property in the cabinet (and schedules synchronization), effectively extending the * cached item's TTL. * * @param property - The property to touch. * @param ttl Number of seconds the data should be retained for. */ touch(property: string, ttl?: number): void; /** * Deletes a property in the cabinet (and schedules synchronization) * * @param property - The property to be deleted. * * @returns A reference to the cabinet instance, so that calls to it may be chained. */ delete(property: string): this; /** * Destroys the current cabinet, discarding any contents it may have. */ destroy(): void; /** * Synchronizes data back into the storage facility after performing a garbage collection run. * * @returns The class instance. */ synchronize: () => this; }