您的位置:首页 > 其它

Ehcache3.0.0的使用

2016-08-08 14:37 155 查看

ehcache使用

版本3.0.0

介绍

1.概述

http://www.ehcache.org/about/

Ehcache is an open source, standards-based cache that boosts performance, offloads your database, and simplifies scalability. It’s the most widely-used Java-based cache because it’s robust, proven, full-featured, and integrates with other popular libraries and frameworks. Ehcache scales from in-process caching, all the way to mixed in-process/out-of-process deployments with terabyte-sized caches.

翻译就是:Ehcache是一个开源,标准的cache来提高性能,卸载数据库并简化扩展性。是被最广泛使用的基于java的cache,由于它的健壮性,久经考验,功能全面以及能和其他流行的库或者框架融合。Ehcache一直从进程内扩展刀混合进程内/进程外部署的TB级数据的cache。

2.特性

http://www.ehcache.org/about/features.html

主要的特性有:

1. 快速

2. 简单

3. 多种缓存策略

4. 缓存数据有两级:内存和磁盘,因此无需担心容量问题

5. 缓存数据会在虚拟机重启的过程中写入磁盘

6. 可以通过RMI、可插入API等方式进行分布式缓存

7. 具有缓存和缓存管理器的侦听接口

8. 支持多缓存管理器实例,以及一个实例的多个缓存区域

9. 提供Hibernate的缓存实现

使用

http://www.ehcache.org/documentation/3.0/getting-started.html

从官网给出的描述可以看到如果基于程序配置和XML格式配置来创建一个cache。

1.通过程序配置

IDE环境:intellij14 + maven

maven依赖:

<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.0.0.m4</version>
</dependency>


一个简单的示例:

CacheManager cacheManager
= CacheManagerBuilder.newCacheManagerBuilder()
.withCache("preConfigured",     CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, ResourcePoolsBuilder.heap(10)))
.build();
cacheManager.init();

Cache<Long, String> preConfigured =
cacheManager.getCache("preConfigured", Long.class, String.class);
Cache<Long, String> myCache = cacheManager.createCache("myCache",       acheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, ResourcePoolsBuilder.heap(10)).build());

myCache.put(1L, "da one!");
String value = myCache.get(1L);

cacheManager.removeCache("preConfigured");

cacheManager.close();


我们也可以封装成一个泛型来创建各种k,v的cache:

示例如下:

/**
* cache的配置,指定名字,容量和ttl
*/
public class CacheConfig {
private String cacheName;
private int entrySize;
private int timeToLiveSecond;

public CacheConfig(String cacheName, int entrySize, int timeToLive) {
this.cacheName = cacheName;
this.entrySize = entrySize;
this.timeToLiveSecond = timeToLive;
}

public String getC
4000
acheName() { return this.cacheName; }
public int getEntrySize() { return this.entrySize; }
public int getTimeToLiveSecond() { return this.timeToLiveSecond; }
}

/**
*
* @param cacheConfig:cache的配置,如上
* @param kClass
* @param vClass
* @param <K>
* @param <V>
* @return
*/
public static <K, V> Cache<K, V> createCache(CacheConfig cacheConfig, Class<K> kClass, Class<V> vClass) {
try {
String cacheName = cacheConfig.getCacheName();
int entrySize = cacheConfig.getEntrySize();
int ttl = cacheConfig.getTimeToLiveSecond();

Expiry ttlExpiry = Expirations.timeToLiveExpiration(new Duration(ttl, TimeUnit.SECONDS));
CacheManager manager = CacheManagerBuilder.newCacheManagerBuilder()
.withCache(cacheName,
CacheConfigurationBuilder.newCacheConfigurationBuilder()
.withExpiry(ttlExpiry)
.usingEvictionPrioritizer(Eviction.Prioritizer.LRU)
.withResourcePools(ResourcePoolsBuilder.newResourcePoolsBuilder()
.with(ResourceType.Core.HEAP, entrySize, EntryUnit.ENTRIES, false))
.buildConfig(kClass, vClass))
.build(true);
return manager.getCache(cacheName, kClass, vClass);
} catch (Exception e) {
return null;
}
}


ehcachejavadoc : http://www.ehcache.org/apidocs/3.0.0.m4/index.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: