ehcache 是一个纯java的进程内缓存框架,具有快速、精干等特点,是hibernate中默认的cacheprovider。
ehcache提供了多种缓存策略,主要分为内存和磁盘两级,所以无需担心容量问题。
spring-boot是一个快速的集成框架,其设计目的是用来简化新spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。
由于spring-boot无需任何样板化的配置文件,所以spring-boot集成一些其他框架时会有略微的不同。
1.spring-boot是一个通过maven管理的jar包的框架,集成ehcache需要的依赖如下
<dependency> <groupid>org.springframework</groupid> <artifactid>spring-context-support</artifactid> </dependency> <dependency> <groupid>net.sf.ehcache</groupid> <artifactid>ehcache</artifactid> <version>2.8.3</version> </dependency>
具体pom.xml文件如下
<?xml version="1.0" encoding="utf-8"?> <project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupid>com.lclc.boot</groupid> <artifactid>boot-cache</artifactid> <version>0.0.1-snapshot</version> <!-- inherit defaults from spring boot --> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>1.1.3.release</version> </parent> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-data-jpa</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-thymeleaf</artifactid> </dependency> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> </dependency> <dependency> <groupid>com.google.guava</groupid> <artifactid>guava</artifactid> <version>17.0</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-context-support</artifactid> </dependency> <dependency> <groupid>net.sf.ehcache</groupid> <artifactid>ehcache</artifactid> <version>2.8.3</version> </dependency> </dependencies> <dependencymanagement> <dependencies> </dependencies> </dependencymanagement> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> <repositories> <repository> <id>spring-snapshots</id> <url>http://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <url>http://repo.spring.io/milestone</url> </repository> </repositories> <pluginrepositories> <pluginrepository> <id>spring-snapshots</id> <url>http://repo.spring.io/snapshot</url> </pluginrepository> <pluginrepository> <id>spring-milestones</id> <url>http://repo.spring.io/milestone</url> </pluginrepository> </pluginrepositories> </project>
2.使用ehcache,我们需要一个ehcache.xml来定义一些cache的属性。
<?xml version="1.0" encoding="utf-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:nonamespaceschemalocation="http://ehcache.org/ehcache.xsd" updatecheck="false"> <diskstore path="java.io.tmpdir/tmp_ehcache" /> <defaultcache eternal="false" maxelementsinmemory="1000" overflowtodisk="false" diskpersistent="false" timetoidleseconds="0" timetoliveseconds="600" memorystoreevictionpolicy="lru" /> <cache name="demo" eternal="false" maxelementsinmemory="100" overflowtodisk="false" diskpersistent="false" timetoidleseconds="0" timetoliveseconds="300" memorystoreevictionpolicy="lru" /> </ehcache>
解释下这个xml文件中的标签。
(1).diskstore: 为缓存路径,ehcache分为内存和磁盘两级,此属性定义磁盘的缓存位置。参数解释如下:
(2).defaultcache:默认缓存策略,当ehcache找不到定义的缓存时,则使用这个缓存策略。只能定义一个。
(3).cache:自定缓存策略,为自定义的缓存策略。参数解释如下:
springboot支持很多种缓存方式:redis、guava、ehcahe、jcache等等。
说明下redis和ehcache的区别:
redis:属于独立的运行程序,需要单独安装后,使用java中的jedis来操纵。因为它是独立,所以如果你写个单元测试程序,放一些数据在redis中,然后又写一个程序去拿数据,那么是可以拿到这个数据的。,
ehcache:与redis明显不同,它与java程序是绑在一起的,java程序活着,它就活着。譬如,写一个独立程序放数据,再写一个独立程序拿数据,那么是拿不到数据的。只能在独立程序中才能拿到数据。
3.将ehcache的管理器暴露给spring的上下文容器,
@configuration // 标注启动了缓存 @enablecaching public class cacheconfiguration { /* * ehcache 主要的管理器 */ @bean(name = "appehcachecachemanager") public ehcachecachemanager ehcachecachemanager(ehcachemanagerfactorybean bean){ return new ehcachecachemanager (bean.getobject ()); } /* * 据shared与否的设置,spring分别通过cachemanager.create()或new cachemanager()方式来创建一个ehcache基地. */ @bean public ehcachemanagerfactorybean ehcachemanagerfactorybean(){ ehcachemanagerfactorybean cachemanagerfactorybean = new ehcachemanagerfactorybean (); cachemanagerfactorybean.setconfiglocation (new classpathresource ("conf/ehcache-app.xml")); cachemanagerfactorybean.setshared (true); return cachemanagerfactorybean; } }
@configuration:为spring-boot注解,主要标注此为配置类,优先扫描。
@bean:向spring容器中加入bean。
至此所有的配置都做好了,通过spring-boot进行集成框架就是这么简单。
4.使用ehcache
使用ehcache主要通过spring的缓存机制,上面我们将spring的缓存机制使用了ehcache进行实现,所以使用方面就完全使用spring缓存机制就行了。
具体牵扯到几个注解:
@cacheable:负责将方法的返回值加入到缓存中,参数3
@cacheevict:负责清除缓存,参数4
参数解释:
不多说,直接上代码:
@service public class cachedemoserviceimpl implements cachedemoservice { /** * 缓存的key */ public static final string thing_all_key = "\"thing_all\""; /** * value属性表示使用哪个缓存策略,缓存策略在ehcache.xml */ public static final string demo_cache_name = "demo"; @cacheevict(value = demo_cache_name,key = thing_all_key) @override public void create(thing thing){ long id = getnextid (); thing.setid (id); data.put (id, thing); } @cacheable(value = demo_cache_name,key = "#thing.getid()+'thing'") @override public thing findbyid(long id){ system.err.println ("没有走缓存!" + id); return data.get (id); } @cacheable(value = demo_cache_name,key = thing_all_key) @override public list<thing> findall(){ return lists.newarraylist (data.values ()); } @override @cacheput(value = demo_cache_name,key = "#thing.getid()+'thing'") @cacheevict(value = demo_cache_name,key = thing_all_key) public thing update(thing thing){ system.out.println (thing); data.put (thing.getid (), thing); return thing; } @cacheevict(value = demo_cache_name) @override public void delete(long id){ data.remove (id); } }
5.只需要通过注解在service层方法上打注解便可以使用缓存,在find**上存入缓存,在delete**,update**上清除缓存。
cache注解详解
@cacheconfig:主要用于配置该类中会用到的一些共用的缓存配置。在这里@cacheconfig(cachenames = "users"):配置了该数据访问对象中返回的内容将存储于名为users的缓存对象中,我们也可以不使用该注解,直接通过@cacheable自己配置缓存集的名字来定义。
@cacheable:配置了findbyname函数的返回值将被加入缓存。同时在查询时,会先从缓存中获取,若不存在才再发起对数据库的访问。该注解主要有下面几个参数:
除了这里用到的两个注解之外,还有下面几个核心注解:
@cacheput:配置于函数上,能够根据参数定义条件来进行缓存,它与@cacheable不同的是,它每次都会真是调用函数,所以主要用于数据新增和修改操作上。它的参数与@cacheable类似,具体功能可参考上面对@cacheable参数的解析
@cacheevict:配置于函数上,通常用在删除方法上,用来从缓存中移除相应数据。除了同@cacheable一样的参数之外,它还有下面两个参数:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问, 点击进行留言回复!!
Mybatis PersistenceException异常:Error building SqlSession The error may exist in SQL Mapper Configur
MyBatis——动态SQL语句——if标签和where标签复合使用
网友评论