-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCacher.cs
163 lines (145 loc) · 5.78 KB
/
Cacher.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using CacheManager.Core;
using System;
namespace Sophie.Cache
{
/// <summary>A simple set of Cacher extensions</summary>
public static class CacherExtensions
{
public static ExpirationMode ConvertMode(this Expiration mode)
{
switch (mode)
{
case Expiration.Default:
return ExpirationMode.Default;
case Expiration.None:
return ExpirationMode.None;
case Expiration.Sliding:
return ExpirationMode.Sliding;
case Expiration.Absolute:
return ExpirationMode.Absolute;
default:
throw new ArgumentOutOfRangeException(nameof(mode), mode, null);
}
}
}
/// <summary></summary>
/// <typeparam name="TItemType"></typeparam>
public class Cacher<TItemType>
{
protected ICacheManager<TItemType> _cache;
/// <summary>A constructor that allows the user to select name and handle of cache</summary>
/// <param name="name"> Moniker for the cache</param>
/// <param name="handle">The handle for the cache</param>
public Cacher(string name, string handle)
{
_cache = CacheFactory.Build<TItemType>(name, settings =>
{
settings.WithSystemRuntimeCacheHandle(handle);
});
}
/// <summary>Default constructor for the proxy class</summary>
public Cacher()
{
const string name = "cache_name";
const string handle = "cache_handle";
_cache = CacheFactory.Build<TItemType>(name, settings =>
{
settings.WithSystemRuntimeCacheHandle(handle);
});
}
/// <summary>Retrieves the item from the cache given the specified key</summary>
/// <param name="key">The key in GUID form by which to retrieve the instance of type TItemType</param>
/// <returns>The instance of type TItemType</returns>
public virtual TItemType Fetch(Guid key)
{
var stringKey = key.ToString();
return Fetch(stringKey);
}
/// <summary>Retrieves the item from the cache given the specified key</summary>
/// <param name="key">
/// The key in string form by which to retrieve the instance of type TItemType
/// </param>
/// <returns>The instance of type TItemType</returns>
public virtual TItemType Fetch(string key)
{
return _cache.Get(key);
}
/// <summary>
/// Adds (or updates) an instance of type TItemType, indexed by the value of key
/// </summary>
/// <param name="key"> The key value in GUID form</param>
/// <param name="value">The instance of type TItemType to insert into the cache.</param>
public virtual void Insert(Guid key, TItemType value)
{
var stringKey = key.ToString();
Insert(stringKey, value);
}
/// <summary>
/// Adds (or updates) an instance of type TItemType, indexed by the value of key
/// </summary>
/// <param name="key"> The key value in string form</param>
/// <param name="value">The instance of type TItemType to insert into the cache.</param>
public virtual void Insert(string key, TItemType value)
{
var item = _cache.GetCacheItem(key);
if (item != null)
{
_cache.Update(key, v => value);
}
else
{
AddItem(key, value, ExpirationMode.None);
}
}
/// <summary>
/// Adds (or updates) an instance of type TItemType, indexed by the value of key
/// </summary>
/// <param name="key"> The key value in GUID form</param>
/// <param name="value">The instance of type TItemType to insert into the cache.</param>
/// <param name="mode"> Allows the user to specify lifespan of cached instance</param>
public virtual void Insert(Guid key, TItemType value, Expiration mode)
{
var stringKey = key.ToString();
Insert(stringKey, value, mode);
}
/// <summary>
/// Adds (or updates) an instance of type TItemType, indexed by the value of key
/// </summary>
/// <param name="key"> The key value in GUID form</param>
/// <param name="value">The instance of type TItemType to insert into the cache.</param>
/// <param name="mode"> Allows the user to specify lifespan of cached instance</param>
public virtual void Insert(string key, TItemType value, Expiration mode)
{
var item = _cache.Get(key);
if (item != null)
{
_cache.Update(key, v => value);
}
else
{
AddItem(key, value, mode.ConvertMode());
}
}
/// <summary>A simple helper function to add a value to the cache</summary>
/// <param name="key"> The key value in string form</param>
/// <param name="value">The instance of type TItemType to insert into the cache.</param>
/// <param name="mode"> Allows the user to specify lifespan of cached instance</param>
protected virtual void AddItem(string key, TItemType value, ExpirationMode mode)
{
_cache.Add(key, value);
}
}
/// <summary>
/// A simplified version of Cacher that allows the user to create a cache without specifying the
/// cached item type.
/// </summary>
public class Cacher : Cacher<object>
{
public Cacher(string name, string handle) : base(name, handle)
{
}
public Cacher() : base()
{
}
}
}