### Full Cache Implementation Example Source: https://github.com/bepalo/cache/blob/main/README.md A comprehensive example demonstrating various cache features including LRU, TTL, and event logging. ```ts import { Cache } from "."; const timestampRef = performance.now(); const timestamp = () => `${(performance.now() - timestampRef).toFixed()}ms: `; const log = console.log; const cache = new Cache({ // now: () => Date.now(), // defaultExp: () => Date.now() + 5000, defaultMaxAge: 5000, // cleanupInterval: 5000, // expiryBucketSize: 5000, lruMaxSize: 3, // getExpired: true, // deleteExpiredOnGet: true, onGetHit: async (cache, key, entry) => log(timestamp(), "cache-hit", key), onGetMiss: async (cache, key, reason) => log(timestamp(), reason, key), onDelete: async (cache, key, entry, reason) => log(timestamp(), "Evict", reason, key), onDeleteExpired: async (count) => count > 0 && log(timestamp(), `Expired ${count} entries.`), }); cache.set("item-1", "'sample entry 1'"); cache.set("item-2", "'sample entry 2'", { exp: Date.now() + 2000 }); cache.set("item-3", "'sample entry 3'", { maxAge: 3000 }); cache.set("item-4", "'sample entry 4'", { maxAge: 2500 }); setTimeout(() => log(timestamp(), "1 get", cache.get("item-4")?.value), 1000); setTimeout(() => log(timestamp(), "2 get", cache.get("item-4", { expired: true })?.value), 2000); setTimeout(() => log(timestamp(), "3 peek", cache.peek("item-4")?.value), 3000); setTimeout(() => log(timestamp(), "4 peek", cache.peek("item-4", { expired: true })?.value), 3000); setTimeout( () => log(timestamp(), "5 get", cache.get("item-4", { deleteExpired: true })?.value), 3000, ); setTimeout(() => log(timestamp(), "6 get", cache.get("item-4", { expired: true })?.value), 4000); for (const [key, entry] of cache) { log(timestamp(), key, entry); } ``` ```sh 1ms: Evict LRU item-1 3ms: item-2 { value: "'sample entry 2'", exp: 1752603303022, } 4ms: item-3 { value: "'sample entry 3'", exp: 1752603304022, } 4ms: item-4 { value: "'sample entry 4'", exp: 1752603303522, } 1003ms: cache-hit item-4 1003ms: 1 get 'sample entry 4' 2003ms: cache-hit item-4 2003ms: 2 get 'sample entry 4' 3003ms: 3 peek undefined 3003ms: 4 peek 'sample entry 4' 3004ms: cache-hit item-4 3004ms: Evict deleted item-4 3003ms: 5 get undefined 4003ms: missing item-4 4003ms: 6 get undefined ``` -------------------------------- ### Install @bepalo/cache Source: https://github.com/bepalo/cache/blob/main/README.md Use these commands to add the package to your project using common Node.js and Bun package managers. ```sh bun add @bepalo/cache # or pnpm add @bepalo/cache # or npm install @bepalo/cache # or yarn add @bepalo/cache ``` -------------------------------- ### Basic Cache Operations Source: https://github.com/bepalo/cache/blob/main/README.md Demonstrates initializing the cache, setting values, retrieving them, and deleting entries. ```js import { Cache } from "@bepalo/cache"; const cache = new Cache(); cache.set("hello", "world"); console.log(cache.get("hello")?.value); // => "world" cache.delete("hello"); console.log(cache.get("hello")); // => undefined ``` -------------------------------- ### Benchmark System Information Source: https://github.com/bepalo/cache/blob/main/README.md Hardware and software environment details used for the performance benchmarks. ```js CPU: AMD Ryzen 7 5700U (8 cores, 16 threads) RAM: 16 GB DDR4 OS: Pop!_OS 22.04 (Linux 6.8) Node.js: v22.6.0 ``` -------------------------------- ### Configuring TTL (Time to Live) Source: https://github.com/bepalo/cache/blob/main/README.md Sets a default expiration time for cache entries using the defaultMaxAge option. ```js const cache = new Cache({ defaultMaxAge: 1000, // 1 second TTL }); cache.set("foo", 123); setTimeout(() => { console.log(cache.get("foo")); // => undefined (expired) }, 1500); ``` -------------------------------- ### Import @bepalo/cache in Deno Source: https://github.com/bepalo/cache/blob/main/README.md Import the Cache class directly using npm or jsr specifiers. ```ts import { Cache } from "npm:@bepalo/cache"; // or import { Cache } from "jsr:@bepalo/cache"; ``` -------------------------------- ### Configuring LRU Eviction Source: https://github.com/bepalo/cache/blob/main/README.md Limits the cache size using lruMaxSize, which automatically removes the least recently used items when the limit is reached. ```js const cache = new Cache({ lruMaxSize: 2, }); cache.set("a", 1); cache.set("b", 2); cache.set("c", 3); // "a" gets evicted (least recently used) console.log(cache.has("a")); // => false console.log(cache.has("b")); // => true ``` -------------------------------- ### Using Event Hooks Source: https://github.com/bepalo/cache/blob/main/README.md Registers callbacks for cache lifecycle events such as hits, misses, and deletions. ```js const cache = new Cache({ deleteExpiredOnGet: true, onGetHit: (cache, key, entry) => { console.log(`Hit: ${key}`); }, onGetMiss: (cache, key, reason) => { console.log(`Miss: ${key} (${reason})`); }, onDelete: (cache, key, entry, reason) => { console.log(`Deleted: ${key} (${reason})`); }, onDeleteExpired: (count) => { console.log(`Expired entries removed: ${count}`); }, }); cache.set("x", 42, { maxAge: 10 }); cache.get("y"); // triggers `onGetMiss` setTimeout(() => { cache.get("x"); // triggers `onGetHit`, `onDelete`, `onDeleteExpired` }, 100); ``` -------------------------------- ### Custom Expiration and Cleanup Source: https://github.com/bepalo/cache/blob/main/README.md Uses a function for default expiration and sets an interval for automatic cache cleanup. ```js const cache = new Cache({ defaultExp: () => Date.now() + 5000, // set default expiry using a function cleanupInterval: 500, // auto-clean every 500ms }); cache.set("temp", "value", { maxAge: 100 }); // expires in 100ms setTimeout(() => { console.log(cache.has("temp")); // => false }, 1000); ``` -------------------------------- ### Custom Time Functions Source: https://github.com/bepalo/cache/blob/main/README.md Overrides the default time source to handle custom time units or logic. ```js const cache = new Cache({ now: () => Date.now() / 1000, // now will return time in seconds defaultMaxAge: 60 // treated as 60 seconds cleanupInterval: 5, // auto-clean every 5sec }); cache.set("temp", "value", { maxAge: 3 }); // expires in 3sec setTimeout(() => { console.log(cache.has("temp")); // => false }, 4000); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.