memcached真实项目中的应用

来源:互联网 发布:mac如何安装office 编辑:程序博客网 时间:2024/04/28 12:04

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

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

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

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public class MemcachedConstant {  
  2.     public static final int MEMCACHED_GOODSDETAIL = 0;  
  3.     public static final int MEMCACHED_ARTICLEDETAIL = 1;  
  4.     public static final int MEMCACHED_COMMENTDETAIL = 2;  
  5.     public static final int MEMCACHED_OTHERDETAIL = 3;  
  6. }  

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

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public class MemcachedKeyUtil {  
  2.     private static final String GOODS_KEY_PREFIX = "goods_";  
  3.       
  4.     public static String getGoodsKey(long goodsId) {  
  5.         return GOODS_KEY_PREFIX + goodsId;  
  6.     }  
  7. }  

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

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public class MemcachedUtil {  
  2.     private int MEMCACHED_SERVER_NUM = 4;  
  3.     private SockIOPool[] pools = new SockIOPool[MEMCACHED_SERVER_NUM];  
  4.     private MemCachedClient[] mcs = new MemCachedClient[MEMCACHED_SERVER_NUM];  
  5.     private final String[] poolNames = new String[] { "GOODSDETAIL_POOL""""""" };  
  6.     private static MemcachedUtil instance;  
  7.     private MemcachedUtil() {  
  8.         this.init();  
  9.     }  
  10.     // 单例  
  11.     public static MemcachedUtil getInstance() {  
  12.         if (MemcachedUtil.instance == null) {  
  13.             synchronized (MemcachedUtil.class) {  
  14.                 if (MemcachedUtil.instance == null) {  
  15.                     MemcachedUtil.instance = new MemcachedUtil();  
  16.                 }  
  17.             }  
  18.         }  
  19.         return MemcachedUtil.instance;  
  20.     }  
  21.       
  22.     public Object get(int index, String key) {  
  23.         return this.mcs[index].get(key);  
  24.     }  
  25.       
  26.     public boolean set(int index, String key, Object value) {  
  27.         return this.mcs[index].set(key, value);  
  28.     }  
  29.       
  30.     public boolean delete(String key) {  
  31.         return this.mcs[index].delete(key);  
  32.     }  
  33.     public MemCachedClient getMemCachedClient(int index) {  
  34.         return this.mcs[index];  
  35.     }  
  36.       
  37.     public void init() {  
  38.         for (int i = 0; i < MEMCACHED_SERVER_NUM; ++i) {  
  39.             this.pools[i] = SockIOPool.getInstance(poolNames[i]);  
  40.             this.pools[i].setServers(servers);  
  41.             this.pools[i].setWeights(weights);  
  42.             this.pools[i].setInitConn(initConn);  
  43.             this.pools[i].setMinConn(minConn);  
  44.             this.pools[i].setMaxConn(maxConn);  
  45.             this.pools[i].setMaxIdle(maxIdle);  
  46.             this.pools[i].setMaxBusyTime(maxBusyTime);  
  47.             this.pools[i].setMaintSleep(maintSleep);  
  48.             this.pools[i].setNagle(ifNagle);  
  49.             this.pools[i].setSocketTO(socketTO);  
  50.             this.pools[i].setSocketConnectTO(socketConnectTO);  
  51.             this.pools[i].setFailover(ifFailOver);  
  52.             this.pools[i].setFailback(ifFailback);  
  53.             this.pools[i].setAliveCheck(ifAliveCheck);  
  54.             this.pools[i].initialize();  
  55.             this.mcs[i] = new MemCachedClient(poolNames[i]);  
  56.         }  
  57.     }  
  58. }  

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

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public class MemcachedSupport {  
  2.     public boolean setDetailData(String key, Object value) {  
  3.         return MemcachedUtil.getInstance().set(MemcachedConstant.MEMCACHED_DETAIL, key, value);  
  4.     }  
  5.       
  6.     public Object getDetailData(String key) {  
  7.         return MemcachedUtil.getInstance().get(MemcachedConstant.MEMCACHED_DETAIL, key);  
  8.     }  
  9.       
  10.     public boolean deleteDetailData(String key) {  
  11.         return MemcachedUtil.getInstance().delete(MemcachedConstant.MEMCACHED_DETAIL);  
  12.     }  
  13. }  

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

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public class GoodsCacheImpl extends MemcachedSupport{  
  2.     @Resource(name = "goodsDaoImpl")  
  3.     private GoodsDao goodsDao;  
  4.       
  5.     public Goods selectGoodsById(long goodsId) {  
  6.         Goods goods = null;  
  7.         String goodsKey = MemcachedKeyUtil.getGoodsKey(goodsId);  
  8.         goods = (Goods) getDetailData(goodsKey);  
  9.         if (goods == null) {  
  10.             goods = goodsDao.selectGoodsById(goodsId, false);  
  11.             if (goods != null) {  
  12.                 setDetailData(goodsKey, goods);  
  13.             }  
  14.         }  
  15.         return goods;  
  16.     }  
  17. }  

这样就在你的应用中使用了memcached,不过上面的只是部分代码,跑不起来的哦。
0 0