Added non-generic registration interface to IKernel and IWindsor to accommodate dynam...
[castle.git] / MonoRail / Castle.MonoRail.Framework / Services / DefaultCacheProvider.cs
blob66875aed508693b8b2e3988eac0ffebebc4f21e2
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.Web;
20 using Castle.Core.Logging;
22 /// <summary>
23 /// Simple implementation of <see cref="ICacheProvider"/>
24 /// that relies on ASP.Net Cache
25 /// </summary>
26 public class DefaultCacheProvider : ICacheProvider
28 /// <summary>
29 /// The logger instance
30 /// </summary>
31 private ILogger logger = NullLogger.Instance;
33 #region IMRServiceEnabled implementation
35 /// <summary>
36 /// Invoked by the framework in order to give a chance to
37 /// obtain other services
38 /// </summary>
39 /// <param name="provider">The service proviver</param>
40 public void Service(IMonoRailServices provider)
42 ILoggerFactory loggerFactory = (ILoggerFactory) provider.GetService(typeof(ILoggerFactory));
44 if (loggerFactory != null)
46 logger = loggerFactory.Create(typeof(DefaultCacheProvider));
50 #endregion
52 /// <summary>
53 /// Determines whether the specified key is on the cache.
54 /// </summary>
55 /// <param name="key">The key.</param>
56 /// <returns>
57 /// <c>true</c> if the cache has the key; otherwise, <c>false</c>.
58 /// </returns>
59 public bool HasKey(String key)
61 if (logger.IsDebugEnabled)
63 logger.DebugFormat("Checking for entry existence with key {0}", key);
66 return Get(key) != null;
69 /// <summary>
70 /// Gets the cache item by the specified key.
71 /// </summary>
72 /// <param name="key">The key.</param>
73 /// <returns></returns>
74 public object Get(String key)
76 if (logger.IsDebugEnabled)
78 logger.DebugFormat("Getting entry with key {0}", key);
81 return GetCurrentContext().Cache.Get(key);
84 /// <summary>
85 /// Stores the cache item by the specified key.
86 /// </summary>
87 /// <param name="key">The key.</param>
88 /// <param name="data">The data.</param>
89 public void Store(String key, object data)
91 if (logger.IsDebugEnabled)
93 logger.DebugFormat("Storing entry {0} with data {1}", key, data);
96 GetCurrentContext().Cache.Insert(key, data);
99 /// <summary>
100 /// Deletes the cache item by the specified key.
101 /// </summary>
102 /// <param name="key">The key.</param>
103 public void Delete(String key)
105 if (logger.IsDebugEnabled)
107 logger.DebugFormat("Deleting entry with key {0}", key);
110 GetCurrentContext().Cache.Remove(key);
113 /// <summary>
114 /// Gets the current context.
115 /// </summary>
116 /// <returns></returns>
117 private static HttpContext GetCurrentContext()
119 return HttpContext.Current;