3 using System.Collections.Generic;
7 using System.Threading.Tasks;
12 internal class Cache<TKey, TValue>
14 private readonly Dictionary<TKey, CacheItem<TValue>> _cache =
new Dictionary<TKey, CacheItem<TValue>>();
15 public void Store(TKey key, TValue value, TimeSpan expiresAfter)
17 _cache[key] =
new CacheItem<TValue>(value, expiresAfter);
19 public void Store(TKey key, TValue value)
21 _cache[key] =
new CacheItem<TValue>(value);
24 public TValue Get(TKey key)
26 if (!_cache.ContainsKey(key))
return default(TValue);
28 var cached = _cache[key];
29 if ( cached.ExpiresAfter != null &&
30 DateTimeOffset.Now - cached.Created >= cached.ExpiresAfter)
33 return default(TValue);
39 internal class CacheItem<T>
41 public CacheItem(T value, TimeSpan expiresAfter)
44 ExpiresAfter = expiresAfter;
46 public CacheItem(T value)
50 public T Value {
get; }
51 internal DateTimeOffset Created {
get; } = DateTimeOffset.Now;
52 internal TimeSpan? ExpiresAfter {
get; }