@stash-it/ttl-plugin@0.0.3
latest
TTL plugin for stash-it. Store your items with time to live.
This package works with Node.js, BrowsersIt is unknown whether this package works with Cloudflare Workers, Deno, Bun
JSR Score
70%
Published
a day ago (0.0.3)
@stash-it/ttl-plugin
@stash-it/ttl-plugin
is a plugin for @stash-it/stash-it
that allows you to add a TTL.
Time to live (TTL) refers to the amount of time or “hops” that an item is set to exist inside a storage before being discarded by stash-it.
When to use it?
If you need to set a time limit for how long an item should be stored in the storage, this plugin is for you. For instance, some data is only relevant for a certain amount of time, and after that time, it should be removed from the storage.
Installation
npm
npm install @stash-it/ttl-plugin
deno
deno add @stash-it/ttl-plugin
yarn
yarn dlx jsr add @stash-it/ttl-plugin
pnpm
pnpm dlx jsr add @stash-it/ttl-plugin
bun
bunx jsr add @stash-it/ttl-plugin
Usage
// Import stash-it main class. import { StashIt } from "@stash-it/stash-it"; // Import the plugin. import { createTtlPlugin } from "@stash-it/ttl-plugin"; // For it to work you will also need an adapter. // You can use any of the @stash-it adapters or create your own. import { MemoryAdapter } from "@stash-it/memory-adapter"; // Create an instance of the adapter. const adapter = new MemoryAdapter(); // Create an instance of stash-it. const stash = new StashIt(adapter); // Create an instance of the plugin. const plugin = createPrefixSuffixPlugin({ ttl: 3_600, // 1 hour }); // Register plugin stash.registerPlugins([plugin]); // Use it. await stash.set("key", "value"); const item = await stash.get("key"); const exists = await stash.has("key"); console.log(exists); // true console.log(item?.value); // 'value' console.log(item?.extra); // { ttl: 3_600, createdAt: '2024-08-26T20:26:41.832Z' } // Wait for 1 hour (and a bit more) const item2 = await stash.get("key"); console.log(item2); // undefined
Add Package
deno add jsr:@stash-it/ttl-plugin
Import symbol
import * as ttl_plugin from "@stash-it/ttl-plugin";
---- OR ----
Import directly with a jsr specifier
import * as ttl_plugin from "jsr:@stash-it/ttl-plugin";
Add Package
npx jsr add @stash-it/ttl-plugin
Import symbol
import * as ttl_plugin from "@stash-it/ttl-plugin";
Add Package
yarn dlx jsr add @stash-it/ttl-plugin
Import symbol
import * as ttl_plugin from "@stash-it/ttl-plugin";
Add Package
pnpm dlx jsr add @stash-it/ttl-plugin
Import symbol
import * as ttl_plugin from "@stash-it/ttl-plugin";
Add Package
bunx jsr add @stash-it/ttl-plugin
Import symbol
import * as ttl_plugin from "@stash-it/ttl-plugin";