-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathNonstrictReadWriteCache.cs
265 lines (237 loc) · 5.8 KB
/
NonstrictReadWriteCache.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using NHibernate.Cache.Access;
namespace NHibernate.Cache
{
/// <summary>
/// Caches data that is sometimes updated without ever locking the cache.
/// If concurrent access to an item is possible, this concurrency strategy
/// makes no guarantee that the item returned from the cache is the latest
/// version available in the database. Configure your cache timeout accordingly!
/// This is an "asynchronous" concurrency strategy.
/// <seealso cref="ReadWriteCache"/> for a much stricter algorithm
/// </summary>
public partial class NonstrictReadWriteCache : IBatchableCacheConcurrencyStrategy
{
private static readonly INHibernateLogger log = NHibernateLogger.For(typeof(NonstrictReadWriteCache));
private CacheBase _cache;
/// <summary>
/// Gets the cache region name.
/// </summary>
public string RegionName
{
get { return Cache.RegionName; }
}
// 6.0 TODO: remove
#pragma warning disable 618
public ICache Cache
#pragma warning restore 618
{
get { return _cache; }
set { _cache = value?.AsCacheBase(); }
}
// 6.0 TODO: make implicit and switch to auto-property
CacheBase IBatchableCacheConcurrencyStrategy.Cache
{
get => _cache;
set => _cache = value;
}
/// <summary>
/// Get the most recent version, if available.
/// </summary>
public object Get(CacheKey key, long txTimestamp)
{
if (log.IsDebugEnabled())
{
log.Debug("Cache lookup: {0}", key);
}
var result = Cache.Get(key);
if (log.IsDebugEnabled())
{
log.Debug(result != null ? "Cache hit: {0}" : "Cache miss: {0}", key);
}
return result;
}
public object[] GetMany(CacheKey[] keys, long timestamp)
{
if (log.IsDebugEnabled())
{
log.Debug("Cache lookup: {0}", string.Join(",", keys.AsEnumerable()));
}
var results = _cache.GetMany(keys);
if (log.IsDebugEnabled())
{
log.Debug("Cache hit: {0}", string.Join(",", keys.Where((k, i) => results != null)));
log.Debug("Cache miss: {0}", string.Join(",", keys.Where((k, i) => results == null)));
}
return results;
}
/// <summary>
/// Add multiple items to the cache
/// </summary>
public bool[] PutMany(
CacheKey[] keys, object[] values, long timestamp, object[] versions, IComparer[] versionComparers,
bool[] minimalPuts)
{
var result = new bool[keys.Length];
if (timestamp == long.MinValue)
{
// MinValue means cache is disabled
return result;
}
var checkKeys = new List<object>();
var checkKeyIndexes = new List<int>();
for (var i = 0; i < minimalPuts.Length; i++)
{
if (minimalPuts[i])
{
checkKeys.Add(keys[i]);
checkKeyIndexes.Add(i);
}
}
var skipKeyIndexes = new HashSet<int>();
if (checkKeys.Any())
{
var objects = _cache.GetMany(checkKeys.ToArray());
for (var i = 0; i < objects.Length; i++)
{
if (objects[i] != null)
{
if (log.IsDebugEnabled())
{
log.Debug("item already cached: {0}", checkKeys[i]);
}
skipKeyIndexes.Add(checkKeyIndexes[i]);
}
}
}
if (skipKeyIndexes.Count == keys.Length)
{
return result;
}
var putKeys = new object[keys.Length - skipKeyIndexes.Count];
var putValues = new object[putKeys.Length];
var j = 0;
for (var i = 0; i < keys.Length; i++)
{
if (skipKeyIndexes.Contains(i))
{
continue;
}
putKeys[j] = keys[i];
putValues[j++] = values[i];
result[i] = true;
}
_cache.PutMany(putKeys, putValues);
return result;
}
/// <summary>
/// Add an item to the cache
/// </summary>
public bool Put(CacheKey key, object value, long txTimestamp, object version, IComparer versionComparator,
bool minimalPut)
{
if (txTimestamp == long.MinValue)
{
// MinValue means cache is disabled
return false;
}
if (minimalPut && Cache.Get(key) != null)
{
if (log.IsDebugEnabled())
{
log.Debug("item already cached: {0}", key);
}
return false;
}
if (log.IsDebugEnabled())
{
log.Debug("Caching: {0}", key);
}
Cache.Put(key, value);
return true;
}
/// <summary>
/// Do nothing
/// </summary>
public ISoftLock Lock(CacheKey key, object version)
{
return null;
}
public void Remove(CacheKey key)
{
if (log.IsDebugEnabled())
{
log.Debug("Removing: {0}", key);
}
Cache.Remove(key);
}
public void Clear()
{
if (log.IsDebugEnabled())
{
log.Debug("Clearing");
}
Cache.Clear();
}
public void Destroy()
{
// The cache is externally provided and may be shared. Destroying the cache is
// not the responsibility of this class.
Cache = null;
}
/// <summary>
/// Invalidate the item
/// </summary>
public void Evict(CacheKey key)
{
if (log.IsDebugEnabled())
{
log.Debug("Invalidating: {0}", key);
}
Cache.Remove(key);
}
/// <summary>
/// Invalidate the item
/// </summary>
public bool Update(CacheKey key, object value, object currentVersion, object previousVersion)
{
Evict(key);
return false;
}
/// <summary>
/// Do nothing
/// </summary>
public bool Insert(CacheKey key, object value, object currentVersion)
{
return false;
}
/// <summary>
/// Invalidate the item (again, for safety).
/// </summary>
public void Release(CacheKey key, ISoftLock @lock)
{
if (log.IsDebugEnabled())
{
log.Debug("Invalidating (again): {0}", key);
}
Cache.Remove(key);
}
/// <summary>
/// Invalidate the item (again, for safety).
/// </summary>
public bool AfterUpdate(CacheKey key, object value, object version, ISoftLock @lock)
{
Release(key, @lock);
return false;
}
/// <summary>
/// Do nothing
/// </summary>
public bool AfterInsert(CacheKey key, object value, object version)
{
return false;
}
}
}