温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java缓存ehcache怎么使用

发布时间:2021-05-10 14:17:28 来源:亿速云 阅读:145 作者:小新 栏目:开发技术

这篇文章给大家分享的是有关Java缓存ehcache怎么使用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

Java的优点是什么

1. 简单,只需理解基本的概念,就可以编写适合于各种情况的应用程序;2. 面向对象;3. 分布性,Java是面向网络的语言;4. 鲁棒性,java提供自动垃圾收集来进行内存管理,防止程序员在管理内存时容易产生的错误。;5. 安全性,用于网络、分布环境下的Java必须防止病毒的入侵。6. 体系结构中立,只要安装了Java运行时系统,就可在任意处理器上运行。7. 可移植性,Java可以方便地移植到网络上的不同机器。8.解释执行,Java解释器直接对Java字节码进行解释执行。

一、pom.xml

<dependency>
		    <groupId>net.sf.ehcache</groupId>
		    <artifactId>ehcache</artifactId>
		    <version>2.10.4</version>
</dependency>

二、编写ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
 
	<cacheManagerPeerProviderFactory
		class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
		properties="peerDiscovery=automatic, multicastGroupAddress=198.1.1.1,
         multicastGroupPort=10001,
         timeToLive=1" />
 
	<cacheManagerPeerListenerFactory
		class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
		properties="port=10001,socketTimeoutMillis=60000" />
 
	<!-- 磁盘缓存位置 -->
	<diskStore path="java.io.tmpdir/anywhere" />
 
	
	<cache name="oneCache" maxElementsInMemory="1500" eternal="false"
		timeToIdleSeconds="900" timeToLiveSeconds="900" overflowToDisk="false"
		memoryStoreEvictionPolicy="LRU">
		<cacheEventListenerFactory
			class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" properties="replicateRemovals=false"/>
		<bootstrapCacheLoaderFactory
			class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" />
	</cache>
	
</ehcache>

三、参数简介

maxElementsInMemory缓存中允许创建的最大对象数
eternal缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期。
timeToIdleSeconds缓存数据空闲的最大时间,也就是说如果有一个缓存有多久没有被访问就会被销毁,
如果该值是 0 就意味着元素可以停顿无穷长的时间。
timeToLiveSeconds缓存数据存活的时间,缓存对象最大的的存活时间,超过这个时间就会被销毁,
这只能在元素不是永久驻留时有效,如果该值是0就意味着元素可以停顿无穷长的时间。
overflowToDisk内存不足时,是否启用磁盘缓存。
memoryStoreEvictionPolicy缓存满了之后的淘汰算法。
peerDiscovery方式:atutomatic 为自动 ;manual 手动
mulicastGroupAddress广播组地址:192.1.1.1
mulicastGroupPort广播组端口:10001;
timeToLive是指搜索范围:0是同一台服务器,1是同一个子网,32是指同一站点,64是指同一块地域,128是同一块大陆;
hostName主机名或者ip,用来接受或者发送信息的接口

四、Ehcache的缓存数据淘汰策略

FIFO:先进先出

LFU:最少被使用,缓存的元素有一个hit属性,hit值最小的将会被清出缓存。

LRU:最近最少使用,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存

五、编写spring-ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 
  <description>ehcache</description>
  <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
    <property name="cacheManager" ref="ehcache"/>
  </bean>
  <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation" value="classpath:/ehcache.xml"/>
  </bean>
</beans>

六、与Spring整合,导入到spring配置文件

<import resource="classpath:/spring-ehcache.xml"/>

七、Java Source code

使用类导入:
    @Resource
    private org.springframework.cache.ehcacheEhCacheCacheManager cacheManager;

从获取cache
    Cache cache = cacheManager.getCache(“oneCache”);
存入cache
    cache.put(“key”, “value”);
从cache中获取
    ValueWrapper val = cache.get(“key”);
    String tempVal = (String)val.get();

感谢各位的阅读!关于“Java缓存ehcache怎么使用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI