forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReadOnlyCache.cs
232 lines (206 loc) · 5.25 KB
/
ReadOnlyCache.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using NHibernate.Cache.Access;
namespace NHibernate.Cache
{
/// <summary>
/// Caches data that is never updated
/// </summary>
public partial class ReadOnlyCache : IBatchableCacheConcurrencyStrategy
{
private static readonly INHibernateLogger log = NHibernateLogger.For(typeof(ReadOnlyCache));
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;
}
public object Get(CacheKey key, long timestamp)
{
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[i] != null)));
log.Debug("Cache miss: {0}", string.Join(",", keys.Where((k, i) => results[i] == null)));
}
return results;
}
/// <summary>
/// Unsupported!
/// </summary>
public ISoftLock Lock(CacheKey key, object version)
{
log.Error("Application attempted to edit read only item: {0}", key);
throw new InvalidOperationException("ReadOnlyCache: Can't write to a readonly object " + key.EntityOrRoleName);
}
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<CacheKey>();
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;
}
public bool Put(CacheKey key, object value, long timestamp, object version, IComparer versionComparator,
bool minimalPut)
{
if (timestamp == 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>
/// Unsupported!
/// </summary>
public void Release(CacheKey key, ISoftLock @lock)
{
log.Error("Application attempted to edit read only item: {0}", key);
}
public void Clear()
{
Cache.Clear();
}
public void Remove(CacheKey key)
{
Cache.Remove(key);
}
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>
/// Unsupported!
/// </summary>
public bool AfterUpdate(CacheKey key, object value, object version, ISoftLock @lock)
{
log.Error("Application attempted to edit read only item: {0}", key);
throw new InvalidOperationException("ReadOnlyCache: Can't write to a readonly object " + key.EntityOrRoleName);
}
/// <summary>
/// Do nothing.
/// </summary>
public bool AfterInsert(CacheKey key, object value, object version)
{
// Ignore
return true;
}
/// <summary>
/// Do nothing.
/// </summary>
public void Evict(CacheKey key)
{
// NOOP
}
/// <summary>
/// Do nothing.
/// </summary>
public bool Insert(CacheKey key, object value, object currentVersion)
{
return false;
}
/// <summary>
/// Unsupported!
/// </summary>
public bool Update(CacheKey key, object value, object currentVersion, object previousVersion)
{
log.Error("Application attempted to edit read only item: {0}", key);
throw new InvalidOperationException("ReadOnlyCache: Can't write to a readonly object " + key.EntityOrRoleName);
}
}
}