Open lcnvdl opened 2 years ago
I have made a Cache hook
a long time ago for my microservices gateway (Foal v2). If you want, you can close this thread (or use this code if is useful):
import { Hook, HookDecorator, HttpResponseOK } from '@foal/core';
import { MemoryCache } from '../services';
export function Cache(): HookDecorator {
return Hook(async (ctx, services) => {
if (ctx.request) {
if (ctx.request.method.toLowerCase() === 'get') {
const cache = services.get(MemoryCache);
const key = `req.${ctx.request.url}${ctx.user ? ('+usr=' + ctx.user.id) : ''}`;
ctx.state.cacheKey = key;
if (cache.containsKey(key)) {
const response = cache.getSync(key).value;
return new HttpResponseOK(response);
}
}
}
});
}
Usage:
@Get('/stats/projects')
@Cache()
async getAllProjectsStats(ctx: Context) {
// ...
}
Hi FoalTS team, I hope you are doing well!
I was looking for a server-side cache implementation in FoalTS but I couldn't find it. I bring you the following
feature request
:You can dramatically improve the performance of your web applications by taking advantage of output caching. The output cache enables you to cache the content returned by a controller action. That way, the same content does not need to be generated each and every time the same controller action is invoked.
Imagine, for example, that your application displays a list of database records in a view named Index. Normally, each and every time that a user invokes the controller action that returns the Index view, the set of database records must be retrieved from the database by executing a database query.
If, on the other hand, you take advantage of the output cache then you can avoid executing a database query every time any user invokes the same controller action. The view can be retrieved from the cache instead of being regenerated from the controller action. Caching enables you to avoid performing redundant work on the server.
Source: Improving Performance with Output Catching - MSDN Article
Usage (example, in foal.ts):