Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/justjavac/deno_ttl
Simple in-memory TTL(Time To Live) cache for Deno.
https://github.com/justjavac/deno_ttl
deno deno-module
Last synced: 4 months ago
JSON representation
Simple in-memory TTL(Time To Live) cache for Deno.
- Host: GitHub
- URL: https://github.com/justjavac/deno_ttl
- Owner: justjavac
- License: mit
- Created: 2021-08-05T06:38:50.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-09-13T04:21:11.000Z (over 2 years ago)
- Last Synced: 2024-10-10T18:37:49.793Z (4 months ago)
- Topics: deno, deno-module
- Language: TypeScript
- Homepage:
- Size: 9.77 KB
- Stars: 8
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# deno_ttl
[![tag](https://img.shields.io/github/release/justjavac/deno_ttl)](https://github.com/justjavac/deno_ttl/releases)
[![ci](https://github.com/justjavac/deno_ttl/actions/workflows/ci.yml/badge.svg)](https://github.com/justjavac/deno_ttl/actions/workflows/ci.yml)
[![license](https://img.shields.io/github/license/justjavac/deno_ttl)](https://github.com/justjavac/deno_ttl/blob/master/LICENSE)
[![](https://img.shields.io/badge/deno-v1.x-green.svg)](https://github.com/denoland/deno)Simple in-memory TTL(Time To Live) cache for Deno.
## Usage
```ts
import TTL from "https://deno.land/x/ttl/mod.ts";const ttl = new TTL(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:
```ts
ttl.addEventListener("set", (event) => {
// event.key, event.val, event.expire
});function onhit(event: TTLEvent) {
// 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](https://github.com/justjavac/deno_ttl) is released under the MIT
License. See the bundled [LICENSE](./LICENSE) file for details.