1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
18 using System
.Collections
;
20 using Castle
.Core
.Logging
;
23 /// Simple implementation that relies on weak
24 /// references in a dictionary
26 public class WeakReferenceCacheProvider
: ICacheProvider
29 /// The logger instance
31 private ILogger logger
= NullLogger
.Instance
;
33 private IDictionary entries
= Hashtable
.Synchronized(new Hashtable());
35 #region IServiceEnabledComponent implementation
38 /// Invoked by the framework in order to give a chance to
39 /// obtain other services
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
));
55 /// Determines whether the specified key is on the cache.
57 /// <param name="key">The key.</param>
59 /// <c>true</c> if the cache has the key; otherwise, <c>false</c>.
61 public bool HasKey(String key
)
63 return Get(key
) != null;
67 /// Gets the cache item by the specified key.
69 /// <param name="key">The key.</param>
70 /// <returns></returns>
71 public object Get(String key
)
73 return InternalGet(key
);
77 /// Stores the cache item by the specified key.
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
);
87 /// Deletes the cache item by the specified key.
89 /// <param name="key">The key.</param>
90 public void Delete(String 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
;
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
);