MyMemeryCache.cs 810 B

123456789101112131415161718192021222324252627
  1. using Microsoft.Extensions.Caching.Memory;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. namespace wispro.sp.api.Utility
  7. {
  8. public class MyMemoryCache
  9. {
  10. static IMemoryCache _cache = new MemoryCache(new MemoryCacheOptions());
  11. public static bool TryGetValue(string strKey,out object outValue)
  12. {
  13. return _cache.TryGetValue(strKey, out outValue);
  14. }
  15. public static void SetValue(string strKey,object value)
  16. {
  17. var cacheEntryOptions = new MemoryCacheEntryOptions()
  18. // Keep in cache for this time, reset time if accessed.
  19. .SetSlidingExpiration(TimeSpan.FromHours(1));
  20. _cache.Set(strKey, value,cacheEntryOptions);
  21. }
  22. }
  23. }