-
Notifications
You must be signed in to change notification settings - Fork 1
/
SystemMemoryCache.cs
50 lines (43 loc) · 1.34 KB
/
SystemMemoryCache.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
using System.Runtime.Caching;
namespace MemoryCacheExample.Cache
{
/// <summary>
/// Responsible for caching data using System Runtime Caching
/// </summary>
public class SystemMemoryCache : ICache
{
public void Remove(string key)
{
ObjectCache cache = MemoryCache.Default;
cache.Remove(key);
}
public T Retrieve<T>(string key)
{
ObjectCache cache = MemoryCache.Default;
return cache.Contains(key) ? (T)cache[key] : default(T);
}
public void Store(string key, object data)
{
ObjectCache cache = MemoryCache.Default;
cache.Add(key, data, null);
}
public void Store(string key, object data, DateTime absoluteExpiration, TimeSpan slidingExpiration)
{
ObjectCache cache = MemoryCache.Default;
// Create a new policy for this object
var policy = new CacheItemPolicy
{
AbsoluteExpiration = absoluteExpiration,
SlidingExpiration = slidingExpiration
};
// Remove if it already exists
if (cache.Contains(key))
{
cache.Remove(key);
}
// Add to cache
cache.Add(key, data, policy);
}
}
}