-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Async Cache #33
Comments
What's the use cases of AsyncCache plugin? |
I am using octokit to get stuff from github and want cache all get requests and persist the changes during server restarts (Astro Framework). So the filesystem cache should work but I don't want to use fs modules without async because that would block the page render. Axios supports the same via interceptors so i thought there would be some support in xior also. |
To persist cache data, update
import { lru, LRU } from 'tiny-lru';
import { Xior as xior, delay, Xior } from 'xior';
import xiorCachePlugin from 'xior/plugins/cache';
import fs from 'fs/promises';
export function persistCachePlugin(
xiorInstance: Xior,
cache: LRU<any>,
filePath: string,
debounceTime = 500
) {
xiorInstance.interceptors.response.use((response) => {
if (!response.fromCache && response.cacheKey) {
debouncedSyncToFs();
}
return response;
});
async function initFromFs() {
try {
const data = JSON.parse(await fs.readFile(filePath, 'utf-8'));
data.forEach(([key, value]: [string, any]) => {
if (value) {
cache.set(key, Promise.resolve(value));
}
});
} catch (e) {
console.error('initFromFs error:', e);
}
}
async function syncToFs() {
const keys = cache.keys();
const data = await Promise.all(keys.map(async (key) => [key, await cache.get(key)]));
await fs.writeFile(filePath, JSON.stringify(data, null, 2));
}
const debouncedSyncToFs = debounce(syncToFs, debounceTime);
function debounce(func: () => Promise<void>, wait: number) {
let timeout: NodeJS.Timeout | null = null;
return () => {
if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(() => {
func();
}, wait);
};
}
return { initFromFs };
}
const http = xior.create();
const cache = lru(100, 1000 * 60 * 5);
http.plugins.use(
xiorCachePlugin({
enableCache: (config) => config?.method === 'GET' || config?.isGet === true,
defaultCache: cache,
})
);
// You need to run `initFromFs` to initialize cache data from the file before sending any requests.
const { initFromFs } = persistCachePlugin(http, cache, './cache.json', 500); You can also check the persistent cache test file: https://github.com/suhaotian/xior/blob/main/tests/src/tests/plugins/persist-cache.test.ts |
Thanks 💯 |
Can someone help me on using a async defaultCache for cachePlugin. I want to do filesystem IO for the cache so async is required.
The text was updated successfully, but these errors were encountered: