Guest User

Untitled

a guest
Oct 19th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. public class InternalService
  2. {
  3. private readonly IMemoryCache m_memoryCache;
  4.  
  5. private readonly MemoryCacheEntryOptions m_cacheEntryOptions;
  6.  
  7. public Service(IMemoryCache memoryCache)
  8. {
  9. m_memoryCache = memoryCache;
  10.  
  11. // Set cache options: keep in cache for this time, reset time in 1 hour, no matter what.
  12. m_cacheEntryOptions = new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromHours(1d));
  13. }
  14. }
  15.  
  16. public class Entity
  17. {
  18. public long Id {get; set;}
  19. }
  20.  
  21. private async IList<Entity> GetEntitiesAsync(string tenantId)
  22. {
  23. if (false == m_cache.TryGetValue(tenantId, out IList<Entity> tenantEntities) || tenantEntities.Count == 0)
  24. {
  25. // Do the expensive call.
  26. IList<Entity> tenantEntities = await ExpensiveServiceCallAsync(tenantId);
  27.  
  28. m_cache.Set(tenantId, tenantEntities , m_cacheEntryOptions);
  29. }
  30.  
  31. return tenantEntities;
  32. }
Add Comment
Please, Sign In to add comment