Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / Facilities / Cache / Castle.Facilities.Cache.Tests / FifoCacheManager.cs
blob25812f6c34c3aefb4a4752554eb6ea518960ab7f
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.Facilities.Cache.Tests
17 using System.Collections;
18 using System.Collections.Specialized;
19 using Castle.Facilities.Cache.Manager;
21 /// <summary>
22 /// Description résumée de FifoCacheManager.
23 /// </summary>
24 public class FifoCacheManager : ICacheManager
26 #region Fields
27 private int _cacheSize = 0;
28 private IDictionary _cache = null;
29 private IList _keyList = null;
30 private ICacheKeyGenerator _cacheKeyGenerator = null;
31 #endregion
33 public IList KeyList
35 get { return _keyList; }
38 #region Constructor (s) / Destructor
39 /// <summary>
40 ///
41 /// </summary>
42 public FifoCacheManager(int cacheSize)
44 _cacheSize = cacheSize;
45 _cache = new HybridDictionary();
46 _keyList = new ArrayList();
48 #endregion
50 #region ICacheManager Members
52 /// <summary>
53 /// A generator of keys for a cache entry.
54 /// </summary>
55 public ICacheKeyGenerator CacheKeyGenerator
57 get { return _cacheKeyGenerator; }
58 set { _cacheKeyGenerator = value; }
61 /// <summary>
62 /// Clears all elements from the cache.
63 /// </summary>
64 public void Clear()
66 lock(this)
68 _cache.Clear();
69 _keyList.Clear();
74 /// <summary>
75 /// Adds an item with the specified key and value into cached data.
76 /// Gets a cached object with the specified key.
77 /// </summary>
78 /// <value>The cached object or <c>null</c></value>
79 public object this [object key]
81 get
83 lock (this)
85 return _cache[key];
88 set
90 lock (this)
92 _cache.Add(key, value);
93 _keyList.Add(key);
94 if (_keyList.Count > _cacheSize)
96 object oldestKey = _keyList[0];
97 _keyList.Remove(0);
98 _cache.Remove(oldestKey);
104 #endregion