1
|
|
|
2
|
|
#region Apache Notice
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
|
18
|
|
|
19
|
|
|
20
|
|
|
21
|
|
|
22
|
|
|
23
|
|
|
24
|
|
|
25
|
|
#endregion
|
26
|
|
|
27
|
|
#region Imports
|
28
|
|
using System;
|
29
|
|
using System.Collections;
|
30
|
|
using System.Collections.Specialized;
|
31
|
|
using System.Reflection;
|
32
|
|
using System.Xml.Serialization;
|
33
|
|
|
34
|
|
using log4net;
|
35
|
|
|
36
|
|
using IBatisNet.Common.Exceptions;
|
37
|
|
using IBatisNet.DataMapper.Exceptions;
|
38
|
|
using IBatisNet.DataMapper.MappedStatements;
|
39
|
|
#endregion
|
40
|
|
|
41
|
|
namespace IBatisNet.DataMapper.Configuration.Cache
|
42
|
|
{
|
43
|
|
|
44
|
|
|
45
|
|
|
46
|
|
|
47
|
|
[Serializable]
|
48
|
|
[XmlRoot("cacheModel")]
|
49
|
|
public class CacheModel
|
50
|
|
{
|
51
|
|
#region Fields
|
52
|
|
|
53
|
|
[NonSerialized]
|
54
|
|
private static readonly ILog _logger = LogManager.GetLogger( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType );
|
55
|
|
|
56
|
|
|
57
|
|
|
58
|
|
|
59
|
|
[NonSerialized]
|
60
|
|
public const long NO_FLUSH_INTERVAL = -99999;
|
61
|
|
|
62
|
|
[NonSerialized]
|
63
|
|
private object _statLock = new Object();
|
64
|
|
[NonSerialized]
|
65
|
|
private int _requests = 0;
|
66
|
|
[NonSerialized]
|
67
|
|
private int _hits = 0;
|
68
|
|
[NonSerialized]
|
69
|
|
private string _id = string.Empty;
|
70
|
|
[NonSerialized]
|
71
|
|
private ICacheController _controller = null;
|
72
|
|
[NonSerialized]
|
73
|
|
private FlushInterval _flushInterval = null;
|
74
|
|
[NonSerialized]
|
75
|
|
private long _lastFlush = 0;
|
76
|
|
[NonSerialized]
|
77
|
|
private HybridDictionary _properties = new HybridDictionary();
|
78
|
|
[NonSerialized]
|
79
|
|
private string _implementation = string.Empty;
|
80
|
|
[NonSerialized]
|
81
|
|
static private Hashtable _cacheControllerAliases = new Hashtable();
|
82
|
|
|
83
|
|
#endregion
|
84
|
|
|
85
|
|
#region Properties
|
86
|
|
|
87
|
|
|
88
|
|
|
89
|
|
[XmlAttribute("id")]
|
90
|
|
public string Id
|
91
|
|
{
|
92
|
510
|
get { return _id; }
|
93
|
340
|
set
|
94
|
|
{
|
95
|
340
|
if ((value == null) || (value.Length < 1))
|
96
|
0
|
throw new ArgumentNullException("The id attribute is mandatory in a cacheModel tag.");
|
97
|
|
|
98
|
340
|
_id = value;
|
99
|
|
}
|
100
|
|
}
|
101
|
|
|
102
|
|
|
103
|
|
|
104
|
|
|
105
|
|
[XmlAttribute("implementation")]
|
106
|
|
public string Implementation
|
107
|
|
{
|
108
|
0
|
get { return _implementation; }
|
109
|
170
|
set
|
110
|
|
{
|
111
|
170
|
if ((value == null) || (value.Length < 1))
|
112
|
0
|
throw new ArgumentNullException("The implementation attribute is mandatory in a cacheModel tag.");
|
113
|
|
|
114
|
170
|
_implementation = value;
|
115
|
|
}
|
116
|
|
}
|
117
|
|
|
118
|
|
|
119
|
|
|
120
|
|
|
121
|
|
[XmlElement("flushInterval",typeof(FlushInterval))]
|
122
|
|
public FlushInterval FlushInterval
|
123
|
|
{
|
124
|
0
|
get
|
125
|
|
{
|
126
|
|
return _flushInterval;
|
127
|
|
}
|
128
|
170
|
set
|
129
|
|
{
|
130
|
170
|
_flushInterval = value;
|
131
|
|
}
|
132
|
|
}
|
133
|
|
#endregion
|
134
|
|
|
135
|
|
#region Constructor (s) / Destructor
|
136
|
|
|
137
|
|
|
138
|
|
|
139
|
|
|
140
|
1
|
static CacheModel()
|
141
|
|
{
|
142
|
1
|
_cacheControllerAliases.Add("MEMORY","IBatisNet.DataMapper.Configuration.Cache.Memory.MemoryCacheControler");
|
143
|
1
|
_cacheControllerAliases.Add("LRU","IBatisNet.DataMapper.Configuration.Cache.Lru.LruCacheController");
|
144
|
1
|
_cacheControllerAliases.Add("FIFO","IBatisNet.DataMapper.Configuration.Cache.Fifo.FifoCacheController");
|
145
|
|
}
|
146
|
|
|
147
|
|
|
148
|
|
|
149
|
|
|
150
|
170
|
public CacheModel()
|
151
|
|
{
|
152
|
170
|
_lastFlush = System.DateTime.Now.Ticks;
|
153
|
|
}
|
154
|
|
#endregion
|
155
|
|
|
156
|
|
#region Methods
|
157
|
|
|
158
|
|
|
159
|
|
|
160
|
170
|
public void Initialize()
|
161
|
|
{
|
162
|
|
|
163
|
170
|
_flushInterval.Initialize();
|
164
|
|
|
165
|
|
|
166
|
170
|
Assembly assembly = null;
|
167
|
170
|
Type type = null;
|
168
|
|
|
169
|
170
|
_implementation = _cacheControllerAliases[_implementation.ToUpper()] as string;
|
170
|
|
|
171
|
170
|
try
|
172
|
|
{
|
173
|
170
|
if (_implementation == null)
|
174
|
|
{
|
175
|
0
|
throw new DataMapperException ("Error instantiating cache controller for cache named '"+_id+"'. Cause: The class for name '"+_implementation+"' could not be found.");
|
176
|
|
}
|
177
|
|
|
178
|
170
|
assembly = Assembly.GetCallingAssembly();
|
179
|
|
|
180
|
|
|
181
|
170
|
type = assembly.GetType(_implementation, true);
|
182
|
170
|
object[] arguments = new object[0];
|
183
|
|
|
184
|
170
|
_controller = (ICacheController)Activator.CreateInstance(type, arguments);
|
185
|
|
}
|
186
|
|
catch (Exception e)
|
187
|
|
{
|
188
|
0
|
throw new ConfigurationException("Error instantiating cache controller for cache named '"+_id+". Cause: " + e.Message, e);
|
189
|
|
}
|
190
|
|
|
191
|
|
|
192
|
170
|
try
|
193
|
|
{
|
194
|
170
|
_controller.Configure(_properties);
|
195
|
|
}
|
196
|
|
catch (Exception e)
|
197
|
|
{
|
198
|
0
|
throw new ConfigurationException ("Error configuring controller named '"+_id+"'. Cause: " + e.Message, e);
|
199
|
|
}
|
200
|
|
finally
|
201
|
|
{
|
202
|
|
|
203
|
|
}
|
204
|
|
}
|
205
|
|
|
206
|
|
|
207
|
|
|
208
|
|
|
209
|
|
|
210
|
|
|
211
|
340
|
public void RegisterTriggerStatement(MappedStatement mappedStatement)
|
212
|
|
{
|
213
|
340
|
mappedStatement.Execute +=new ExecuteEventHandler(FlushHandler);
|
214
|
|
}
|
215
|
|
|
216
|
|
|
217
|
|
|
218
|
|
|
219
|
|
|
220
|
|
|
221
|
|
|
222
|
5
|
private void FlushHandler(object sender, ExecuteEventArgs e)
|
223
|
|
{
|
224
|
5
|
if (_logger.IsDebugEnabled)
|
225
|
|
{
|
226
|
5
|
_logger.Debug("Flush cacheModel named "+_id+" for statement '"+e.StatementName+"'");
|
227
|
|
}
|
228
|
|
|
229
|
5
|
Flush();
|
230
|
|
}
|
231
|
|
|
232
|
|
|
233
|
|
|
234
|
|
|
235
|
|
|
236
|
6
|
public void Flush()
|
237
|
|
{
|
238
|
6
|
_lastFlush = System.DateTime.Now.Ticks;
|
239
|
6
|
_controller.Flush();
|
240
|
|
}
|
241
|
|
|
242
|
|
|
243
|
|
|
244
|
|
|
245
|
|
|
246
|
|
|
247
|
|
|
248
|
|
|
249
|
|
|
250
|
|
|
251
|
|
|
252
|
|
public object this [object key]
|
253
|
|
{
|
254
|
13
|
get
|
255
|
|
{
|
256
|
13
|
lock(this)
|
257
|
|
{
|
258
|
13
|
if (_lastFlush != NO_FLUSH_INTERVAL
|
259
|
|
&& (System.DateTime.Now.Ticks - _lastFlush > _flushInterval.Interval))
|
260
|
|
{
|
261
|
0
|
Flush();
|
262
|
|
}
|
263
|
|
}
|
264
|
|
|
265
|
13
|
object value = _controller[key];
|
266
|
|
|
267
|
13
|
lock(_statLock)
|
268
|
|
{
|
269
|
13
|
_requests++;
|
270
|
13
|
if (value != null)
|
271
|
|
{
|
272
|
5
|
_hits++;
|
273
|
|
}
|
274
|
|
}
|
275
|
13
|
return value;
|
276
|
|
}
|
277
|
8
|
set
|
278
|
|
{
|
279
|
8
|
_controller[key] = value;
|
280
|
|
}
|
281
|
|
}
|
282
|
|
|
283
|
|
|
284
|
|
|
285
|
|
|
286
|
0
|
public double HitRatio
|
287
|
|
{
|
288
|
|
get
|
289
|
|
{
|
290
|
|
if (_requests!=0)
|
291
|
|
{
|
292
|
|
return
|
293
|
|
(double)_hits/(double)_requests;
|
294
|
|
}
|
295
|
|
else
|
296
|
|
{
|
297
|
|
return 0;
|
298
|
|
}
|
299
|
|
}
|
300
|
|
}
|
301
|
|
|
302
|
|
|
303
|
|
|
304
|
|
|
305
|
|
|
306
|
|
|
307
|
|
|
308
|
170
|
public void AddPropertie(string name, string value)
|
309
|
|
{
|
310
|
170
|
_properties.Add(name, value);
|
311
|
|
}
|
312
|
|
#endregion
|
313
|
|
|
314
|
|
}
|
315
|
|
}
|
316
|
|
|