Bun Cache is a caching library for Bun apps that harnesses the power of the Bun's SQLite to offer a straightforward and efficient caching solution.
To get Bun Cache up and running, you can easily install it using bun cli:
bun add @samocodes/bun-cacheTo leverage Bun Cache, simply create a new instance of the BunCache class and start using its methods:
import { BunCache } from "@samocodes/bun-cache";
const cache = new BunCache(); // new BunCache(true) for persistance
cache.put("my-key", "my-value", 1000); // Store a value with a 1-second TTL
const value = cache.get("my-key");
console.log(value); // 🌟 "my-value"persistance: The persistance mode for the cache. If set totrue, the cache will be stored in the database and will persist across app restarts. If set tofalse, the cache will be stored in memory and will be lost when the app is restarted.
Adds a value to the cache.
key: The key under which to store the value.value: The value to be stored.ttl: The time-to-live for the value, in milliseconds.- Returns:
trueif the value was successfully stored,falseotherwise.
Retrieves the value associated with a key from the cache.
key: The key for which to fetch the value.- Returns: The value if the key exists and hasn't expired,
nullotherwise.
Removes a key from the cache.
key: The key to be deleted.- Returns:
trueif the key was successfully deleted,falseotherwise.
Checks if a key exists in the cache.
key: The key to be checked- Returns:
trueif the key exists,falseotherwise
Bun Cache is distributed under the MIT License.