这一篇将以介绍一个memcached在项目中的应用。假设我们有一个web应用,里面有商品信息,文章信息,评论信息,其他信息,我们希望对其做缓存,那么我们在ServiceImpl层就不在调用DAOmpl层,而是调用CacheImpl层,在CacheImpl层中判断要取出的商品信息是否已经在缓存中,如果在了,那么直接从缓存中去,如果没有这个时候还是从数据库中取,同时将它放到缓存中,以便下次使用。

第一步、新建一个常量类,用于上面的四种信息的在数组中的索引。

public class MemcachedConstant {
public static final int MEMCACHED_GOODSDETAIL = 0;
public static final int MEMCACHED_ARTICLEDETAIL = 1;
public static final int MEMCACHED_COMMENTDETAIL = 2;
public static final int MEMCACHED_OTHERDETAIL = 3;
}

  

第二步、由于有大量的商品信息,我们在放入缓存时必须给定一个key,那么我们最好规范的命名不同类别的key,如商品的key就是商品的前缀加上商品的编号。

public class MemcachedKeyUtil {
private static final String GOODS_KEY_PREFIX = "goods_"; public static String getGoodsKey(long goodsId) {
return GOODS_KEY_PREFIX + goodsId;
}
}

  

第三步、我们建一个和上一篇文章中一样的工具类,用于新建pool、client,操作缓存等。这里再强调一下,一个pool关联多个server(就是会根据权重将缓存放在这些servers上),一个client会通过poolName关联具体的pool。

public class MemcachedUtil {
private int MEMCACHED_SERVER_NUM = 4;
private SockIOPool[] pools = new SockIOPool[MEMCACHED_SERVER_NUM];
private MemCachedClient[] mcs = new MemCachedClient[MEMCACHED_SERVER_NUM];
private final String[] poolNames = new String[] { "GOODSDETAIL_POOL", "", "", "" };
private static MemcachedUtil instance;
private MemcachedUtil() {
this.init();
}
// 单例
public static MemcachedUtil getInstance() {
if (MemcachedUtil.instance == null) {
synchronized (MemcachedUtil.class) {
if (MemcachedUtil.instance == null) {
MemcachedUtil.instance = new MemcachedUtil();
}
}
}
return MemcachedUtil.instance;
} public Object get(int index, String key) {
return this.mcs[index].get(key);
} public boolean set(int index, String key, Object value) {
return this.mcs[index].set(key, value);
} public boolean delete(String key) {
return this.mcs[index].delete(key);
}
public MemCachedClient getMemCachedClient(int index) {
return this.mcs[index];
} public void init() {
for (int i = 0; i < MEMCACHED_SERVER_NUM; ++i) {
this.pools[i] = SockIOPool.getInstance(poolNames[i]);
this.pools[i].setServers(servers);
this.pools[i].setWeights(weights);
this.pools[i].setInitConn(initConn);
this.pools[i].setMinConn(minConn);
this.pools[i].setMaxConn(maxConn);
this.pools[i].setMaxIdle(maxIdle);
this.pools[i].setMaxBusyTime(maxBusyTime);
this.pools[i].setMaintSleep(maintSleep);
this.pools[i].setNagle(ifNagle);
this.pools[i].setSocketTO(socketTO);
this.pools[i].setSocketConnectTO(socketConnectTO);
this.pools[i].setFailover(ifFailOver);
this.pools[i].setFailback(ifFailback);
this.pools[i].setAliveCheck(ifAliveCheck);
this.pools[i].initialize();
this.mcs[i] = new MemCachedClient(poolNames[i]);
}
}
}

  

第四步、新建一个基类以供所用继承它的CacheImpl直接调用MemcachedUtil里的方法,如果不写该类那么在CacheImpl中会有很多重复的操作MemcachedUtil的代码。

public class MemcachedSupport {
public boolean setDetailData(String key, Object value) {
return MemcachedUtil.getInstance().set(MemcachedConstant.MEMCACHED_DETAIL, key, value);
} public Object getDetailData(String key) {
return MemcachedUtil.getInstance().get(MemcachedConstant.MEMCACHED_DETAIL, key);
} public boolean deleteDetailData(String key) {
return MemcachedUtil.getInstance().delete(MemcachedConstant.MEMCACHED_DETAIL);
}
}

  

第五步、新建一个GoodsCacheImpl,该类的作用就是一开始所说的,娶不到缓存,就调用DAO查询并放入缓存,如果缓存中有就直接从缓存中拿。

public class GoodsCacheImpl extends MemcachedSupport{
@Resource(name = "goodsDaoImpl")
private GoodsDao goodsDao; public Goods selectGoodsById(long goodsId) {
Goods goods = null;
String goodsKey = MemcachedKeyUtil.getGoodsKey(goodsId);
goods = (Goods) getDetailData(goodsKey);
if (goods == null) {
goods = goodsDao.selectGoodsById(goodsId, false);
if (goods != null) {
setDetailData(goodsKey, goods);
}
}
return goods;
}
}

  原文链接 :http://blog.csdn.net/sup_heaven/article/details/32728477/

