Java缓存ehcache的使用步骤

网友投稿 344 2022-10-25


Java缓存ehcache的使用步骤

一、pom.xml

net.sf.ehcache

ehcache

2.10.4

二、编写ehcache.xml

xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">

class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"

properties="peerDiscovery=automatic, multicastGroupAddress=198.1.1.1,

multicastGroupPort=10001,

timeToLive=1" />

class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"

properties="port=10001,socketTimeoutMillis=60000" />

timeToIdleSeconds="900" timeToLiveSeconds="900" overflowToDisk="false"

memoryStoreEvictionPolicy="LRU">

class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" properties="replicateRemovals=false"/>

class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" />

xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">

class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"

properties="peerDiscovery=automatic, multicastGroupAddress=198.1.1.1,

multicastGroupPort=10001,

timeToLive=1" />

class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"

properties="port=10001,socketTimeoutMillis=60000" />

timeToIdleSeconds="900" timeToLiveSeconds="900" overflowToDisk="false"

memoryStoreEvictionPolicy="LRU">

class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" properties="replicateRemovals=false"/>

class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" />

class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"

properties="peerDiscovery=automatic, multicastGroupAddress=198.1.1.1,

multicastGroupPort=10001,

timeToLive=1" />

class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"

properties="port=10001,socketTimeoutMillis=60000" />

timeToIdleSeconds="900" timeToLiveSeconds="900" overflowToDisk="false"

memoryStoreEvictionPolicy="LRU">

class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" properties="replicateRemovals=false"/>

class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" />

class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"

properties="port=10001,socketTimeoutMillis=60000" />

timeToIdleSeconds="900" timeToLiveSeconds="900" overflowToDisk="false"

memoryStoreEvictionPolicy="LRU">

class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" properties="replicateRemovals=false"/>

class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" />

timeToIdleSeconds="900" timeToLiveSeconds="900" overflowToDisk="false"

memoryStoreEvictionPolicy="LRU">

class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" properties="replicateRemovals=false"/>

class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" />

class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" properties="replicateRemovals=false"/>

class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" />

class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" />

三、参数简介

maxElementsInMemory

缓存中允许创建的最大对象数

eternal

缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期。

timeToIdleSeconds

缓存数据空闲的最大时间,也就是说如果有一个缓存有多久没有被访问就会被销毁,

如果该值是 0 就意味着元素可以停顿无穷长的时间。

timeToLiveSeconds

缓存数据存活的时间,缓存对象最大的的存活时间,超过这个时间就会被销毁,

这只能在元素不是永久驻留时有效,如果该值是0就意味着元素可以停顿无穷长的时间。

DGQfF

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

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd">

ehcache

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd">

ehcache

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

七、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();


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:eclipse 更换 JDK 版本后报错
下一篇:HTML中的超链接
相关文章

 发表评论

评论列表