Java分布式集群緩存框架:Cacheonix

jopen 10年前發布 | 37K 次閱讀 Cacheonix 緩存組件

記得之前分享過的一款Java分布式緩存系統Ehcache,可以有效地減輕數據庫的讀寫負擔,提高Web系統的吞吐率。這次介紹的Cacheonix同樣也是一個基于Java的分布式集群緩存系統,它同樣可以幫助你實現分布式緩存的部署。

Java分布式集群緩存框架:Cacheonix

Cacheonix的特點

  • 可靠的分布式 Java 緩存
  • 通過復制實現高可用性
  • 支持泛型的緩存 API
  • 可與 ORM 框架集成
  • 使用數據分區實現負載均衡
  • 支持非多播網絡
  • 高性能計算
  • 快速的本地 Java 緩存
  • 分布式鎖機制

Cacheonix的架構圖

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

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!