- Fixed MR-84
[castle.git] / MonoRail / Castle.MonoRail.Framework / Services / WeakReferenceCacheProvider.cs
blob2f56773f7f1a28af152a6a280bffb14de3c74e44
1 // Copyright 2004-2007 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.MonoRail.Framework.Services
17 using System;
18 using System.Collections;
20 using Castle.Core.Logging;
22 /// <summary>
23 /// Simple implementation that relies on weak
24 /// references in a dictionary
25 /// </summary>
26 public class WeakReferenceCacheProvider : ICacheProvider
28 /// <summary>
29 /// The logger instance
30 /// </summary>
31 private ILogger logger = NullLogger.Instance;
33 private IDictionary entries = Hashtable.Synchronized(new Hashtable());
35 #region IServiceEnabledComponent implementation
37 /// <summary>
38 /// Invoked by the framework in order to give a chance to
39 /// obtain other services
40 /// </summary>
41 /// <param name="provider">The service proviver</param>
42 public void Service(IServiceProvider provider)
44 ILoggerFactory loggerFactory = (ILoggerFactory) provider.GetService(typeof(ILoggerFactory));
46 if (loggerFactory != null)
48 logger = loggerFactory.Create(typeof(WeakReferenceCacheProvider));
52 #endregion
54 /// <summary>
55 /// Determines whether the specified key is on the cache.
56 /// </summary>
57 /// <param name="key">The key.</param>
58 /// <returns>
59 /// <c>true</c> if the cache has the key; otherwise, <c>false</c>.
60 /// </returns>
61 public bool HasKey(String key)
63 return Get(key) != null;
66 /// <summary>
67 /// Gets the cache item by the specified key.
68 /// </summary>
69 /// <param name="key">The key.</param>
70 /// <returns></returns>
71 public object Get(String key)
73 return InternalGet(key);
76 /// <summary>
77 /// Stores the cache item by the specified key.
78 /// </summary>
79 /// <param name="key">The key.</param>
80 /// <param name="data">The data.</param>
81 public void Store(String key, object data)
83 InternalStore(key, data);
86 /// <summary>
87 /// Deletes the cache item by the specified key.
88 /// </summary>
89 /// <param name="key">The key.</param>
90 public void Delete(String key)
92 InternalDelete(key);
95 private void InternalStore(object key, object data)
97 if (key == null) throw new ArgumentNullException("key");
98 if (data == null) throw new ArgumentNullException("data");
100 if (logger.IsDebugEnabled)
102 logger.DebugFormat("Storing entry {0} with value {1}", key, data);
105 entries[key] = new WeakReference(data);
108 private object InternalGet(object key)
110 if (key == null) throw new ArgumentNullException("key");
112 if (logger.IsDebugEnabled)
114 logger.DebugFormat("Getting entry {0}", key);
117 WeakReference reference = (WeakReference)entries[key];
119 if (reference == null) return null;
121 if (reference.IsAlive)
123 return reference.Target;
126 InternalDelete(key);
128 return null;
131 private void InternalDelete(object key)
133 if (key == null) throw new ArgumentNullException("key");
135 if (logger.IsDebugEnabled)
137 logger.DebugFormat("Deleting entry {0}", key);
140 entries.Remove(key);