上一篇memcached基本配置與使用http://blog.csdn.net/sup_heaven/article/details/32337711介紹了memcached的一些基本概念和一個範例。java
這一篇將以介紹一個memcached在項目中的應用。假設咱們有一個web應用,裏面有商品信息,文章信息,評論信息,其餘信息,咱們但願對其作緩存,那麼咱們在ServiceImpl層就不在調用DAOmpl層,而是調用CacheImpl層,在CacheImpl層中判斷要取出的商品信息是否已經在緩存中,若是在了,那麼直接從緩存中去,若是沒有這個時候仍是從數據庫中取,同時將它放到緩存中,以便下次使用。mysql
第一步、新建一個常量類,用於上面的四種信息的在數組中的索引。web
- 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;
- }
- }
這樣就在你的應用中使用了memcached,不過上面的只是部分代碼,跑不起來的哦。