Skip to content
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

Add Cache System to Web Project #17

Open
ikesnowy opened this issue Jan 9, 2021 · 0 comments
Open

Add Cache System to Web Project #17

ikesnowy opened this issue Jan 9, 2021 · 0 comments

Comments

@ikesnowy
Copy link
Collaborator

ikesnowy commented Jan 9, 2021

Using midiatR, we could easily archive something like this to simplify query cache.

public class CacheBehavior<TQuery, TResponse> : IPipelineBehavior<TQuery, TResponse>
    where TQuery : ICachableQuery
    where TResponse : ICachableQueryResponse
{
    public async Task<TResponse> Handle(TQuery request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
    {
        if (request.AllowCache == false)
        {
            return await next();
        }

        var cacheResult = await _cacheService.Get<TResponse>(request.CacheKey);
        if (cacheResult is not null)
        {
            return cacheResult;
        }
        var freshResult = await next();
        await _cacheService.Add<TResponse>(request.CacheKey, request.CacheTime);
        return freshResult;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant