项目作者: XGNPreTender

项目描述 :
Easy to use helper for your basic caching needs.
高级语言: C#
项目地址: git://github.com/XGNPreTender/PretWorks.Helpers.Cache.git
创建时间: 2018-09-19T06:28:52Z
项目社区:https://github.com/XGNPreTender/PretWorks.Helpers.Cache

开源协议:MIT License

下载


Welcome to PretWorks.Helpers.Cache

Easy to use helper for your basic caching needs.
It’s a wrapper around the IMemoryCache that will help with the basic tasks of null checking.

Basic installation

Getting the package:

  1. Install-Package PretWorks.Helpers.Cache

Setup:

Register the cache helper in startup.cs

  1. services.AddTransient(typeof(ICachehelper), typeof(InMemoryCacheHelper));

Register the cache provider in startup.cs

(for example the MemoryCache from the nuget package Microsoft.Extensions.Caching.Memory)

  1. services.AddMemoryCache();

Basic usage:

You can now inject the ICacheService into your code and use it for example:

  1. public class TestService
  2. {
  3. private readonly ICachehelper _cachehelper;
  4. private readonly IHttpClientFactory _clientFactory;
  5. public TestService(ICachehelper cachehelper, IHttpClientFactory clientFactory)
  6. {
  7. _cachehelper = cachehelper;
  8. _clientFactory = clientFactory;
  9. }
  10. private async Task<string> GetAsync()
  11. {
  12. return await _cachehelper.GetOrAddAsync(
  13. "MyCacheKey", // Cache Key
  14. async () =>
  15. {
  16. var client = _clientFactory.CreateClient("Get");
  17. var response = await client.GetAsync(new Uri("https://www.google.com"));
  18. if (response.IsSuccessStatusCode)
  19. {
  20. return await response.Content.ReadAsStringAsync();
  21. }
  22. return null;
  23. }, // Call to get data
  24. 300 // Time in seconds to cache
  25. );
  26. }
  27. }