日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

5个强大的Java分布式缓存框架推荐

發(fā)布時間:2025/7/14 java 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 5个强大的Java分布式缓存框架推荐 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

2019獨角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>

在開發(fā)中大型Java軟件項目時,很多Java架構(gòu)師都會遇到數(shù)據(jù)庫讀寫瓶頸,如果你在系統(tǒng)架構(gòu)時并沒有將緩存策略考慮進(jìn)去,或者并沒有選擇更優(yōu)的緩存策略,那么到時候重構(gòu)起來將會是一個噩夢。本文主要是分享了5個常用的Java分布式緩存框架,這些緩存框架支持多臺服務(wù)器的緩存讀寫功能,可以讓你的緩存系統(tǒng)更容易擴展。

 1、Ehcache – Java分布式緩存框架

  Ehcache是一個Java實現(xiàn)的開源分布式緩存框架,EhCache 可以有效地減輕數(shù)據(jù)庫的負(fù)載,可以讓數(shù)據(jù)保存在不同服務(wù)器的內(nèi)存中,在需要數(shù)據(jù)的時候可以快速存取。同時EhCache 擴展非常簡單,官方提供的Cache配置方式有好幾種。你可以通過聲明配置、在xml中配置、在程序里配置或者調(diào)用構(gòu)造方法時傳入不同的參數(shù)。

  官方網(wǎng)站:http://ehcache.org/

  Ehcache有以下特點:

  • 存取速度非常快,性能很不錯。
  • 可以應(yīng)用多種緩存策略。
  • 分級緩存,用戶可以指定哪些數(shù)據(jù)在硬盤中緩存,哪些數(shù)據(jù)在內(nèi)存中緩存。
  • 可以通過RMI、可插入API等方式進(jìn)行分布式緩存。
  • 具有緩存和緩存管理器的偵聽接口。
  • 支持多緩存管理器實例,以及一個實例的多個緩存區(qū)域。
  • 默認(rèn)提供Hibernate的緩存實現(xiàn)。

  Ehcache的配置示例代碼:

<ehcache><diskStore path=”java.io.tmpdir”/><defaultCachemaxElementsInMemory=”10000″eternal=”false” timeToIdleSeconds=”120″timeToLiveSeconds=”120″overflowToDisk=”true” maxElementsOnDisk=”10000000″diskPersistent=”false” diskExpiryThreadIntervalSeconds=”120″memoryStoreEvictionPolicy=”LRU” /></ehcache>

 在同類的Java緩存框架中,Ehcache配置相對簡單,也比較容易上手,最大的優(yōu)勢是它支持分布式緩存。

2、Cacheonix – 高性能Java分布式緩存系統(tǒng)

  Cacheonix同樣也是一個基于Java的分布式集群緩存系統(tǒng),它同樣可以幫助你實現(xiàn)分布式緩存的部署。

  官方網(wǎng)站:http://www.cacheonix.com/

  Cacheonix的特點

  • 可靠的分布式 Java 緩存
  • 通過復(fù)制實現(xiàn)高可用性
  • 支持泛型的緩存 API
  • 可與 ORM 框架集成
  • 使用數(shù)據(jù)分區(qū)實現(xiàn)負(fù)載均衡
  • 支持非多播網(wǎng)絡(luò)
  • 高性能計算
  • 快速的本地 Java 緩存
  • 分布式鎖機制

  Cacheonix的架構(gòu)圖

  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.xmlcacheonix = 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");

設(shè)置緩存

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作為一款開源的分布式緩存框架,可以滿足中型企業(yè)規(guī)模的系統(tǒng)架構(gòu),對提升系統(tǒng)性能有非常棒的作用。

