Java缓存ehcache的使用步骤

一、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的使用步骤的文章就介绍到这了,更多相关ehcache缓存的使用内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • 详解SpringBoot缓存的实例代码(EhCache 2.x 篇)

    本篇介绍了SpringBoot 缓存(EhCache 2.x 篇),分享给大家,具体如下: SpringBoot 缓存 在 spring Boot中,通过@EnableCaching注解自动化配置合适的缓存管理器(CacheManager),Spring Boot根据下面的顺序去侦测缓存提供者: Generic JCache (JSR-107) EhCache 2.x Hazelcast Infinispan Redis Guava Simple 关于 Spring Boot 的缓存机制: 高速

  • 使用ehcache三步搞定springboot缓存的方法示例

    本次内容主要介绍基于Ehcache 3.0来快速实现Spring Boot应用程序的数据缓存功能.在Spring Boot应用程序中,我们可以通过Spring Caching来快速搞定数据缓存.接下来我们将介绍如何在三步之内搞定Spring Boot缓存. 1. 创建一个Spring Boot工程并添加Maven依赖 你所创建的Spring Boot应用程序的maven依赖文件至少应该是下面的样子: <?xml version="1.0" encoding="UTF-8

  • spring-boot整合ehcache实现缓存机制的方法

    EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. ehcache提供了多种缓存策略,主要分为内存和磁盘两级,所以无需担心容量问题. spring-boot是一个快速的集成框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置. 由于spring-boot无需任何样板化的配置文件,所以spring-boot集成一些其他框架时会有略微的

  • Spring Boot缓存实战 EhCache示例

    Spring boot默认使用的是SimpleCacheConfiguration,即使用ConcurrentMapCacheManager来实现缓存.但是要切换到其他缓存实现也很简单 pom文件 在pom中引入相应的jar包 <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web<

  • Spring+EHcache缓存实例详解

    一.ehcahe的介绍 EhCache 是一个纯Java的进程内缓存框架,具有高速.精干等特点,是Hibernate中默认的CacheProvider.Ehcache是一种广泛使用的开源Java分布式缓存. 主要面向通用缓存,Java EE和轻量级容器. 它具有内存和磁盘存储.缓存载入器,缓存扩展,缓存异常处理程序,一个gzip缓存servlet过滤器.支持REST和SOAP api等特点. 优点: 1. 高速 2. 简单 3. 多种缓存策略 4. 缓存数据有两级:内存和磁盘,因此无需操心容量问

  • 详解Spring MVC 集成EHCache缓存

    废话少说,直接上代码: ehcache.xml 文件 <?xml version="1.0" encoding="UTF-8"?> <ehcache dynamicConfig="false" monitoring="off" updateCheck="false" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  • 详解Spring Boot Oauth2缓存UserDetails到Ehcache

    在Spring中有一个类CachingUserDetailsService实现了UserDetailsService接口,该类使用静态代理模式为UserDetailsService提供缓存功能.该类源码如下: CachingUserDetailsService.java public class CachingUserDetailsService implements UserDetailsService { private UserCache userCache = new NullUserC

  • Spring Boot集成Ehcache缓存解决方式

    本次内容主要介绍基于Ehcache 3.0来快速实现Spring Boot应用程序的数据缓存功能.在Spring Boot应用程序中,我们可以通过Spring Caching来快速搞定数据缓存. 接下来我们将介绍如何在三步之内搞定 Spring Boot 缓存. 1. 创建一个Spring Boot工程 你所创建的Spring Boot应用程序的maven依赖文件至少应该是下面的样子: <?xml version="1.0" encoding="UTF-8"?

  • Java Ehcache缓存框架入门级使用实例

    前言 JAVA缓存实现方案有很多,最基本的自己使用Map去构建缓存,或者使用memcached或Redis,但是上述两种缓存框架都要搭建服务器,而Map自行构建的缓存可能没有很高的使用效率,那么我们可以尝试一下使用Ehcache缓存框架. Ehcache主要基于内存缓存,磁盘缓存为辅的,使用起来方便.下面介绍如何在项目中使用Ehcache 入门使用教程 1.maven引用 <dependency> <groupId>net.sf.ehcache</groupId> &l

  • SpringBoot2 整合Ehcache组件,轻量级缓存管理的原理解析

    本文源码:GitHub·点这里 || GitEE·点这里 一.Ehcache缓存简介 1.基础简介 EhCache是一个纯Java的进程内缓存框架,具有快速.上手简单等特点,是Hibernate中默认的缓存提供方. 2.Hibernate缓存 Hibernate三级缓存机制简介: 一级缓存:基于Session级别分配一块缓存空间,缓存访问的对象信息.Session关闭后会自动清除缓存. 二级缓存:是SessionFactory对象缓存,可以被创建出的多个 Session 对象共享,二级缓存默认是

  • SpringBoot中Shiro缓存使用Redis、Ehcache的方法

    SpringBoot 中配置redis作为session 缓存器. 让shiro引用 本文是建立在你是使用这shiro基础之上的补充内容 第一种:Redis缓存,将数据存储到redis 并且开启session存入redis中. 引入pom <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifac

  • 在Mybatis中使用自定义缓存ehcache的方法

    自定义缓存 - ehcache Ehcache是一种广泛使用的开源Java分布式缓存.主要面向通用缓存,Java EE和轻量级容器 1.导包 <!-- https://mvnrepository.com/artifact/org.mybatis.caches/mybatis-ehcache --> <dependency> <groupId>org.mybatis.caches</groupId> <artifactId>mybatis-ehca

随机推荐