-
Notifications
You must be signed in to change notification settings - Fork 2
/
MemoryProvider.cs
59 lines (50 loc) · 1.56 KB
/
MemoryProvider.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
51
52
53
54
55
56
57
58
59
using System;
using System.Runtime.Caching;
namespace CacheCSharp
{
public class MemoryProvider : CacheProviderBase<MemoryCache>
{
protected override MemoryCache InitCache()
{
return MemoryCache.Default;
}
public override T Get<T>(string key)
{
try
{
if (!Exists(key))
{
return default(T);
}
return (T)Cache[KeyPrefix + key];
}
catch
{
return default(T);
}
}
public override void Set<T>(string key, T value, int duration)
{
var policy = new CacheItemPolicy {AbsoluteExpiration = DateTime.Now.AddMinutes(duration)};
Cache.Set(KeyPrefix + key, value, policy);
}
public override void SetSliding<T>(string key, T value, int duration)
{
var policy = new CacheItemPolicy { SlidingExpiration = new TimeSpan(0, duration, 0) };
Cache.Set(KeyPrefix + key, value, policy);
}
public override void Set<T>(string key, T value, DateTimeOffset expiration)
{
var policy = new CacheItemPolicy { AbsoluteExpiration = expiration.DateTime };
Cache.Set(KeyPrefix + key, value, policy);
}
public override bool Exists(string key)
{
return Cache[KeyPrefix + key] != null;
}
public override void Remove(string key)
{
Cache.Remove(KeyPrefix + key);
}
}
}