3、ASimpleCache – 輕量級Android緩存框架

  ASimpleCache是一款基于Android的輕量級緩存框架,它只有一個Java文件,ASimpleCache基本可以緩存常用的Android對象,包括普通字符串、JSON對象、經(jīng)過序列化的Java對象、字節(jié)數(shù)組等。

  官方網(wǎng)站:https://github.com/yangfuhai/ASimpleCache

  ASimpleCache可以緩存哪些東西

  ASimpleCache基本可以緩存常用的Android對象,包括但不限于以下幾種類型:

  • 普通字符串
  • JSON對象
  • 經(jīng)過序列化的Java對象
  • 字節(jié)數(shù)組

  ASimpleCache的特點

  • 輕量級,只有一個Java文件
  • 完整而靈活的配置,可以配置緩存路徑,緩存大小,緩存數(shù)量,緩存超時時間等。
  • 超時緩存自動失效,并從內(nèi)存中自動刪除。
  • 多進(jìn)程的支持

  在Android開發(fā)中,我們可以用ASimpleCache來替換SharePreference配置文件,特別是如果你的應(yīng)用經(jīng)常要從互聯(lián)網(wǎng)上讀取數(shù)據(jù),那么利用ASimpleCache可以緩存這些請求數(shù)據(jù),等一段時間失效后再去重新讀取,這樣可以減少客戶端流量,同時減少服務(wù)器并發(fā)量。

  ASimpleCache的示例代碼

  設(shè)置緩存數(shù)據(jù):

ACache mCache = ACache.get(this); mCache.put("test_key1", "test value"); mCache.put("test_key2", "test value", 10);//保存10秒,如果超過10秒去獲取這個key,將為null mCache.put("test_key3", "test value", 2 * ACache.TIME_DAY);//保存兩天,如果超過兩天去獲取這個key,將為null

獲取緩存數(shù)據(jù):

ACache mCache = ACache.get(this); String value = mCache.getAsString("test_key1");

ASimpleCache的作者是國人,代碼托管在Github上,也用過ASimpleCache的同學(xué)可以分享一下使用心得,為開源事業(yè)貢獻(xiàn)一份力量。

4、JBoss Cache – 基于事物的Java緩存框架

  JBoss Cache是一款基于Java的事務(wù)處理緩存系統(tǒng),它的目標(biāo)是構(gòu)建一個以Java框架為基礎(chǔ)的集群解決方案,可以是服務(wù)器應(yīng)用,也可以是Java SE應(yīng)用。

  官方網(wǎng)站:http://jbosscache.jboss.org/

  集群高可用性

  JBoss Cache將會自動復(fù)制緩存數(shù)據(jù),并且在集群中的服務(wù)器之間進(jìn)行緩存數(shù)據(jù)的同步,這樣可以保證任何一臺服務(wù)器重啟了都不會影響緩存的可用性。

  集群緩存可避免系統(tǒng)瓶頸

  JBoss Cache顧名思義是利用緩存來提高系統(tǒng)擴展性的,當(dāng)我們的WEB系統(tǒng)遇到大量的數(shù)據(jù)庫讀寫時,系統(tǒng)的瓶頸將會出現(xiàn)在數(shù)據(jù)庫端,JBoss Cache正好可以解決數(shù)據(jù)庫的頻繁讀取問題,解決這個瓶頸。

  另外,由于JBoss Cache的緩存是在集群中的每一個服務(wù)器間同步的,因此也不會因為一臺緩存服務(wù)器遇到性能問題而影響整個系統(tǒng)。

  JBoss Cache的standalone用法

  首先是初始化TreeCache

TreeCache tree = new TreeCache();

然后是讀進(jìn)配置文件

PropertyConfigurator config = new PropertyConfigurator(); config.configure("配置文件.xml");

然后開始服務(wù)

Tree.startService();

因為Tree的結(jié)構(gòu)是用NODE來Access的,TreeCache這里就很簡單的用:

  /level1/level2/node1 來表示兩級Tree下面的Node1。

  現(xiàn)在我們添加幾個要Cache的對象。

Tree.put("/level1/level2/node1", "key1", "value1"); String[] array = { "1", "2", "3", "4" } Tree.put("/level3/array/", "myarray", array);

大家可以看到,TreeCache里面可以存儲任何種類的對象,包括所有復(fù)雜對象。

  讀取對象就很方便了

String s = (String)Tree.get("/level1/level2/node1/", "key1");

value1就讀出來了。

  同理:

String[] sarr = (String[]) Tree.get("/level3/array/","myarray");

System.out.println(sarr[1]) 會顯示2

  最后停止服務(wù):

Tree.stopService();

JBoss Cache的FileCacheLoader示例

  首先創(chuàng)建一個FileCache類封裝JBoss Cache的相關(guān)操作,如下:

