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
20 using Castle
.Core
.Logging
;
23 /// Simple implementation of <see cref="ICacheProvider"/>
24 /// that relies on ASP.Net Cache
26 public class DefaultCacheProvider
: ICacheProvider
29 /// The logger instance
31 private ILogger logger
= NullLogger
.Instance
;
33 #region IMRServiceEnabled implementation
36 /// Invoked by the framework in order to give a chance to
37 /// obtain other services
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
));
53 /// Determines whether the specified key is on the cache.
55 /// <param name="key">The key.</param>
57 /// <c>true</c> if the cache has the key; otherwise, <c>false</c>.
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;
70 /// Gets the cache item by the specified key.
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
);
85 /// Stores the cache item by the specified key.
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
);
100 /// Deletes the cache item by the specified key.
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
);
114 /// Gets the current context.
116 /// <returns></returns>
117 private static HttpContext
GetCurrentContext()
119 return HttpContext
.Current
;