Ehcache是一种广泛使用的开源Java分布式缓存,主要面向通用缓存,Java EE和轻量级容器。本博文使用3.x版本。
maven包含依赖
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.0.0.rc1</version>
</dependency>
程序示例
package club.chuxing.tech.learn.ehcache;
import org.ehcache.Cache;
import org.ehcache.CacheManager;
import org.ehcache.UserManagedCache;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.config.builders.UserManagedCacheBuilder;
import static club.chuxing.tech.learn.utils.PrintUtil.*;
public class TestEhCache {
public static void main(String[] args) {
//Managed Cache
CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder().withCache("preConfigured",
CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class)).build(true);
Cache<Long, String> name = cacheManager.createCache("name",
CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class).build());
name.put(1L, "first one");
print(name.get(1L));
Cache<Long, String> aaa = cacheManager.getCache("preConfigured", Long.class, String.class);
aaa.put(1L, "The First");
print(aaa.get(1L));
cacheManager.removeCache("preConfigured");
cacheManager.close();
//User Managed Cache
UserManagedCache<String, Long> userManagedCache = UserManagedCacheBuilder.newUserManagedCacheBuilder(
String.class, Long.class).build(true);
userManagedCache.put("lee", 1213L);
print(userManagedCache.get("lee"));
userManagedCache.close();
}
}