memcached在项目中的应用的更多相关文章

  1. memcached真实项目中的应用

    上一篇memcached介绍及基本使用介绍了memcached的一些基本概念和一个范例.这一篇将介绍一个memcached在实际项目中的应用

  2. 在Java项目中部署使用Memcached[转]

    在项目中使用到Memcached主要的目的是,通过缓存数据库查询结果,减少数据库访问次数,从而提高动态.数据库驱动网站的速度.提高可扩展性.Memcached是一个高性能的分布式内存对象缓存系统,基于 ...

  3. Key/Value之王Memcached初探:二、Memcached在.Net中的基本操作

    一.Memcached ClientLib For .Net 首先,不得不说,许多语言都实现了连接Memcached的客户端,其中以Perl.PHP为主. 仅仅memcached网站上列出的语言就有: ...

  4. 【转】 Key/Value之王Memcached初探:二、Memcached在.Net中的基本操作

    一.Memcached ClientLib For .Net 首先,不得不说,许多语言都实现了连接Memcached的客户端,其中以Perl.PHP为主. 仅仅memcached网站上列出的语言就有: ...

  5. 【转载】Memcached在.Net中的基本操作

    一.Memcached ClientLib For .Net 首先,不得不说,许多语言都实现了连接Memcached的客户端,其中以Perl.PHP为主. 仅仅memcached网站上列出的语言就有: ...

  6. Key/Value之王Memcached初探:二、Memcached在.Net中的基本操作 - Edison Chou

    一.Memcached ClientLib For .Net 首先,不得不说,许多语言都实现了连接Memcached的客户端,其中以Perl.PHP为主. 仅仅memcached网站上列出的语言就有: ...

  7. Memcached在.net中的应用

    一.MemCached下载 服务端下载:http://memcachedproviders.codeplex.com/ client下载:path=/trunk">http://sou ...

  8. Memcached在.Net中的基本操作

    Memcached在.Net中的基本操作 一.Memcached ClientLib For .Net 首先,不得不说,许多语言都实现了连接Memcached的客户端,其中以Perl.PHP为主. 仅 ...

  9. 在.NET项目中使用PostSharp,使用CacheManager实现多种缓存框架的处理

    在前面几篇随笔中,介绍了PostSharp的使用,以及整合MemoryCache,<在.NET项目中使用PostSharp,实现AOP面向切面编程处理>.<在.NET项目中使用Pos ...

随机推荐

  1. html+css基础篇

    2016年11月19号,计划把基础在看一下,听大神说好的东西就要多看几遍,知识是学来用的解决问题的,加油 接下来的是我在自学中的小笔记吧,每天都在保持几个小时的学习思考状态,由于要升本所以学前端的时间 ...

  2. Jenkins slave image

    Add a new shell script configure_slave.sh as following: #!/bin/bash dnf -openjdk git wget openssh-se ...

  3. URL中的#

    作者:阮一峰   http://www.ruanyifeng.com/blog/2011/03/url_hash.html 一.#的涵义 #代表网页中的一个位置.其右面的字符,就是该位置的标识符.比如 ...

  4. Markdown引用本地图片语法

    Markdown引用本地图片语法 markdown引用图片标准方式如下: ![Alt text](/path/to/img.jpg) 测试markdown文本如下: # 测试相对路径图片 ![Alt ...

  5. HTML5扩展之微数据与丰富网页摘要itemscope, itemtype, itemprop

    HTML5扩展之微数据与丰富网页摘要 by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpr ...

  6. Hadoop之MapReduce分布式计算

    简单介绍一下项目背景——很简单,作死去接下老师的活,然后一干就是半个月,一直忙着从零基础到使用Hadoop中的MapReduce来解决一个实际问题,也就是用来计算一个数据量较大的二度朋友关系. 那么首 ...

  7. 【Loadrunner】初学Loadrunner——场景设计

    在使用Loadrunner的时候,常常需要使用到场景设计.但是怎么设计一个满意的场景?如何开展? 首先可以点击tools > Create Controller Scenario > OK ...

  8. 各个Maven仓库镜像(包括国内)

    各个Maven仓库镜像(包括国内) 衽孤魍墓 ゅ槭 众矿工唯唯诺诺我在旁哭笑不得原 宦蠃サ 骘猩池 粑涫汾滹 吧滔哌蹋 飑俗た 狃攵庾唾 想必是想挡住什么我想反正这笔筒也不是 翡蜮胼 娴左 ...

  9. bfs或者dfs Good Bye 2016 D

    http://codeforces.com/contest/750/problem/D 题目大意: 放鞭炮,鞭炮会爆炸n次,每次只会往目前前进方向的左上和右上放出他的子鞭炮.问,最后能有多少格子被覆盖 ...

  10. javascript 私有方法的实现

    原文地址: http://frugalcoder.us/post/2010/02/11/js-classes.aspx Classy JavaScript - Best Practices 11. F ...