Created
September 23, 2024 04:45
-
-
Save ynwd/0d97acd5b1948e7d2bd2a39afec59b8e to your computer and use it in GitHub Desktop.
Store: key & value map with TTL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Store } from "https://fastro.dev/core/map/map.ts"; | |
// init store with options. | |
// you have to prepare the repository | |
// and GITHUB_TOKEN if you want to save | |
// to Github repository | |
const store = new Store({ | |
owner: "fastrodev", | |
repo: "fastro", | |
branch: "store", | |
path: "modules/store/store.json", | |
token: Deno.env.get("GITHUB_TOKEN"), | |
}); | |
// sync with github repository | |
store.sync(); | |
// set key and value | |
store.set("key1", "hello"); | |
// set key and value with 4000ms TTL | |
store.check("key2").set("key2", "hello2", 4000); | |
// Check, set, and save it to the repository. | |
// Please note that committing takes some time. | |
// It takes around 3 seconds to save to GitHub. | |
await store.check("key3").set("key3", "hello3").commit(); | |
// get the value from the in-memory map | |
const r1 = await store.get("key1"); | |
console.log(r1); // hello | |
const r2 = await store.get("key2"); | |
console.log(r2); // hello2 | |
// wait 5000ms to make sure the key is already expired | |
await new Promise((resolve) => setTimeout(resolve, 5000)); | |
const r3 = await store.get("key2"); | |
console.log(r3); // undefined | |
const r4 = await store.get("key3"); | |
console.log(r4); // hello3 | |
// clear the map | |
store.clear(); | |
// delete the map | |
store.delete("key1"); | |
store.delete("key3"); | |
// delete the map and the github file | |
await store.destroy(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment