🚀 Socket Launch Week Day 4:Socket MCP Adds Org Alerts, Threat Feed Review, and Package Inspection.Learn more
Sign In

@arcjet/cache

Package Overview
Dependencies
Maintainers
2
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@arcjet/cache

Arcjet cache interface and implementations

Source
npmnpm
Version
1.0.0-beta.8
Version published
Weekly downloads
70K
27.77%
Maintainers
2
Weekly downloads
 
Created
Source
Arcjet Logo

@arcjet/cache

npm badge

Arcjet cache interface and implementations.

Installation

npm install -S @arcjet/cache

API

Caches implement the Cache interface over a specific type:

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).
   *
   * @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`.
   */
  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.
   *
   * @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.
   */
  set(namespace: string, key: string, value: T, ttl: number): void;
}

One such implementation is provided as MemoryCache:

MemoryCache#constructor()

Instantiate the MemoryCache using new MemoryCache() without any arguments.

MemoryCache#get(namespace: string, key: string): Promise<[T | undefined, number]>

Attempts to retrieve a value from the cache. If a value exists, it will be returned with the remaining time-to-live (in seconds).

Non-string arguments will cause the returned promise to reject.

MemoryCache#set(namespace: string, key: string, value: T, ttl: number): void

Makes a best attempt at storing the value provided until the time-to-live specified.

Non-string arguments will cause the function to throw.

Example

import { MemoryCache } from "@arcjet/cache";

const cache = new MemoryCache();

const namespace = "myNamespace";
const key = "myKey";

const ttlSeconds = 60; // seconds

const valueToCache = 1;

cache.set(namespace, key, valueToCache, ttlSeconds);

const [cachedValue, ttl] = await cache.get(namespace, key);
if (cachedValue && ttl > 0) {
  // do something with cached value
}

License

Licensed under the Apache License, Version 2.0.

FAQs

Package last updated on 28 May 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts