The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MemoryCacheHelper.cs 1.0KB

123456789101112131415161718192021222324252627282930313233
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Microsoft.Extensions.Caching.Memory;
  5. namespace Teknik.Utilities
  6. {
  7. public static class MemoryCacheHelper
  8. {
  9. public static bool GetCacheValue<T>(this IMemoryCache cache, string key, out T value)
  10. {
  11. return cache.TryGetValue(key, out value);
  12. }
  13. public static bool GetCacheValue(this IMemoryCache cache, string key, out object value)
  14. {
  15. return cache.TryGetValue(key, out value);
  16. }
  17. public static void AddToCache(this IMemoryCache cache, string key, object value, TimeSpan expiration)
  18. {
  19. var cacheOptions = new MemoryCacheEntryOptions()
  20. .SetAbsoluteExpiration(expiration);
  21. cache.AddToCache(key, value, cacheOptions);
  22. }
  23. public static void AddToCache(this IMemoryCache cache, string key, object value, MemoryCacheEntryOptions options)
  24. {
  25. cache.Set(key, value, options);
  26. }
  27. }
  28. }