`
xupo
  • 浏览: 212250 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

对象缓存管理器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.

分享到:
评论

相关推荐

    架构师培训教程 大数据高并发服务器实战 第1篇-Linux 02-文件系统结构及常用shell命令 共62页.pptx

    第1篇-Linux 01-Linux简介及安装 共11页 第1篇-Linux 02-文件系统结构及常用shell命令 共62页 第2.1篇-LNMP部分-源码方式安装 共27页 第2.2篇-LNMP部分-yum方式安装 共21页 第2.4篇-LNMP部分-Nginx部分-基本配置 共...

    架构师培训教程 大数据高并发服务器实战 第2.1篇-LNMP部分-源码方式安装 共27页.pptx

    第1篇-Linux 01-Linux简介及安装 共11页 第1篇-Linux 02-文件系统结构及常用shell命令 共62页 第2.1篇-LNMP部分-源码方式安装 共27页 第2.2篇-LNMP部分-yum方式安装 共21页 第2.4篇-LNMP部分-Nginx部分-基本配置 共...

    第六讲-缓存接入之多级缓存实现及分布式部署落地实践.pdf

    缓存接入之多级缓存实现及分布式部署落地实践 今日课程主题: 1、项目分离部署 2、分布式部署 3、多级缓存(堆内缓存,分布式缓存,nginx+lua 缓存) 1 单体部署 部署形式: 项目+MySQL 放在同一个服务器(高性能的...

    架构师培训教程 大数据高并发服务器实战 第2.4篇-LNMP部分-Nginx部分-基本配置 共30页.pptx

    第1篇-Linux 01-Linux简介及安装 共11页 第1篇-Linux 02-文件系统结构及常用shell命令 共62页 第2.1篇-LNMP部分-源码方式安装 共27页 第2.2篇-LNMP部分-yum方式安装 共21页 第2.4篇-LNMP部分-Nginx部分-基本配置 共...

    Java-Edge#Java-Interview-Tutorial#突破Java面试(19) - 分布式缓存的第一个问题1

    从缓存里获取数据1返回给用户2 ,耗费10ms假设10分钟之内有1000个用户都查询了同一个数据10分钟之内,那1000个用户,每个人查询这个数据都感觉很慢,

    架构师培训教程 大数据高并发服务器实战 第2.6篇-Nginx部分-反向代理和负载均衡-负载均衡配置 共9页.pptx

    第1篇-Linux 01-Linux简介及安装 共11页 第1篇-Linux 02-文件系统结构及常用shell命令 共62页 第2.1篇-LNMP部分-源码方式安装 共27页 第2.2篇-LNMP部分-yum方式安装 共21页 第2.4篇-LNMP部分-Nginx部分-基本配置 共...

    架构师培训教程 大数据高并发服务器实战 第2.9篇-Nginx部分-与Tomcat整合配置 共14页.pptx

    第1篇-Linux 01-Linux简介及安装 共11页 第1篇-Linux 02-文件系统结构及常用shell命令 共62页 第2.1篇-LNMP部分-源码方式安装 共27页 第2.2篇-LNMP部分-yum方式安装 共21页 第2.4篇-LNMP部分-Nginx部分-基本配置 共...

    J2Cache 基于内存和 Redis 的两级 Java 缓存框架

    第一级缓存使用内存(同时支持 Ehcache 2.x、Ehcache 3.x 和 Caffeine),第二级缓存使用 Redis(推荐)/Memcached 。 由于大量的缓存读取会导致 L2 的网络成为整个系统的瓶颈,因此 L1 的目标是降低对 L2 的读取次数。 ...

    精通 Hibernate:Java 对象持久化技术详解(第2版).part2

    第1章 Java应用分层架构及软件模型  1.1 应用程序的分层体系结构  1.1.1 区分物理层和逻辑层  1.1.2 软件层的特征  1.1.3 软件分层的优点  1.1.4 软件分层的缺点  1.1.5 Java应用的持久化层  1.2 软件的模型 ...

    java开源包4

    SimpleCache 是一个简单易用的java缓存工具,用来简化缓存代码的编写,让你摆脱单调乏味的重复工作!1. 完全透明的缓存支持,对业务代码零侵入 2. 支持使用Redis和Memcached作为后端缓存。3. 支持缓存数据分区规则的...

    java开源包1

    SimpleCache 是一个简单易用的java缓存工具,用来简化缓存代码的编写,让你摆脱单调乏味的重复工作!1. 完全透明的缓存支持,对业务代码零侵入 2. 支持使用Redis和Memcached作为后端缓存。3. 支持缓存数据分区规则的...

    JAVA上百实例源码以及开源项目

     Tcp服务端与客户端的JAVA实例源代码,一个简单的Java TCP服务器端程序,别外还有一个客户端的程序,两者互相配合可以开发出超多的网络程序,这是最基础的部分。 递归遍历矩阵 1个目标文件,简单! 多人聊天室 3...

    java开源包10

    SimpleCache 是一个简单易用的java缓存工具,用来简化缓存代码的编写,让你摆脱单调乏味的重复工作!1. 完全透明的缓存支持,对业务代码零侵入 2. 支持使用Redis和Memcached作为后端缓存。3. 支持缓存数据分区规则的...

    java开源包3

    SimpleCache 是一个简单易用的java缓存工具,用来简化缓存代码的编写,让你摆脱单调乏味的重复工作!1. 完全透明的缓存支持,对业务代码零侵入 2. 支持使用Redis和Memcached作为后端缓存。3. 支持缓存数据分区规则的...

    JAVA上百实例源码以及开源项目源代码

     Tcp服务端与客户端的JAVA实例源代码,一个简单的Java TCP服务器端程序,别外还有一个客户端的程序,两者互相配合可以开发出超多的网络程序,这是最基础的部分。 递归遍历矩阵 1个目标文件,简单! 多人聊天室 3...

    java开源包11

    SimpleCache 是一个简单易用的java缓存工具,用来简化缓存代码的编写,让你摆脱单调乏味的重复工作!1. 完全透明的缓存支持,对业务代码零侵入 2. 支持使用Redis和Memcached作为后端缓存。3. 支持缓存数据分区规则的...

    java开源包6

    SimpleCache 是一个简单易用的java缓存工具,用来简化缓存代码的编写,让你摆脱单调乏味的重复工作!1. 完全透明的缓存支持,对业务代码零侵入 2. 支持使用Redis和Memcached作为后端缓存。3. 支持缓存数据分区规则的...

    java开源包9

    SimpleCache 是一个简单易用的java缓存工具,用来简化缓存代码的编写,让你摆脱单调乏味的重复工作!1. 完全透明的缓存支持,对业务代码零侵入 2. 支持使用Redis和Memcached作为后端缓存。3. 支持缓存数据分区规则的...

    java开源包5

    SimpleCache 是一个简单易用的java缓存工具,用来简化缓存代码的编写,让你摆脱单调乏味的重复工作!1. 完全透明的缓存支持,对业务代码零侵入 2. 支持使用Redis和Memcached作为后端缓存。3. 支持缓存数据分区规则的...

    java开源包101

    SimpleCache 是一个简单易用的java缓存工具,用来简化缓存代码的编写,让你摆脱单调乏味的重复工作!1. 完全透明的缓存支持,对业务代码零侵入 2. 支持使用Redis和Memcached作为后端缓存。3. 支持缓存数据分区规则的...

Global site tag (gtag.js) - Google Analytics