Options
All
  • Public
  • Public/Protected
  • All
Menu

Class TTLDatastore<Value>

TTLDatastore is an implementation of the Datastore interface that supports a time-to-live (TTL) for datastore entries. After the TTL expires on a given key, the entry will be automatically cleared from the datastore unless it is refreshed in the mean time. In this way you can build utilities like session managers where a given session is refreshed with each interaction but expires after a set period of time since the last interaction.

note

TTLDatastore borrows ideas from https://github.com/Level/level-ttl.

Type parameters

  • Value

Hierarchy

  • TTLDatastore

Implements

  • Datastore<Value>

Index

Constructors

constructor

  • TTLDatastore creates a new datastore that supports TTL expirations.

    Parameters

    • store: Datastore<Value>

      The underlying Datastore to wrap with TTL expirations.

    • Default value meta: Datastore<Buffer> = new NamespaceDatastore(store, ttlPrefix)

      A Datastore to use for storing TTL information. Will default to a NamespaceDatastore that stores all TTL metadata in the input store under a ttl prefix. If you don't want to mix your metadata with your keys, consider provider your own store directly.

    • Optional options: TTLDatastoreOptions

      A set of options for controlling the underlying TTL mechanics.

    Returns TTLDatastore

Properties

options

store

store: Datastore<Value>

The underlying Datastore to wrap with TTL expirations.

Methods

batch

  • batch(ttl?: undefined | number): TTLBatch<Value>
  • batch returns a Batch object with which you can chain multiple operations. The operations are only executed upon calling commit. Any put operation on a batch will use the supplied TTL value.

    Parameters

    • Optional ttl: undefined | number

    Returns TTLBatch<Value>

close

  • close(): Promise<void>
  • close the datastore. This should always be called to ensure resources are cleaned up. This will wait until the TTL timer has been cleared before resolving.

    Returns Promise<void>

delete

  • delete(key: Key): Promise<void>
  • delete removes the content stored under the given key.

    Parameters

    • key: Key

      The key.

    Returns Promise<void>

expiration

  • expiration(key: Key): Promise<number>
  • expiration returns the expiration UTC timestamp in milliseconds for a given key.

    Parameters

    • key: Key

      The key.

    Returns Promise<number>

get

  • get(key: Key): Promise<Value>
  • get retrieves the value stored under the given key.

    Parameters

    • key: Key

      The key.

    Returns Promise<Value>

has

  • has(key: Key): Promise<boolean>
  • has checks for the existence of a given key.

    Parameters

    • key: Key

      The key.

    Returns Promise<boolean>

open

  • open(): Promise<void>
  • open the datastore. This is only needed if the store was closed before, otherwise this is taken care of by the constructor. If a store is reopened, the TTL timing is not automatically restarted (@see startTTL).

    Returns Promise<void>

put

  • put(key: Key, value: Value, ttl?: undefined | number): Promise<void>
  • put stores a value with the given key.

    note

    If you put the same entry twice, you refresh the TTL to the last put operation.

    Parameters

    • key: Key

      The key.

    • value: Value

      The value.

    • Optional ttl: undefined | number

      The time-to-live, in milliseconds. Defaults to options.ttl.

    Returns Promise<void>

query

  • query(query: Query<Value>): AsyncIterable<Result<Value>>
  • query searches the store for key/value pairs matching a given query.

    Parameters

    • query: Query<Value>

      Object describing the query parameters.

    Returns AsyncIterable<Result<Value>>

startTTL

  • startTTL(): number | Timeout
  • startTTL starts the underlying TTL timer. This is called by default upon datastore initialization, but can be used to restart a the TTL timer after stopTTL has been called (@see stopTTL).

    Returns number | Timeout

stopTTL

  • stopTTL(): Promise<void>
  • stopTTL clears the underlying TTL timer. If stopped, keys will no longer expire, but any new entires will still be given TTL values, which may expire after the timer is restarted (@see startTTL).

    Returns Promise<void>

ttl

  • ttl(key: Key, ttl?: undefined | number): Promise<void>
  • ttl inserts or updates a TTL for a given key.

    note

    This will update the TTL even if the given key doesn't exist yet, but may in the future.

    Parameters

    • key: Key

      The key.

    • Optional ttl: undefined | number

      The time-to-live, in milliseconds. Defaults to options.ttl.

    Returns Promise<void>