Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class InternalService
- {
- private readonly IMemoryCache m_memoryCache;
- private readonly MemoryCacheEntryOptions m_cacheEntryOptions;
- public Service(IMemoryCache memoryCache)
- {
- m_memoryCache = memoryCache;
- // Set cache options: keep in cache for this time, reset time in 1 hour, no matter what.
- m_cacheEntryOptions = new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromHours(1d));
- }
- }
- public class Entity
- {
- public long Id {get; set;}
- }
- private async IList<Entity> GetEntitiesAsync(string tenantId)
- {
- if (false == m_cache.TryGetValue(tenantId, out IList<Entity> tenantEntities) || tenantEntities.Count == 0)
- {
- // Do the expensive call.
- IList<Entity> tenantEntities = await ExpensiveServiceCallAsync(tenantId);
- m_cache.Set(tenantId, tenantEntities , m_cacheEntryOptions);
- }
- return tenantEntities;
- }
Add Comment
Please, Sign In to add comment