memcached真实项目中的应用

上一篇memcached基本配置与使用http://blog.csdn.net/sup_heaven/article/details/32337711介绍了memcached的一些基本概念和一个范例。

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

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

[java] view plain copy
在CODE上查看代码片 派生到我的代码片
  1. publicclassMemcachedConstant{
  2. publicstaticfinalintMEMCACHED_GOODSDETAIL=0;
  3. publicstaticfinalintMEMCACHED_ARTICLEDETAIL=1;
  4. publicstaticfinalintMEMCACHED_COMMENTDETAIL=2;
  5. publicstaticfinalintMEMCACHED_OTHERDETAIL=3;
  6. }

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

[java] view plain copy
在CODE上查看代码片 派生到我的代码片
  1. publicclassMemcachedKeyUtil{
  2. privatestaticfinalStringGOODS_KEY_PREFIX="goods_";
  3. publicstaticStringgetGoodsKey(longgoodsId){
  4. returnGOODS_KEY_PREFIX+goodsId;
  5. }
  6. }

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

[java] view plain copy
在CODE上查看代码片 派生到我的代码片
  1. publicclassMemcachedUtil{
  2. privateintMEMCACHED_SERVER_NUM=4;
  3. privateSockIOPool[]pools=newSockIOPool[MEMCACHED_SERVER_NUM];
  4. privateMemCachedClient[]mcs=newMemCachedClient[MEMCACHED_SERVER_NUM];
  5. privatefinalString[]poolNames=newString[]{"GOODSDETAIL_POOL","","",""};
  6. privatestaticMemcachedUtilinstance;
  7. privateMemcachedUtil(){
  8. this.init();
  9. }
  10. //单例
  11. publicstaticMemcachedUtilgetInstance(){
  12. if(MemcachedUtil.instance==null){
  13. synchronized(MemcachedUtil.class){
  14. if(MemcachedUtil.instance==null){
  15. MemcachedUtil.instance=newMemcachedUtil();
  16. }
  17. }
  18. }
  19. returnMemcachedUtil.instance;
  20. }
  21. publicObjectget(intindex,Stringkey){
  22. returnthis.mcs[index].get(key);
  23. }
  24. publicbooleanset(intindex,Stringkey,Objectvalue){
  25. returnthis.mcs[index].set(key,value);
  26. }
  27. publicbooleandelete(Stringkey){
  28. returnthis.mcs[index].delete(key);
  29. }
  30. publicMemCachedClientgetMemCachedClient(intindex){
  31. returnthis.mcs[index];
  32. }
  33. publicvoidinit(){
  34. for(inti=0;i<MEMCACHED_SERVER_NUM;++i){
  35. this.pools[i]=SockIOPool.getInstance(poolNames[i]);
  36. this.pools[i].setServers(servers);
  37. this.pools[i].setWeights(weights);
  38. this.pools[i].setInitConn(initConn);
  39. this.pools[i].setMinConn(minConn);
  40. this.pools[i].setMaxConn(maxConn);
  41. this.pools[i].setMaxIdle(maxIdle);
  42. this.pools[i].setMaxBusyTime(maxBusyTime);
  43. this.pools[i].setMaintSleep(maintSleep);
  44. this.pools[i].setNagle(ifNagle);
  45. this.pools[i].setSocketTO(socketTO);
  46. this.pools[i].setSocketConnectTO(socketConnectTO);
  47. this.pools[i].setFailover(ifFailOver);
  48. this.pools[i].setFailback(ifFailback);
  49. this.pools[i].setAliveCheck(ifAliveCheck);
  50. this.pools[i].initialize();
  51. this.mcs[i]=newMemCachedClient(poolNames[i]);
  52. }
  53. }
  54. }

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

[java] view plain copy
在CODE上查看代码片 派生到我的代码片
  1. publicclassMemcachedSupport{
  2. publicbooleansetDetailData(Stringkey,Objectvalue){
  3. returnMemcachedUtil.getInstance().set(MemcachedConstant.MEMCACHED_DETAIL,key,value);
  4. }
  5. publicObjectgetDetailData(Stringkey){
  6. returnMemcachedUtil.getInstance().get(MemcachedConstant.MEMCACHED_DETAIL,key);
  7. }
  8. publicbooleandeleteDetailData(Stringkey){
  9. returnMemcachedUtil.getInstance().delete(MemcachedConstant.MEMCACHED_DETAIL);
  10. }
  11. }

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

[java] view plain copy
在CODE上查看代码片 派生到我的代码片
  1. publicclassGoodsCacheImplextendsMemcachedSupport{
  2. @Resource(name="goodsDaoImpl")
  3. privateGoodsDaogoodsDao;
  4. publicGoodsselectGoodsById(longgoodsId){
  5. Goodsgoods=null;
  6. StringgoodsKey=MemcachedKeyUtil.getGoodsKey(goodsId);
  7. goods=(Goods)getDetailData(goodsKey);
  8. if(goods==null){
  9. goods=goodsDao.selectGoodsById(goodsId,false);
  10. if(goods!=null){
  11. setDetailData(goodsKey,goods);
  12. }
  13. }
  14. returngoods;
  15. }
  16. }

这样就在你的应用中使用了memcached,不过上面的只是部分代码,跑不起来的哦。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值