2024-02-09 04:00:45 +08:00
|
|
|
import { ttlCache } from "$lib/memory_cache.ts";
|
|
|
|
import { sleep } from "$lib/async.ts";
|
2024-03-16 22:29:24 +08:00
|
|
|
import { assertEquals } from "$std/testing/asserts.ts";
|
2024-01-15 23:43:12 +08:00
|
|
|
|
|
|
|
Deno.test("Memory cache", async () => {
|
|
|
|
let calls = 0;
|
|
|
|
async function expensiveFunction(key: string) {
|
|
|
|
calls++;
|
|
|
|
await sleep(1);
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
assertEquals("key", await ttlCache("key", expensiveFunction, 0.01));
|
|
|
|
assertEquals(1, calls);
|
|
|
|
assertEquals("key", await ttlCache("key", expensiveFunction, 0.01));
|
|
|
|
assertEquals(1, calls);
|
|
|
|
await sleep(10);
|
|
|
|
});
|