@stephen-shopopop/cache
    Preparing search index...

    Class SQLiteCacheStore<Metadata>

    A SQLite-based cache store implementation that provides persistent key-value storage with metadata.

    This class implements a cache mechanism using SQLite as the underlying storage engine, supporting features like TTL (Time To Live), metadata storage, and automatic pruning of expired or excess entries.

    const cache = new SQLiteCacheStore({ filename: 'cache.db' });

    // Store a value with metadata and 1 hour TTL
    cache.set('key', 'value', { source: 'api' }, 3600000);

    // Retrieve a value
    const result = cache.get('key');
    • Uses SQLite WAL (Write-Ahead Logging) mode for better concurrency
    • Supports automatic pruning of expired entries
    • Enforces maximum entry size limits
    • Provides optional entry count limits

    When invalid options are provided in the constructor

    When invalid arguments are provided to set() method

    When attempting to store entries larger than maxEntrySize

    Type Parameters

    • Metadata extends object = Record<PropertyKey, unknown>

      The type of metadata to store alongside cached values. Defaults to Record<PropertyKey, unknown>

    Index

    Constructors

    Accessors

    Methods

    Constructors

    • Creates a new SQLiteCacheStore instance.

      Type Parameters

      • Metadata extends object = Record<PropertyKey, unknown>

      Parameters

      • options: Readonly<SQLiteCacheStoreOptions> = {}

        Configuration options for the SQLite cache store

        • filename

          The filename for the SQLite database

        • timeout

          The timeout value for database operations

        • maxCount

          Maximum number of entries allowed in the cache (must be non-negative integer)

        • maxEntrySize

          Maximum size of a single cache entry in bytes (must be non-negative integer < 2GB)

      Returns SQLiteCacheStore<Metadata>

      When maxEntrySize is not a non-negative integer or exceeds 2GB

      When maxCount is not a non-negative integer

      The constructor initializes the SQLite database with the following optimizations:

      • WAL journal mode
      • NORMAL synchronous mode
      • Memory-based temp store
      • Database optimization

      It also creates necessary tables and indexes for cache operations and prepares SQL statements for common operations like get, update, insert, delete, and count.

    Accessors

    • get size(): number

      Gets the total number of entries in the cache store.

      Returns number

      The total count of cache entries

    Methods

    • Closes the SQLite database connection. This method should be called when the cache store is no longer needed to free up resources.

      Returns void

    • Removes an entry from the SQLite cache store by its key.

      Parameters

      • key: string

        The key of the cache entry to delete

      Returns void

    • Retrieves a cached value and its associated metadata by key

      Parameters

      • key: string

        The unique identifier to lookup in the cache

      Returns undefined | { metadata: Metadata; value: Buffer }

      An object containing the cached value as a Buffer, metadata, or undefined if the key doesn't exist in the cache

    • Sets a value in the SQLite cache store with an optional TTL and metadata.

      Parameters

      • key: string

        The key to store the value under

      • value: string | Buffer<ArrayBufferLike>

        The value to store. Must be a string or Buffer

      • metadata: Metadata = ...

        Optional metadata to store with the value

      • ttl: number = 0

        Time-to-live in milliseconds. 0 means no expiration

      Returns void

      If value is not a string or Buffer

      If TTL is not a non-negative integer

      If value size exceeds maxEntrySize