Cacheonix:Java分散式叢集快取框架

2014-11-01    分類:開源軟體、快取系統、程式設計開發、首頁精華2人評論發表於2014-11-01

本文由碼農網 – 小峰原創,轉載請看清文末的轉載要求,歡迎參與我們的付費投稿計劃

記得之前分享過的一款Java分散式快取系統Ehcache,可以有效地減輕資料庫的讀寫負擔,提高Web系統的吞吐率。這次介紹的Cacheonix同樣也是一個基於Java的分散式叢集快取系統,它同樣可以幫助你實現分散式快取的部署。

Cacheonix的特點

  • 可靠的分散式 Java 快取
  • 通過複製實現高可用性
  • 支援泛型的快取 API
  • 可與 ORM 框架整合
  • 使用資料分割槽實現負載均衡
  • 支援非多播網路
  • 高效能運算
  • 快速的本地 Java 快取
  • 分散式鎖機制

Cacheonix的架構圖

Cacheonix分散式快取XML配置

<?xml version ="1.0"?>
<cacheonix xmlns="http://www.cacheonix.com/schema/configuration"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.cacheonix.com/schema/configuration http://www.cacheonix.com/schema/cacheonix-config-2.0.xsd">

   <server>

      <listener>
         <tcp port="8879" buffer="128k"/>
      </listener>

      <broadcast>
         <multicast multicastAddress="225.0.1.2" multicastPort="9998" multicastTTL="0"/>
      </broadcast>

      <partitionedCache name="customer.cache">
         <store>
            <lru maxElements="10000" maxBytes="10mb"/>
            <expiration idleTime="120s"/>
         </store>
      </partitionedCache>

      <partitionedCache name="invoice.cache">
         <store>
            <lru maxElements="10000" maxBytes="10mb"/>
            <expiration idleTime="120s"/>
         </store>
      </partitionedCache>

      <partitionedCache name="search.results.cache">
         <store>
            <lru maxBytes="5mb"/>
         </store>
      </partitionedCache>
   </server>
</cacheonix>

Cacheonix快取的存取

從配置中獲取Cacheonix例項

/**
 * Tester for CacheManager.
 */
public final class CacheonixTest extends TestCase {

   private Cacheonix cacheonix;

   /**
    * Tests getting an instance of CacheManager using a default Cacheonix configuration.
    */
   public void testGetInstance() {

      assertNotNull("Cacheonix created in setUp() method should not be null", cacheonix);
   }

   /**
    * Sets up the fixture. This method is called before a test is executed.
    * <p/>
    * Cacheonix receives the default configuration from a <code>cacheonix-config.xml</code> found in a class path or
    * using a file that name is defined by system parameter <code>cacheonix.config.xml<code>.
    */
   protected void setUp() throws Exception {

      super.setUp();

      // Get Cacheonix using a default Cacheonix configuration. The configuration
      // is stored in the conf/cacheonix-config.xml
      cacheonix = Cacheonix.getInstance();
   }

   /**
    * Tears down the fixture. This method is called after a test is executed.
    */
   protected void tearDown() throws Exception {

      // Cache manager has be be shutdown upon application exit.
      // Note that call to shutdown() here uses unregisterSingleton
      // set to true. This is necessary to support clean restart on setUp()
      cacheonix.shutdown(ShutdownMode.GRACEFUL_SHUTDOWN, true);
      cacheonix = null;

      super.tearDown();
   }
}

讀取快取

Cacheonix cacheonix = Cacheonix.getInstance();
Cache<String, String> cache = cacheonix.getCache("my.cache");
String cachedValue = cache.get("my.key");

設定快取

Cacheonix cacheonix = Cacheonix.getInstance();
Cache<String, String> cache = cacheonix.getCache("my.cache");
String replacedValue = cache.put("my.key", "my.value");

刪除快取

Cacheonix cacheonix = Cacheonix.getInstance();
Cache<String, String> cache = cacheonix.getCache("my.cache");
String removedValue = cache.remove("my.key");

Cacheonix作為一款開源的分散式快取框架,可以滿足中型企業規模的系統架構,對提升系統效能有非常棒的作用。

本文連結:http://www.codeceo.com/article/java-cacheonix.html
本文作者:碼農網 – 小峰
原創作品,轉載必須在正文中標註並保留原文連結和作者等資訊。]

相關文章