ehcache缓存的使用

来源:互联网 发布:对p2p网络借贷的看法 编辑:程序博客网 时间:2024/04/26 17:04

目录

ehcache是什么

EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。
Ehcache是一种广泛使用的开源Java分布式缓存。主要面向通用缓存,Java EE和轻量级容器。它具有内存和磁盘存储,缓存加载器,缓存扩展,缓存异常处理程序,一个gzip缓存servlet过滤器,支持REST和SOAP api等特点。

ehcache简单使用


1:使用缓存,肯定先要导入缓存使用的包,这里用maven的方式导包
<!-- 缓存ehcache需要的依赖包 --><dependency><groupId>net.sf.ehcache</groupId><artifactId>ehcache</artifactId><version>2.7.4</version></dependency><!-- 缓存ehcache需要的依赖包 -->

2:使用缓存需要一个配置组的配置文件,这边我把loginUser当成一个组,还有一个testStudent对象当成一个组,这个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">  <!--Default Cache configuration. These will applied to caches programmatically created through        the CacheManager.        The following attributes are required:        maxElementsInMemory   - Sets the maximum number of objects that will be created in memory        eternal               - Sets whether elements are eternal. If eternal,  timeouts are ignored and the                                         element is never expired.        overflowToDisk                 - Sets whether elements can overflow to disk when the in-memory cache                                         has reached the maxInMemory limit.        The following attributes are optional:        timeToIdleSeconds         - Sets the time to idle for an element before it expires.                                  i.e. The maximum amount of time between accesses before an element expires                                         Is only used if the element is not eternal.                                         Optional attribute. A value of 0 means that an Element can idle for infinity.                                         The default value is 0.        timeToLiveSeconds            - Sets the time to live for an element before it expires.                                    i.e. The maximum time between creation time and when an element expires.                                         Is only used if the element is not eternal.                              Optional attribute. A value of 0 means that and Element can live for infinity.                                         The default value is 0.        diskPersistent            - Whether the disk store persists between restarts of the Virtual Machine.                                         The default value is false.        diskExpiryThreadIntervalSeconds- The number of seconds between runs of the disk expiry thread. The default value                                         is 120 seconds.                                                                                  s        <cache name="CreditQueryAccount" maxElementsInMemory="100000" eternal="false" timeToLiveSeconds="600" overflowToDisk="false" />        -->    <cache name="loginUser" maxElementsInMemory="100000" eternal="false" timeToLiveSeconds="600" overflowToDisk="false" />    <cache name="testStudent" maxElementsInMemory="100000" eternal="false" timeToLiveSeconds="600" overflowToDisk="false" />   </ehcache>

3:写一个缓存管理类CacheCenter.java类,这个用来作为一个工具类使用,当要存入数据的时候直接cache.put(key,value,group),当要取出缓存中的数据的时候直接cache.read(key,group)即可。废话不说,下面是这个类的实现。

package com.yiyong.mavenspring.demo.helper;/** * 缓存帮助类 */import net.sf.ehcache.Cache;import net.sf.ehcache.CacheManager;import net.sf.ehcache.Element;import org.springframework.stereotype.Component;@Componentpublic class CacheCenter {//private CacheManager cacheManager = CacheManager.create("/usr/local/tomcat/money-new-tomcat/webapps/bank-progress/WEB-INF/classes/com/kingdee/common/ehcache.xml");private CacheManager cacheManager = CacheManager.create(this.getClass().getResource("ehcache.xml"));public void put(String key, Object value, String group) {Element element = new Element(key, value);Cache cache = cacheManager.getCache(group);cache.put(element);}public Object read(String key, String group) {Cache cache = cacheManager.getCache(group);System.out.println(cache);Element element = cache.get(key);return null == element ? null : element.getObjectValue();}public boolean remove(String key, String group) {Cache cache = cacheManager.getCache(group);return cache.remove(key);}public CacheManager getCacheManager() {return cacheManager;}public Cache read(String group) {return cacheManager.getCache(group);}}

4:接下去就是调用的controller或者其他地方进行调用。下面是我在controller中的调用

@Controllerpublic class LoginController  {        //这个地方一定要用springmvc注解的方式加载这个类,如果没有使用springmvc就直接new一个对象。    //找不到的时候到springmvc-servlet.xml的配置文件中加一个扫描固件的配置。    @Autowired    private CacheCenter cache;    @RequestMapping(value="/login")    public String login(HttpServletRequest request,@RequestBody() LoginUser loginUser){cache.put("loginUserUserName", userName, "loginUser");cache.put("loginUserPassword", loginUser.password, "loginUser");TestStudent testStudent = new TestStudent("张三","213");cache.put("testStudent", testStudent, "testStudent");TestStudent test = (TestStudent) cache.read("testStudent", "testStudent");//read出来是一个object类型return "login";    }}

注:这边你自己要创建所必须的实体类才能执行程序,导入必要的包,希望能够帮助到你们!

3 0
原创粉丝点击