Skip to content

Simple in-memory TTL(Time To Live) cache for Deno.

License

Notifications You must be signed in to change notification settings

justjavac/deno_ttl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

deno_ttl

tag ci license

Simple in-memory TTL(Time To Live) cache for Deno.

Usage

import TTL from "https://deno.land/x/ttl/mod.ts";

const ttl = new TTL<string>(10_000);

ttl.addEventListener("hit", (event) => {
  console.log("hit %s with value: %s", event.key, event.val);
});

ttl.set("foo", "bar");
ttl.set("ping", "pong", 20 * 1000);
ttl.set("user", "justjavac", 30 * 1000);

ttl.get("foo"); // > 'bar'
ttl.get("user"); // > 'justjavac'
ttl.get("ping"); // > 'pong'
ttl.get("lol"); // > undefined

// after 10 seconds
ttl.get("foo"); // > undefined
ttl.size; // > 2
ttl.del("ping"); // > 'pong'
ttl.get("ping"); // > undefined
ttl.size; // > 1
ttl.clear();
ttl.size; // > 0

Events

Usage:

ttl.addEventListener("set", (event) => {
  // event.key, event.val, event.expire
});

function onhit(event: TTLEvent<string>) {
  // event.key, event.val, event.expire
}

ttl.addEventListener("hit", onhit);
ttl.removeEventListener("hit", onhit);
  • set - The key has been added or changed.
  • del - The key has been removed manually or due to expiry.
  • expired - The key was expired.
  • hit - The key was found in TTL cache.
  • miss - The key was not found in TTL cache.
  • drop - The key was not been added.

License

deno_ttl is released under the MIT License. See the bundled LICENSE file for details.

About

Simple in-memory TTL(Time To Live) cache for Deno.

Topics

Resources

License

Stars

Watchers

Forks