-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cache.cs
29 lines (24 loc) · 792 Bytes
/
Cache.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using System.Collections.Concurrent;
namespace ShoppingCart
{
public interface ICache
{
void Add(string key, object value, TimeSpan ttl);
object? Get(string key);
}
public class Cache : ICache
{
private static readonly IDictionary<string, (DateTimeOffset, object)> cache = new ConcurrentDictionary<string, (DateTimeOffset, object)>();
public void Add(string key, object value, TimeSpan ttl)
{
cache[key] = (DateTimeOffset.UtcNow.Add(ttl), value);
}
public object? Get(string productsResource)
{
if (cache.TryGetValue(productsResource, out var value))
return value;
cache.Remove(productsResource);
return null;
}
}
}