Data Caching
$store
is a function that stores the return value of another function.
The first argument accepts the function you want to store, and the second argument accepts a dependencies
value used to specify a unique ID.
The compiler, serpack, automatically assigns a unique ID to each $store
function, allowing it to work seamlessly.
#$store
import { GET } from "@zely-js/core";
import { setTimeout } from "timers/promises";
async function slowGreeting() {
await setTimeout(1000);
return "Hello, Doe!";
}
export default [
GET(async (ctx) => {
const { data } = await $store(slowGreeting);
ctx.send(data);
}),
];
TIP
You don't need to import any other modules. Just use the $store
function
directly.
After the first request, the $greeting
function will no longer be executed (during ttl).
#Dependencies
To handle dynamically changing dependencies, you can register dependencies with the store.
$store(fn, [someDep]);
Data is separated and stored based on the registered dependency value.
WARNING
Each item in dependencies must be of type string or Context.
#Context as Dependencies
If the context is provided as dependencies
, $store
will automatically analyze the params
and generate a unique ID.
export default [
GET(async (ctx) => {
await $store(fn, ctx);
// ...
}),
];