对象缓存管理器JAVA实现(第一篇)---一个简单的对象缓存器实现方式

As I wrote in a previous post,.Net Logo with ASP.NET
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 {    
 /// <summary>     
/// Add a new object into the cache     
/// </summary>     
/// <param name="key">The key of the object to add</param>     
/// <param name="value">The value of the object to add</param>     
void Add(string key, object value);      
/// <summary>     
/// Check whether the key is contained by the cache     
/// </summary>     
/// <param name="key">The key to check</param>     
/// <returns>Returns true if the key is contained by the cache</returns>     
bool Contains(string key);      
/// <summary>     
/// Returns the number of items in the cache     
/// </summary>     
/// <returns></returns>     
int Count();      
/// <summary>     
/// Insert a new object into the cache      
/// </summary>     
/// <param name="key">The key of the object to insert</param>     
/// <param name="value">The value of the object to insert</param>     
void Insert(string key, object value);         
/// <summary>     
/// Get the object that its key is given     
/// </summary>     
/// <typeparam name="T">The object</typeparam>     
/// <param name="key">The given key to check</param>     
/// <returns>returns the object or null if it doesn't exists</returns>     
T Get<T>(string key);      
/// <summary>     
/// Removes the object that is referenced by the given key     /// </summary>     
/// <param name="key">The given key</param>     
void Remove(string key);      
/// <summary>     /// Get/Set the the given key and object     /// </summary>     /// <param name="key">The given key to the indexer</param>     /// <returns>Returns the object that the given key reference</returns>     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<T>(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 our caching 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 mechanism such as Velocity.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值