-
Notifications
You must be signed in to change notification settings - Fork 249
Expand file tree
/
Copy pathMemoryCacheDefault.cs
More file actions
72 lines (62 loc) · 1.85 KB
/
MemoryCacheDefault.cs
File metadata and controls
72 lines (62 loc) · 1.85 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Caching;
namespace WebApi.OutputCache.Core.Cache
{
public class MemoryCacheDefault : IApiOutputCache
{
private static readonly MemoryCache Cache = MemoryCache.Default;
public virtual void RemoveStartsWith(string key)
{
lock (Cache)
{
Cache.Remove(key);
}
}
public virtual T Get<T>(string key) where T : class
{
var o = Cache.Get(key) as T;
return o;
}
[Obsolete("Use Get<T> instead")]
public virtual object Get(string key)
{
return Cache.Get(key);
}
public virtual void Remove(string key)
{
lock (Cache)
{
Cache.Remove(key);
}
}
public virtual bool Contains(string key)
{
return Cache.Contains(key);
}
public virtual void Add(string key, object o, DateTimeOffset expiration, string dependsOnKey = null, TimeSpan slidingExpiration = default(TimeSpan), bool slide = false)
{
var cachePolicy = new CacheItemPolicy();
if (slide)
cachePolicy.SlidingExpiration = slidingExpiration;
else
cachePolicy.AbsoluteExpiration = expiration;
if (!string.IsNullOrWhiteSpace(dependsOnKey) && !slide)
{
cachePolicy.ChangeMonitors.Add(Cache.CreateCacheEntryChangeMonitor(new[] { dependsOnKey }));
}
lock (Cache)
{
Cache.Add(key, o, cachePolicy);
}
}
public virtual IEnumerable<string> AllKeys
{
get
{
return Cache.Select(x => x.Key);
}
}
}
}