Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

CacheModelExtension.cs 887B

1234567891011121314151617181920212223242526
  1. using Microsoft.Extensions.Caching.Memory;
  2. namespace Diligent.WebAPI.Host.Extensions
  3. {
  4. public class CacheModelExtension
  5. {
  6. private static readonly IMemoryCache _memoryCache = new MemoryCache(new MemoryCacheOptions());
  7. public static void AddCache(string cacheKey, string value, DateTime expiritaion)
  8. {
  9. var cacheExipiryOptions = new MemoryCacheEntryOptions
  10. {
  11. AbsoluteExpiration = expiritaion,
  12. Priority = CacheItemPriority.High,
  13. SlidingExpiration = TimeSpan.FromSeconds(20)
  14. };
  15. _memoryCache.Set(cacheKey, value, cacheExipiryOptions);
  16. }
  17. public static string GetCache(string cacheKey)
  18. {
  19. if (_memoryCache.TryGetValue<string>(cacheKey, out var result))
  20. return result; ;
  21. return "";
  22. }
  23. }
  24. }