Clover.NET coverage report - Coverage

Coverage timestamp: Friday, May 20, 2005 9:17:00 PM

File Stats: LOC: 316   Methods: 16
NCLOC: 194 Classes: 1
 
Source File Conditionals Statements Methods TOTAL
Configuration\Cache\CacheModel.cs 50.0 % 77.6 % 81.3 % 73.4 %
coverage coverage
1  
2   #region Apache Notice
3   /*****************************************************************************
4   * $Header: $
5   * $Revision: $
6   * $Date: $
7   *
8   * iBATIS.NET Data Mapper
9   * Copyright (C) 2004 - Gilles Bayon
10   *
11   *
12   * Licensed under the Apache License, Version 2.0 (the "License");
13   * you may not use this file except in compliance with the License.
14   * You may obtain a copy of the License at
15   *
16   * http://www.apache.org/licenses/LICENSE-2.0
17   *
18   * Unless required by applicable law or agreed to in writing, software
19   * distributed under the License is distributed on an "AS IS" BASIS,
20   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21   * See the License for the specific language governing permissions and
22   * limitations under the License.
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   /// <summary>
45   /// Summary description for CacheModel.
46   /// </summary>
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   /// <summary>
57   /// Constant to turn off periodic cache flushes
58   /// </summary>
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   /// <summary>
87   /// Identifier used to identify the CacheModel amongst the others.
88   /// </summary>
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   /// <summary>
103   /// Cache controller implementation name.
104   /// </summary>
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   /// <summary>
119   /// Set or get the flushInterval (in Ticks)
120   /// </summary>
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   /// <summary>
138   /// Constructor
139   /// </summary>
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   /// <summary>
148   /// Constructor
149   /// </summary>
150 170 public CacheModel()
151   {
152 170 _lastFlush = System.DateTime.Now.Ticks;
153   }
154   #endregion
155  
156   #region Methods
157   /// <summary>
158   ///
159   /// </summary>
160 170 public void Initialize()
161   {
162   // Initialize FlushInterval
163 170 _flushInterval.Initialize();
164  
165   // Initialize controller
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   // Build the CacheController
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   //------------ configure Controller---------------------
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   /// <summary>
208   /// Event listener
209   /// </summary>
210   /// <param name="mappedStatement">A MappedStatement on which we listen ExecuteEventArgs event.</param>
211 340 public void RegisterTriggerStatement(MappedStatement mappedStatement)
212   {
213 340 mappedStatement.Execute +=new ExecuteEventHandler(FlushHandler);
214   }
215  
216  
217   /// <summary>
218   /// FlushHandler which clear the cache
219   /// </summary>
220   /// <param name="sender"></param>
221   /// <param name="e"></param>
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   /// <summary>
234   /// Clears all elements from the cache.
235   /// </summary>
236 6 public void Flush()
237   {
238 6 _lastFlush = System.DateTime.Now.Ticks;
239 6 _controller.Flush();
240   }
241  
242  
243   /// <summary>
244   /// Adds an item with the specified key and value into cached data.
245   /// Gets a cached object with the specified key.
246   /// </summary>
247   /// <value>The cached object or <c>null</c></value>
248   /// <remarks>
249   /// A side effect of this method is that is may clear the cache
250   /// if it has not been cleared in the flushInterval.
251   /// </remarks>
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   /// <summary>
284   ///
285   /// </summary>
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   /// <summary>
304   /// Add a propertie
305   /// </summary>
306   /// <param name="name">The name of the propertie</param>
307   /// <param name="value">The value of the propertie</param>
308 170 public void AddPropertie(string name, string value)
309   {
310 170 _properties.Add(name, value);
311   }
312   #endregion
313  
314   }
315   }
316