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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tango.FSE.BL
{
/// <summary>
/// Represents an in-memory cache dictionary of type T.
/// </summary>
/// <typeparam name="TValue"></typeparam>
/// <seealso cref="Tango.FSE.BL.IMemoryCacheDictionary" />
public class MemoryCacheDictionary<TKey, TValue> : IMemoryCacheDictionary where TValue : class
{
private ConcurrentDictionary<TKey, TValue> _dictionary;
/// <summary>
/// Gets the cache name.
/// </summary>
public String Name { get; private set; }
/// <summary>
/// Gets the count of items.
/// </summary>
public int Count
{
get { return _dictionary.Count; }
}
/// <summary>
/// Initializes a new instance of the <see cref="MemoryCacheDictionary{T}"/> class.
/// </summary>
/// <param name="name">The name of the cache.</param>
public MemoryCacheDictionary(String name)
{
Name = name;
_dictionary = new ConcurrentDictionary<TKey, TValue>();
}
/// <summary>
/// Determines whether this cache contains an element with the specified key.
/// </summary>
/// <param name="key">The key.</param>
public bool ContainsKey(TKey key)
{
return _dictionary.ContainsKey(key);
}
/// <summary>
/// Gets a cached item by the specified key.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="throwIfNotFound">if set to <c>true</c> throws exception when item not found otherwise returns null.</param>
/// <exception cref="KeyNotFoundException"></exception>
public TValue Get(TKey key, bool throwIfNotFound = true)
{
TValue value = null;
if (_dictionary.TryGetValue(key, out value))
{
return value;
}
else
{
if (throwIfNotFound)
{
throw new KeyNotFoundException($"Could not locate key '{key}' on cache '{Name}'.");
}
else
{
return null;
}
}
}
/// <summary>
/// Puts a new cached item or overrides an existing one.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="value">The value.</param>
public void Put(TKey key, TValue value)
{
_dictionary[key] = value;
}
/// <summary>
/// Get all cached items as a list.
/// </summary>
/// <returns></returns>
public List<TValue> ToList(bool throwOnEmpty = false)
{
var list = _dictionary.Select(x => x.Value).ToList();
if (throwOnEmpty && list.Count == 0)
{
throw new KeyNotFoundException("The memory cache does not contain any items.");
}
return list;
}
/// <summary>
/// Clears this all items from cache.
/// </summary>
public void Clear()
{
_dictionary.Clear();
}
}
/// <summary>
/// Represents an in-memory cache dictionary of type T.
/// </summary>
/// <typeparam name="TValue"></typeparam>
/// <seealso cref="Tango.FSE.BL.IMemoryCacheDictionary" />
public class MemoryCacheDoubleKeyDictionary<TKey1, TKey2, TValue> : IMemoryCacheDictionary where TValue : class
{
private ConcurrentDictionary<String, TValue> _dictionary;
/// <summary>
/// Gets the cache name.
/// </summary>
public String Name { get; private set; }
/// <summary>
/// Gets the count of items.
/// </summary>
public int Count
{
get { return _dictionary.Count; }
}
/// <summary>
/// Initializes a new instance of the <see cref="MemoryCacheDictionary{T}"/> class.
/// </summary>
/// <param name="name">The name of the cache.</param>
public MemoryCacheDoubleKeyDictionary(String name)
{
Name = name;
_dictionary = new ConcurrentDictionary<String, TValue>();
}
/// <summary>
/// Determines whether this cache contains an element with the specified keys.
/// </summary>
/// <param name="key1">The first key.</param>
/// <param name="key2">The second key.</param>
public bool ContainsKey(TKey1 key1, TKey2 key2)
{
return _dictionary.ContainsKey(key1.ToString() + key2.ToString());
}
/// <summary>
/// Gets a cached item by the specified keys.
/// </summary>
/// <param name="key1">The first key.</param>
/// <param name="key2">The second key.</param>
/// <param name="throwIfNotFound">if set to <c>true</c> throws exception when item not found otherwise returns null.</param>
/// <exception cref="KeyNotFoundException"></exception>
public TValue Get(TKey1 key1, TKey2 key2, bool throwIfNotFound = true)
{
TValue value = null;
if (_dictionary.TryGetValue(key1.ToString() + key2.ToString(), out value))
{
return value;
}
else
{
if (throwIfNotFound)
{
throw new KeyNotFoundException($"Could not locate key '{key1 + " " + key2}' on cache '{Name}'.");
}
else
{
return null;
}
}
}
/// <summary>
/// Puts a new cached item or overrides an existing one.
/// </summary>
/// <param name="key1">The first key.</param>
/// <param name="key2">The second key.</param>
/// <param name="value">The value.</param>
public void Put(TKey1 key1, TKey2 key2, TValue value)
{
_dictionary[key1.ToString() + key2.ToString()] = value;
}
/// <summary>
/// Get all cached items as a list.
/// </summary>
/// <returns></returns>
public List<TValue> ToList(bool throwOnEmpty = false)
{
var list = _dictionary.Select(x => x.Value).ToList();
if (throwOnEmpty && list.Count == 0)
{
throw new KeyNotFoundException("The memory cache does not contain any items.");
}
return list;
}
/// <summary>
/// Clears this all items from cache.
/// </summary>
public void Clear()
{
_dictionary.Clear();
}
}
}
|