Ehcache应用(一)--- 基本使用

引入的依赖:

		<dependency>
			<groupId>org.ehcache</groupId>
			<artifactId>ehcache</artifactId>
			<version>3.5.2</version>
		</dependency>

使用java配置方式:

	/**
	 * 使用java配置启动ehcache
	 */
	private static void cacheBaseJavaConfig() {
		// 这一步只是实例化了CacheManager,还需要进行初始化
		CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
				// 实例化CacheManager时,可以指定cache
				.withCache("myCache1", CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, ResourcePoolsBuilder.heap(10)))
				.build();
		// CacheManager初始化,也可以在上面的build方法中传入参数true,来达到初始化的目的
		cacheManager.init();

		Cache<Long, String> myCache1 = cacheManager.getCache("myCache1", Long.class, String.class);
		// ehcache也可以使用这种方式构造一个cache,cache是不可以重名的
		Cache<String, String> myCache2 = cacheManager.createCache("myCache2", CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class, ResourcePoolsBuilder.heap(10)));
		myCache1.put(0L, "tornado");
		myCache2.put("name","hurricane");
		System.out.println(myCache1.get(0L));
		System.out.println(myCache2.get("name"));
		// 可以用这种方式移除一个已经存在的cache
		cacheManager.removeCache("myCache1");
		// 关闭所有的cache
		cacheManager.close();		
	}

使用xml配置方式:

ehcache.xml文件为:

<config xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
	xmlns='http://www.ehcache.org/v3'
	xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core.xsd">
	
	<cache alias="myCache1">
		<key-type>java.lang.String</key-type>
		<value-type>java.lang.String</value-type>
		<!-- 缓存过期策略配置,只能配置tti、ttl、none、class之中的一种 -->
		<expiry>
			<!-- 空闲指定时间回收 -->
			<!-- <tti unit="seconds">1</tti> -->
			<!-- 存在指定时间回收 -->
			<!-- <ttl unit="seconds">1</ttl> -->
			<none/>
		</expiry>
		<resources>
			<!-- 堆内存使用配置 -->
			<!-- 堆内存配置的空间不能大于堆外内存的配置空间 -->
			<heap unit="entries">100</heap>
			<!-- 1、堆外内存使用限制配置 -->
			<!-- 2、使用的空间达到此处配置后,进行回收 -->
			<!-- 3、最小配置为1M,超出指定空间后,无法正常保存(获取的结果为null,见2) -->
			<offheap unit="MB">1</offheap>
		</resources>
	</cache>
	
	<cache-template name="myTemplate">
		<key-type>java.lang.Long</key-type>
		<value-type>java.lang.String</value-type>
		<heap unit="entries">200</heap>
	</cache-template>
	<!-- 使用模板的情况下,模板的属性是可以进行覆盖的 -->
	<cache alias="myCache2" uses-template="myTemplate">
		<key-type>java.lang.String</key-type>
	</cache>

</config>

对应的java代码为:

	/**
	 * 使用xml配置启动ehcache
	 */
	private static void cacheBaseXmlConfig() {
		URL myUrl = App.class.getClass().getResource("/ehcache.xml"); 
		Configuration xmlConfig = new XmlConfiguration(myUrl); 
		CacheManager cacheManager = CacheManagerBuilder.newCacheManager(xmlConfig);
		cacheManager.init();
		
		Cache<String, String> myCache1 = cacheManager.getCache("myCache2", String.class, String.class);
		
		myCache1.put("name", "hurricane");
		System.out.println(myCache1.get("name"));
		
		cacheManager.close();		
	}

关于ehcache存储空间,过期策略配置的测试:

	/**
	 * 测试ehcache配置项:1.存储空间、2.缓存过期策略 的作用
	 */
	private static void cacheConfigTest() throws InterruptedException {
		URL myUrl = App.class.getClass().getResource("/ehcache.xml"); 
		Configuration xmlConfig = new XmlConfiguration(myUrl); 
		CacheManager cacheManager = CacheManagerBuilder.newCacheManager(xmlConfig);
		cacheManager.init();
		
		Cache<String, String> myCache1 = cacheManager.getCache("myCache1", String.class, String.class);
		String value = "测试字符串";
		//测试配置空间不够的情况,构造大于指定空间的字符串进行储存,结果:获取到的值为null
//		for (;value.getBytes().length<1024*1024;) {
//			value += value;
//		}
		myCache1.put("attr", value);
//		System.out.println(myCache1.get("attr"));
		
		//测试缓存过期策略ttl(time to live),tti(time to idle)
		//过期时间分别设置为1s,通过下面的11次打印中,ttl情况下10次有值;tti情况下1次有值
		for (int i = 0; i < 10; i++) {
			Thread.sleep(500);
			String string = myCache1.get("attr");
			System.out.println(string);
		}
		Thread.sleep(1000);
		System.out.println(myCache1.get("attr"));
		
		cacheManager.close();
	}

参考:

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

https://blog.csdn.net/Gentlemike/article/details/80403967

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值