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

本文介绍了作者作为技术团队 leader,为提高应用性能推荐使用缓存,并设计了一个简单的缓存管理器接口。接口旨在创建应用程序与缓存管理器之间的抽象,方便未来可能更换缓存实现,如 Velocity 分布式缓存。同时,它增强了缓存子系统的可测试性,并考虑了依赖注入容器的使用。作者实现了基于 ASP.NET 缓存的缓存管理器,但计划在未来替换为更合适的缓存机制。
摘要由CSDN通过智能技术生成

As I wrote in a previous post,.NetLogowithASP.NET_thumb_0218084B.jpg

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.

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2009-09-07 23:31

浏览 1971

评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值