JCS實現分布式緩衝共用

來源:互聯網
上載者:User

由於要做一個產品,並想希望產品實現分布式,不得不研究下分布式緩衝。

緩衝的開源項目很多,通過測試,JCS配置及使用最為容易,所以就選用它做為產品緩衝管理器。

 

JCS是Apache的一個開源項目,項目網址:http://jakarta.apache.org/jcs/

實現步驟:

1、下載項目jar包,及依賴jar包

    jcs-1.3.jar

    commons-lang-2.3.jar

    commons-collections-2.1.1.jar

    concurrent-1.3.4.jar

2、JCS分布式配置

    在你項目WEB-INF/classes下設定檔cache.ccf

    內容如下:

       jcs.default=LTCP
       jcs.default.cache.attributes=org.apache.jcs.engine.CompositeCacheAttributes
       jcs.default.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache
       jcs.auxiliary.LTCP=org.apache.jcs.auxiliary.lateral.socket.tcp.LateralTCPCacheFactory
       jcs.auxiliary.LTCP.attributes=org.apache.jcs.auxiliary.lateral.socket.tcp.TCPLateralCacheAttributes
       jcs.auxiliary.LTCP.attributes.PutOnlyMode=true
       jcs.auxiliary.LTCP.attributes.TcpListenerPort=1100
       jcs.auxiliary.LTCP.attributes.UdpDiscoveryAddr=228.5.6.8
       jcs.auxiliary.LTCP.attributes.UdpDiscoveryPort=1101
       jcs.auxiliary.LTCP.attributes.UdpDiscoveryEnabled=true

   以上配置實現了區域網路內組播形式的通訊,你可以配置多台伺服器,當一台更新緩衝時,其他幾台緩衝同時被通知更新,實現分布式緩衝需求。

3、代碼實現

   實現代碼相當簡潔,附上我的緩衝管理類。

   當其中一台機子實現addCache.updateCache.removeCache方法時,均會觸發緩衝同步事件。

 

 

附:

package com.framework.cache;

import org.apache.jcs.JCS;

/**
 * 用途:緩衝管理器
 *
 * @author 顧偉民
 * @date 2009-11-12
 */
public class CacheManager {
 /**
  * 系統緩衝體
  */
 public static JCS CACHE;
 
 /**
  * 初始化緩衝管理器
  * @autor 顧偉民
  */
 public static void init() {
  try {
   CACHE = JCS.getInstance("SystemCache");
  }
  catch(Exception e) {
   e.printStackTrace();
  }
 }
 
 /**
  * 新增緩衝
  * @autor 顧偉民
  *
  * @param cacheKey 標識
  * @param obj 緩衝對象
  * @return true或false
  */
 public static boolean addCache(String cacheKey, Object obj) {
  try {
   CACHE.put(cacheKey, obj);
   return true;
  }
  catch(Exception e) {
   e.printStackTrace();
   return false;
  }
 }
 
 /**
  * 更新緩衝
  * @autor 顧偉民
  *
  * @param cacheKey 標識
  * @param obj 緩衝對象
  * @return true或false
  */
 public static boolean updateCache(String cacheKey, Object obj) {
  try {
   if(CACHE.get(cacheKey)!=null) removeCache(cacheKey);
   return addCache(cacheKey, obj);
  }
  catch(Exception e) {
   e.printStackTrace();
   return false;
  }
 }
 
 /**
  * 刪除緩衝
  * @autor 顧偉民
  *
  * @param key 標識
  * @return true或false
  */
 public static boolean removeCache(String cacheKey) {
  try {
   CACHE.remove(cacheKey);
   return true;
  }
  catch(Exception e) {
   e.printStackTrace();
   return false;
  }
 }
 
 /**
  * 擷取指定緩衝對象
  * @autor 顧偉民
  *
  * @param key 標識
  * @return Object 緩衝對象
  */
 public static Object getCache(String cacheKey) {
  try {
   return CACHE.get(cacheKey);
  }
  catch(Exception e) {
   e.printStackTrace();
   return null;
  }
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.