-
-
Notifications
You must be signed in to change notification settings - Fork 137
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
Improve performance using output catching #1051
Labels
Projects
Comments
I have made a 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) {
// ...
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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):
The text was updated successfully, but these errors were encountered: