Server Data
SERPACK ONLY
A feature has been added to execute functions that take a long time to compute or degrade performance only on the first request and then send stored cache data.
You can see that after the first request, all responses are printed immediately.
import { GET } from '@zely-js/core';
import { setTimeout } from 'timers/promises';
async function greeting() {
await setTimeout(1000);
return 'Hello, Doe!';
}
export default [
GET(async (ctx) => {
const { data } = await $store(greeting);
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.
INFO
The code in line 11 will be:
let { data: r } = yield $store(
$greeting,
[],
'pages\\index.ts-586' // unique id
);
e.send(r);
#Dependencies
To handle dynamically changing dependencies, such as parameters (req.params), you can register dependencies with the store.
$store(fn, [someDep]);
Data is separated and stored based on the registered dependency value.
#TTL
The store's default TTL (Time To Live) is 6 minutes. To modify the TTL, enter the following code:
$cache.ttl = 1000 * 60 * 10; // cache ttl: 10m
The $cache
instance is declared globally. You don't need to import it.
#Try this feature
This feature works based on serpack.
To use this feature, enable the --serpack
flag.
zely dev --serpack