springboot 使用cache
2018-03-31 21:04
429 查看
springboot 支持的缓存有以下几种,并且项目中如果引入多个时,会按下列排序的优先级选择。也可以强制缓存提供者通过spring.cache.type 属性使用。
Generic
JCache (JSR-107) (EhCache 3, Hazelcast, Infinispan, etc)
EhCache 2.x
Hazelcast
Infinispan
Couchbase
Redis
Caffeine
Guava (deprecated)
Simple
这里以EhCache为例介绍一下springboot如何引入缓存机制。
1.首先添加cache和ehcache的依赖
2.添加配置
3.添加ehcahe.xml文件
4.在入口类上添加@EnableCaching注解,告知springboot启用缓存
5.使用缓存
缓存存储的结构是分cache管理,cache以key-value的键值对方式存储数据。缓存的使用无非也就是增删改查这四种操作。而实现方式是通过注解实现的,常用的注解有
缓存示例:
就着示例讲解一下这四个注解的意思
5.1 @Cacheable
@Cacheable可以标记在一个方法上,也可以标记在一个类上。当标记在一个方法上时表示该方法是支持缓存的,当标记在一个类上时则表示该类所有的方法都是支持缓存的。对于一个支持缓存的方法,Spring会在其被调用后将其返回值缓存起来,以保证下次利用同样的参数来执行该方法时可以直接从缓存中获取结果,而 不需要再次执行该方法。 切记!切记!@Cacheable常用的属性有:
以上面getInfo为例,存储数据所使用的缓存是simpleCache,存储的值为”hello world”,key为变量name所对应的值。
5.2 @CacheEvict
作用是从缓存中清除数据。也就是删除数据
5.3 @CachePut
使用@Cacheable时,如果缓存中有数据是不再执行方法,直接取缓存了。but如果缓存需要更新,且不干扰方法的执行,就可以使用注解@CachePut,每一次都会执行方法并且把数据更新到缓存中。属性同@Cacheable。
5.4 @CacheConfig
有时候一个类中可能会有多个缓存操作,而这些缓存操作可能是重复的。这个时候可以使用@CacheConfig 。它是一个类级别的注解,只能放在类上。上面的例子中就表示getInfo,putInfo,clearInfo所操作的缓存就是simpleCache
Generic
JCache (JSR-107) (EhCache 3, Hazelcast, Infinispan, etc)
EhCache 2.x
Hazelcast
Infinispan
Couchbase
Redis
Caffeine
Guava (deprecated)
Simple
这里以EhCache为例介绍一下springboot如何引入缓存机制。
1.首先添加cache和ehcache的依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> </dependency>
2.添加配置
#强制设置缓存类型为ehcache,如果用redis改为值改为redis就好 spring.cache.type: ehcache #ehcache的配置文件路径 spring.cache.ehcache.config: classpath:config/ehcahe.xml
3.添加ehcahe.xml文件
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd"> <cache name="simpleCache" eternal="false" maxEntriesLocalHeap="0" timeToIdleSeconds="200"></cache> <!-- eternal:true表示对象永不过期,此时会忽略timeToIdleSeconds和timeToLiveSeconds属性,默认为false --> <!-- maxEntriesLocalHeap:堆内存中最大缓存对象数,0没有限制 --> <!-- timeToIdleSeconds: 设定允许对象处于空闲状态的最长时间,以秒为单位。当对象自从最近一次被访问后,如果处于空闲状态的时间超过了timeToIdleSeconds属性值,这个对象就会过期,EHCache将把它从缓存中清空。只有当eternal属性为false,该属性才有效。如果该属性值为0,则表示对象可以无限期地处于空闲状态 --> </ehcache>
4.在入口类上添加@EnableCaching注解,告知springboot启用缓存
5.使用缓存
缓存存储的结构是分cache管理,cache以key-value的键值对方式存储数据。缓存的使用无非也就是增删改查这四种操作。而实现方式是通过注解实现的,常用的注解有
@Cacheable @CacheEvict @CachePut @CacheConfig
缓存示例:
package com.example.spongebob.springbootcache.controller.service; import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Repository; import org.springframework.stereotype.Service; @CacheConfig(cacheNames = "simpleCache") @Service public class SimpleServiceImpl { @Cacheable(key = "p1") public String getInfo(String name){ String info="hello world"; System.out.println(info); return info; } @CachePut(key = "p1") public String putInfo(String name){ String info="hello Cache"; System.out.println(info); return info; } @CacheEvict(key = "p1") public String clearInfo(String name){ String info="hello Evict Cache"; System.out.println(info); return info; } }
就着示例讲解一下这四个注解的意思
5.1 @Cacheable
@Cacheable可以标记在一个方法上,也可以标记在一个类上。当标记在一个方法上时表示该方法是支持缓存的,当标记在一个类上时则表示该类所有的方法都是支持缓存的。对于一个支持缓存的方法,Spring会在其被调用后将其返回值缓存起来,以保证下次利用同样的参数来执行该方法时可以直接从缓存中获取结果,而 不需要再次执行该方法。 切记!切记!@Cacheable常用的属性有:
属性名 | 含义 | 示例 |
---|---|---|
value | 就是ehcache.xml中声明的cache的name。表示要存储在哪个缓存里,可以写多个。必填项。但是可以使用@CacheConfig统一指定 | value={“xxxxCache”, “simpleCache”} |
key | 值所对应的key,该属性支持SpringEL表达式,使用方法参数时我们可以直接使用“#参数名”或者“#p参数index”,例子里面”#p1”,代表的含义就是key为第一个变量(name)所对应的值。该属性不是必须的,没有时cache会以默认的方式帮你生成一个key,而且也支持自定义key的生成规则。 | key = “#p1” |
condition | 触发条件,只有满足条件的情况才会加入缓存,默认为空,既表示全部都加入缓存,支持SpEL | condition=”#p1.length < 32” |
5.2 @CacheEvict
作用是从缓存中清除数据。也就是删除数据
属性名 | 含义 | 示例 |
---|---|---|
value | 同上 | value={“xxxxCache”, “simpleCache”} |
key | 同上,表示要清除的key | key = “#p1” |
condition | 同上,触发条件,只有满足条件的情况才会删除 | condition=”#p1.length < 32” |
allEntries | true表示清除value中的全部缓存,默认为false |
使用@Cacheable时,如果缓存中有数据是不再执行方法,直接取缓存了。but如果缓存需要更新,且不干扰方法的执行,就可以使用注解@CachePut,每一次都会执行方法并且把数据更新到缓存中。属性同@Cacheable。
5.4 @CacheConfig
有时候一个类中可能会有多个缓存操作,而这些缓存操作可能是重复的。这个时候可以使用@CacheConfig 。它是一个类级别的注解,只能放在类上。上面的例子中就表示getInfo,putInfo,clearInfo所操作的缓存就是simpleCache
属性名 | 含义 | 示例 |
---|---|---|
cacheNames | 所使用的缓存 | cacheNames = “simpleCache” |
相关文章推荐
- SpringBoot使用Redis做缓存,@Cacheable、@CachePut、@CacheEvict等注解的使用
- 在 spring boot 中使用spring cache + redis
- Spring-boot中使用Cache
- spring boot 整合 redis,使用@Cacheable,@CacheEvict,@CachePut,jedisPool操作redis数据库
- Spring-Boot (四) cache/ehcache/redis-cache集成使用
- Springboot中使用缓存的示例代码
- SpringBoot中aop的使用
- 使用Eclipse创建spring boot项目
- springboot(五):spring data jpa的使用
- Spring Boot中@ConfigurationProperties的使用
- Spring boot 1: 使用IDEA创建Spring boot项目
- 自定义Spring Boot starter并使用EnableAutoConfiguration装配
- springboot--如何优雅的使用mybatis
- springboot(十九)使用actuator监控应用
- Spring Boot入门二:使用ThymeLeaf+表单验证
- Spring Boot 定时任务的使用
- Spring boot 下使用RabbitMQ报错:406
- spring boot 使用logback日志系统的详细说明
- 使用spring boot +Junit 测试 spring mvc 配置中心项目