package com.javaeye.terrencexu.jbosscache; import java.io.File; import java.util.Map; import org.jboss.cache.Cache; import org.jboss.cache.DefaultCacheFactory; import org.jboss.cache.Fqn; import org.jboss.cache.Node; import org.jboss.cache.config.CacheLoaderConfig; import org.jboss.cache.config.Configuration; import org.jboss.cache.loader.FileCacheLoader; import org.jboss.cache.loader.FileCacheLoaderConfig; /** * <p> * This is demo to illustrate how to use the JBoss Cache to cache your * frequently accessed Java objects in order to dramatically improve * the performance of your applications. This makes it easy to remove * data access bottlenecks, such as connecting to a database. * </p> * <p> * As a rule of thumb, it is recommended that the FileCacheLoader not * be used in a highly concurrent, transactional or stressful environment, * ant its use is restricted to testing. * </p> * * @author TerrenceX * * @param <T> */ public class FileCache<T> { /** * The JBoss Cache, used to cache frequently accessed Java objects. */ private Cache<String, T> cache; /** * @constructor * @param fsCacheLoaderLocation The file system location to store the cache */ public FileCache(File fsCacheLoaderLocation) { cache = initCache(fsCacheLoaderLocation); } /** * Create a Cache and whose cache loader type is File Cache Loader * * @param fsCacheLoaderLocation The file position used to store the cache. * * @return Cache */ public Cache<String, T> initCache(File fsCacheLoaderLocation) { // initiate a FileCacheLoader instance FileCacheLoader fsCacheLoader = new FileCacheLoader(); // prepare the file cache loader configuration file for File Cache Loader FileCacheLoaderConfig fsCacheLoaderConfig = new FileCacheLoaderConfig(); fsCacheLoaderConfig.setLocation(fsCacheLoaderLocation.toString()); fsCacheLoaderConfig.setCacheLoader(fsCacheLoader); // set configuration to File Cache Loader fsCacheLoader.setConfig(fsCacheLoaderConfig); // prepare the configuration for Cache Configuration config = new Configuration(); config.setCacheLoaderConfig(new CacheLoaderConfig()); config.getCacheLoaderConfig().addIndividualCacheLoaderConfig(fsCacheLoaderConfig); // create a Cache through the default cache factory return new DefaultCacheFactory<String, T>().createCache(config); } /** * Add a new node into the tree-node hierarchy * * @param fqn Full Qualified Name for the new node * @return */ public Node<String, T> addNode(Fqn<String> fqn) { return cache.getRoot().addChild(fqn); } /** * Remove a specified node from the tree-node hierarchy * * @param fqn Full Qualified Name for the specified node */ public void removeNode(Fqn<String> fqn) { cache.removeNode(fqn); } /** * Add node information to the specified node. * * @param fqn Full Qualified Name for the specified node * @param key The key of the node information * @param value The value of the node information */ public void addNodeInfo(Fqn<String> fqn, String key, T value) { cache.put(fqn, key, value); } /** * Batch add node information to the specified node. * * @param fqn Full Qualified Name for the specified node * @param infos Node informations map */ public void addNodeInfos(Fqn<String> fqn, Map<String, T> infos) { cache.put(fqn, infos); } /** * Get node information from the specified node. * * @param fqn Full Qualified Name for the specified node * @param key The key of the node information * @return */ public T getNodeInfo(Fqn<String> fqn, String key) { return cache.get(fqn, key); } /** * Remove node information from the specified node. * * @param fqn Full Qualified Name for the specified node * @param key The key of the node information */ public void removeNodeInfo(Fqn<String> fqn, String key) { cache.remove(fqn, key); } }

下面是一個測試案例:

package com.javaeye.terrencexu.jbosscache; import java.io.File; import org.jboss.cache.Fqn; public class Main { public static void main(String[] args) { FileCache<String> fileCache = new FileCache<String>(new File("d:\\tmp")); Fqn<String> jimmyFqn = Fqn.fromString("/com/manager/jimmy"); Fqn<String> hansonFqn = Fqn.fromString("/com/developer/hanson"); fileCache.addNode(jimmyFqn); fileCache.addNode(hansonFqn); fileCache.addNodeInfo(jimmyFqn, "en-name", "Jimmy Zhang"); fileCache.addNodeInfo(jimmyFqn, "zh-name", "Zhang Ji"); fileCache.addNodeInfo(hansonFqn, "en-name", "Hanson Yang"); fileCache.addNodeInfo(hansonFqn, "zh-name", "Yang Kuo"); String enName = fileCache.getNodeInfo(hansonFqn, "en-name"); System.out.println(enName); } }

運行結(jié)果如下:

- JBossCache MBeans were successfully registered to the platform mbean server. - JBoss Cache version: JBossCache 'Malagueta' 3.2.5.GA Hanson Yang

生成的緩存文件目錄結(jié)構(gòu)如下:

D:/tmp/com.fdb/manage.fdb/jimmy.fdb/data.dat D:/tmp/com.fdb/developer.fdb/hanson.fdb/data.dat

總結(jié)

  JBoss Cache還有更多的用法,如果你的系統(tǒng)遇到數(shù)據(jù)庫瓶頸問題,可以考慮使用JBoss Cache來解決。

5、Voldemort – 基于鍵-值(key-value)的緩存框架

  Voldemort是一款基于Java開發(fā)的分布式鍵-值緩存系統(tǒng),像JBoss Cache一樣,Voldemort同樣支持多臺服務(wù)器之間的緩存同步,以增強系統(tǒng)的可靠性和讀取性能。

  官方網(wǎng)站:http://www.project-voldemort.com/voldemort/

  Voldemort的特點

  • 緩存數(shù)據(jù)可以自動在各個服務(wù)器節(jié)點之間同步復(fù)制。
  • 每一個服務(wù)器的緩存數(shù)據(jù)被橫向分割,因此是總緩存的一個子集。
  • 嚴(yán)格保持緩存的一致性。
  • 提供服務(wù)器宕機快速恢復(fù)方案。
  • 可配置的數(shù)據(jù)存儲引擎。
  • 可配置的數(shù)據(jù)序列化方式。
  • 每一個數(shù)據(jù)項都有版本標(biāo)識,用來保證數(shù)據(jù)的完整性和可用性。
  • 每一個緩存節(jié)點都是獨立的,因此任何一個節(jié)點的故障都不會影響系統(tǒng)的正常運行。

  Voldemort鍵-值原理圖

Voldemort邏輯架構(gòu)圖

Voldemort物理架構(gòu)圖

  Voldemort的配置方式

  集群配置文件:

<cluster><!-- The name is just to help users identify this cluster from the gui --><name>mycluster</name><zone><zone-id>0</zone-id><proximity-list>1</proximity-list><zone><zone><zone-id>1</zone-id><proximity-list>0</proximity-list><zone><server><!-- The node id is a unique, sequential id beginning with 0 that identifies each server in the cluster--><id>0</id><host>vldmt1.prod.linkedin.com</host><http-port>8081</http-port><socket-port>6666</socket-port><admin-port>6667</admin-port><!-- A list of data partitions assigned to this server --><partitions>0,1,2,3</partitions><zone-id>0</zone-id></server><server><id>1</id><host>vldmt2.prod.linkedin.com</host><http-port>8081</http-port><socket-port>6666</socket-port><admin-port>6667</admin-port><partitions>4,5,6,7</partitions><zone-id>1</zone-id></server></cluster>

數(shù)據(jù)存儲方式配置文件:

<stores><store><name>test</name><replication-factor>2</replication-factor><preferred-reads>2</preferred-reads><required-reads>1</required-reads><preferred-writes>2</preferred-writes><required-writes>1</required-writes><persistence>bdb</persistence><routing>client</routing><routing-strategy>consistent-routing</routing-strategy><key-serializer><type>string</type><schema-info>utf8</schema-info></key-serializer><value-serializer><type>json</type><schema-info version="1">[{"id":"int32", "name":"string"}]</schema-info><compression><type>gzip<type></compression></value-serializer></store></stores>

Voldemort的使用示例

value = store.get(key) store.put(key, value) store.delete(key)

總結(jié)

  Voldemort是分布式緩存系統(tǒng),因此可以應(yīng)用在中大型的軟件項目中,性能方面也都還不錯。

轉(zhuǎn)載于:https://my.oschina.net/liuyuantao/blog/704588

總結(jié)

以上是生活随笔為你收集整理的5个强大的Java分布式缓存框架推荐的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。