As I wrote in a previous post,
I’ve started to work in a
new project. My role
there is a technical team
leader of a very big team
(10 developers).
Part of my role is helping the
team to create infrastructure for their applications. As part of the application’s
infrastructure, I recommended to start using caching for better performance.
In the post I’ll demonstrate the simple cache manager I’ve built for the team.
ICacheManager Interface
Before building the cache manager I created an interface for caching.
I’ve done that in order to create abstraction between the application
and the used cache manager since in the future we may want to change
the implementation for Velocity distributed cache for example. Another
reason was for the testability of the caching subsystem. The last reason is
because we are going to use a dependency injection container for our
infrastructure. The following code is the interface I’ve created:
public interface ICacheManager {
///
/// Add a new object into the cache
///
/// The key of the object to add
/// The value of the object to add
void Add(string key, object value);
///
/// Check whether the key is contained by the cache
///
/// The key to check
/// Returns true if the key is contained by the cache
bool Contains(string key);
///
/// Returns the number of items in the cache
///
///
int Count();
///
/// Insert a new object into the cache
///
/// The key of the object to insert
/// The value of the object to insert
void Insert(string key, object value);
///
/// Get the object that its key is given
///
/// The object
/// The given key to check
/// returns the object or null if it doesn't exists
T Get(string key);
///
/// Removes the object that is referenced by the given key ///
/// The given key
void Remove(string key);
/// /// Get/Set the the given key and object /// /// The given key to the indexer /// Returns the object that the given key reference object this[string key] { get; set; } }
The CacheManager Implementation
The implementation of the cache manager is very straight forward.
I’ve used the ASP.NET caching in order to cache the data. The following code
is the simple implementation I’ve created:
public class CacheManager : ICacheManager{ #region ICacheManager Members public void Add(string key, object value) { HttpRuntime.Cache.Add(key, value, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal, null); } public bool Contains(string key) { return HttpRuntime.Cache.Get(key) != null; } public int Count() { return HttpRuntime.Cache.Count; } public void Insert(string key, object value) { HttpRuntime.Cache.Insert(key, value); } public T Get(string key) { return (T)HttpRuntime.Cache.Get(key); } public void Remove(string key) { HttpRuntime.Cache.Remove(key); } public object this[string key] { get { return HttpRuntime.Cache[key]; } set { HttpRuntime.Cache[key] = value; } } #endregion}
This isn’t going to be ourcaching manager in the future but for now
it is enough in order to start using cache in the application.
Summary
I’ve shown a very simple cache manager. I could have created other
solutions like having an in memory dictionary with cached data or other
implementation that I can think about but the ASP.NET caching is there
for using so why not use it. In the future this implementation will be
replaced with a more appropriate caching mechanismsuch as Velocity.
分享到:
2009-09-07 23:31
浏览 1971
评论