- 浏览: 535492 次
- 性别:
- 来自: 长沙
文章分类
最新评论
-
wocan23:
镜像是不是就是集群里面的从服务器啊
高并发处理方案 -
chenyi0834:
net.sf.ehcache.Cache ehCache=Ap ...
spring+ehCache简单整合使用示例 -
fsh430623:
是一个获取spring容器bean的工具类
spring+ehCache简单整合使用示例 -
hxc985815621:
ApplicationContextUtils是什么?
spring+ehCache简单整合使用示例 -
peixingchen:
你好,楼主,Action里面的CompanyApplyDTO类 ...
jquery+struts1.3.8的ajax上传文件
下面介绍一下简单使用的配置过程:ehcache.jar及spring相关jar就不说了,加到项目中就是了。
简单的使用真的很简单。但只能做为入门级了。
1.ehcache.xml,可放classpath根目录下,
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true" monitoring="autodetect"
dynamicConfig="true">
< diskStore path="java.io.tmpdir" />
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
diskSpoolBufferSizeMB="30"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
<cache name="DEFAULT_CACHE"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
diskSpoolBufferSizeMB="30"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
</ehcache>
<!--
1.必须要有的属性:
name: cache的名字,用来识别不同的cache,必须惟一。
maxElementsInMemory: 内存管理的缓存元素数量最大限值。
maxElementsOnDisk: 硬盘管理的缓存元素数量最大限值。默认值为0,就是没有限制。
eternal: 设定元素是否持久话。若设为true,则缓存元素不会过期。
overflowToDisk: 设定是否在内存填满的时候把数据转到磁盘上。
2.下面是一些可选属性:
timeToIdleSeconds: 设定元素在过期前空闲状态的时间,只对非持久性缓存对象有效。默认值为0,值为0意味着元素可以闲置至无限长时间。
timeToLiveSeconds: 设定元素从创建到过期的时间。其他与timeToIdleSeconds类似。
diskPersistent: 设定在虚拟机重启时是否进行磁盘存储,默认为false.(我的直觉,对于安全小型应用,宜设为true)。
diskExpiryThreadIntervalSeconds: 访问磁盘线程活动时间。
diskSpoolBufferSizeMB: 存入磁盘时的缓冲区大小,默认30MB,每个缓存都有自己的缓冲区。
memoryStoreEvictionPolicy: 元素逐出缓存规则。共有三种,Recently Used (LRU)最近最少使用,为默认。 First In First Out (FIFO),先进先出。Less Frequently Used(specified as LFU)最少使用
-->
2.第二步,配置applicationContext-ehcache.xml,与spring整合文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
default-autowire="byName" default-lazy-init="false">
<!-- 引用ehCache的配置 -->
<bean id="defaultCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation">
<value>classpath:ehcache.xml</value>
</property>
</bean>
<!-- 定义ehCache的工厂,并设置所使用的Cache name -->
<bean id="ehCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager">
<ref local="defaultCacheManager"/>
</property>
<property name="cacheName">
<value>DEFAULT_CACHE</value>
</property>
</bean>
</beans>
实际上这样就把两者结合起来了。当然集群的话还得另外配置,这里只讲最简单的。
下面使用:
3. 添加数据到缓存:
net.sf.ehcache.Cache ehCache=ApplicationContextUtils.getBean("ehCache");
net.sf.ehcache.Element lgEle=new net.sf.ehcache.Element("loginName", users.getLoginName());
net.sf.ehcache.Element pwdEle=new net.sf.ehcache.Element("password", users.getPassword());
ehCache.put(lgEle);
ehCache.put(pwdEle);
这样使用就可。
当然,在spring管理的bean中,也可:
private Cache ehCache;
@Resource(name="ehCache")
public void setEhCache(Cache ehCache) {
this.ehCache = ehCache;
}
4.使用。
这个其实就不用说了,大家都会了,我相信,能过对应的key值去获取就是了。
如: Element lgEle= ehCache.get("loginName");
需要修改,就先取得再修改,删除就直接删除。
简单的使用真的很简单。但只能做为入门级了。
1.ehcache.xml,可放classpath根目录下,
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true" monitoring="autodetect"
dynamicConfig="true">
< diskStore path="java.io.tmpdir" />
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
diskSpoolBufferSizeMB="30"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
<cache name="DEFAULT_CACHE"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
diskSpoolBufferSizeMB="30"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
</ehcache>
<!--
1.必须要有的属性:
name: cache的名字,用来识别不同的cache,必须惟一。
maxElementsInMemory: 内存管理的缓存元素数量最大限值。
maxElementsOnDisk: 硬盘管理的缓存元素数量最大限值。默认值为0,就是没有限制。
eternal: 设定元素是否持久话。若设为true,则缓存元素不会过期。
overflowToDisk: 设定是否在内存填满的时候把数据转到磁盘上。
2.下面是一些可选属性:
timeToIdleSeconds: 设定元素在过期前空闲状态的时间,只对非持久性缓存对象有效。默认值为0,值为0意味着元素可以闲置至无限长时间。
timeToLiveSeconds: 设定元素从创建到过期的时间。其他与timeToIdleSeconds类似。
diskPersistent: 设定在虚拟机重启时是否进行磁盘存储,默认为false.(我的直觉,对于安全小型应用,宜设为true)。
diskExpiryThreadIntervalSeconds: 访问磁盘线程活动时间。
diskSpoolBufferSizeMB: 存入磁盘时的缓冲区大小,默认30MB,每个缓存都有自己的缓冲区。
memoryStoreEvictionPolicy: 元素逐出缓存规则。共有三种,Recently Used (LRU)最近最少使用,为默认。 First In First Out (FIFO),先进先出。Less Frequently Used(specified as LFU)最少使用
-->
2.第二步,配置applicationContext-ehcache.xml,与spring整合文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
default-autowire="byName" default-lazy-init="false">
<!-- 引用ehCache的配置 -->
<bean id="defaultCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation">
<value>classpath:ehcache.xml</value>
</property>
</bean>
<!-- 定义ehCache的工厂,并设置所使用的Cache name -->
<bean id="ehCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager">
<ref local="defaultCacheManager"/>
</property>
<property name="cacheName">
<value>DEFAULT_CACHE</value>
</property>
</bean>
</beans>
实际上这样就把两者结合起来了。当然集群的话还得另外配置,这里只讲最简单的。
下面使用:
3. 添加数据到缓存:
net.sf.ehcache.Cache ehCache=ApplicationContextUtils.getBean("ehCache");
net.sf.ehcache.Element lgEle=new net.sf.ehcache.Element("loginName", users.getLoginName());
net.sf.ehcache.Element pwdEle=new net.sf.ehcache.Element("password", users.getPassword());
ehCache.put(lgEle);
ehCache.put(pwdEle);
这样使用就可。
当然,在spring管理的bean中,也可:
private Cache ehCache;
@Resource(name="ehCache")
public void setEhCache(Cache ehCache) {
this.ehCache = ehCache;
}
4.使用。
这个其实就不用说了,大家都会了,我相信,能过对应的key值去获取就是了。
如: Element lgEle= ehCache.get("loginName");
需要修改,就先取得再修改,删除就直接删除。
- ehcache.xml.rar (1.4 KB)
- 下载次数: 198
评论
3 楼
chenyi0834
2015-09-03
net.sf.ehcache.Cache ehCache=ApplicationContextUtils.getBean("ehCache");
这里前者是net.sf.ehcache.Cache 后面那个是 spring下面的cache
接收的时候 应该会报错吧
这里前者是net.sf.ehcache.Cache 后面那个是 spring下面的cache
接收的时候 应该会报错吧
2 楼
fsh430623
2015-08-22
是一个获取spring容器bean的工具类
1 楼
hxc985815621
2015-08-17
ApplicationContextUtils是什么?
发表评论
-
最新idea2020注册码永久激活(激活到2100年)
2020-03-23 15:10 3最新idea2020注册码永久激活(激活到2100年) ... -
Java字符串通配符验证算法
2019-10-17 17:08 623因程序中要用到通配符匹配方法 ,网上找了一些,不如人意,就自己 ... -
springjdbc的对象映射与命名参数问题
2016-01-07 09:24 1692springjdbctemplate ... -
mappingDirectoryLocations/mappingJarLocations/mappingResources/mappingLocations的
2015-10-20 17:38 2437于spring对hibernate配置文件hibernate ... -
mysql数据库主从复制配置
2015-08-22 15:58 1779最近研究了一下mysql, ... -
读取request将参数文件
2015-06-16 16:00 852读取时很重要一点是 inputStream.read() ... -
maven常见问题问答(超全面)
2015-04-10 12:13 829maven常见问题问答(超全 ... -
Jackson库 简单使用
2014-10-11 12:03 635Jackson在json返回时,据说效率最高最 ... -
JAVA 日期格式 本地语言环境设置
2014-08-15 11:56 811有时候机器的本地语言导致取得的结果与实际需求显示的内容不同, ... -
通过深克隆将复制数据到另一个对象
2014-07-22 16:53 902想将一个对象的数据复制到另一个对象上面,用BeanUtil ... -
fmt:formatDate 标签关于时间简单使用
2014-07-10 11:10 826jstl标签的功能还是十分强大,这格式标签简单好用,我这里分 ... -
面向对象设计原则简单总结
2014-06-29 11:07 1158面向对象设计原则其实大家谈得太多了,我可以 ... -
正则表达式处理img标签
2014-05-29 11:41 10491文章内容中包含有<img>标签内容,但有时候不 ... -
获取上传文件格式
2014-04-09 15:11 982java准确获取各种上传文件包括图片文件的格式 ... -
springmvc服务端数据验证的自定义验证与扩展使用
2014-02-20 11:37 1973... -
springmvc服务端验证
2014-02-20 11:26 4525... -
java去掉全角半角空格
2013-11-30 17:25 1210有时候需要在后台来处理全解半解空格, ... -
SecurityContext is empty or contents are anonymous处理
2013-11-30 10:14 6229最近用swfupload做上传,在火狐浏览器下上传操作时 出 ... -
文件上传利器SWFUpload使用指南
2013-11-18 14:44 987文件上传利器SWFUpload使用指南 SWFUplo ... -
SpringMVC Controller 综合介绍 一、简介
2013-11-08 17:48 989SpringMVC Controller 综合介绍 一 ...
相关推荐
spring+ehcache示例整合Demo
这是自己整合的Spring 3.0+Struts2+Mybatis 3 + p6spy +ehcache的平台框架,内含一点示例代码,目前ehcache没有使用。直接编译后发布就能用 测试环境基于JDK1.6+Tomcat 6.0. 大家拿到后请根据实际情况修改 ...
springboot 整合 ehcache 简单示例 1、添加依赖 pom.xml 2、添加配置文件ehcache.xml 3、添加注解@EnableCaching @Cacheable 4、插入缓存 5 读取缓存 6 设置缓存过期时间ehcache.xml -->timeToLiveSeconds。
里面本人亲测shiro 和ssm整合 开发工具为myeclipse jdk1.7 tomcat7 ,是一个实战的项目,注释也很清楚。大家可以看看,新手也可以参考一下。shiro的认证 授权 记住我 和ehcache 缓存都结合的很清楚。
Spring Boot 功能特性和组件系列的整合, 详解与使用。下面所有Demo都是基于 Spring Boot 2.0.x, 2.1.x, 2.2.x 版本。 : Blog: Github: Gitee: 数据库 本示例及所有子项目使用的数据库系统是MySQL, 连接的数据库...
SpringBoot2.0 + NoSQL使用教程,项目名称:“ SpringBoot2NoSQL” 项目介绍 一站式SpringBoot for NoSQL开发教程学习手册。 含SpringBoot2.0 +: 雷迪斯 高速缓存 MongoDB 弹性搜索 卡桑德拉 CouchBase 索尔 ...
Spring4CachingAnnotationsExample的示例,通过Java注解的方式整合EhCache框架
直接写业务代码就可以项目优点:1、简单高效2、代码简洁易懂,通过对简单Demo的参考,一般人在1个小时内就可以完成一个增删改的全业务代码实现3、组件增强:本项目是联合组件项目的基础平台,在这个平台上开发的项目...
本开发手册会详细阐述如何在JessMA中整合FreeMarker、Velocity、UrlRewrite、EHCache-Web、Spring、Hibernate和MyBaits等常用框架和组件。 4) 高性能:性能要求是JessMA的硬性指标,从每个模块的设计到每行代码的...
• 使用Spring缓存抽象时我们需要关注以下两点; 1、确定方法需要被缓存 以及他们的缓存策略 2、从缓存中读取之前缓存存储的数据 Cache 缓存接口,定义缓存操作。实现有:RedisCache、EhCacheCache、 ...
└── zheng-demo -- 示例模块(包含一些示例代码等) ├── zheng-demo-rpc-api -- rpc接口包 ├── zheng-demo-rpc-service -- rpc服务提供者 └── zheng-demo-web -- 演示示例[端口:8888] ``` ### 技术...