您的位置:首页 > 编程语言 > Java开发

springboot 使用cache

2018-03-31 21:04 393 查看
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的依赖

<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触发条件,只有满足条件的情况才会加入缓存,默认为空,既表示全部都加入缓存,支持SpELcondition=”#p1.length < 32”
  以上面getInfo为例,存储数据所使用的缓存是simpleCache,存储的值为”hello world”,key为变量name所对应的值。

5.2 @CacheEvict

作用是从缓存中清除数据。也就是删除数据

属性名含义示例
value同上value={“xxxxCache”, “simpleCache”}
key同上,表示要清除的keykey = “#p1”
condition同上,触发条件,只有满足条件的情况才会删除condition=”#p1.length < 32”
allEntriestrue表示清除value中的全部缓存,默认为false
5.3 @CachePut

  使用@Cacheable时,如果缓存中有数据是不再执行方法,直接取缓存了。but如果缓存需要更新,且不干扰方法的执行,就可以使用注解@CachePut,每一次都会执行方法并且把数据更新到缓存中。属性同@Cacheable。

5.4 @CacheConfig

  有时候一个类中可能会有多个缓存操作,而这些缓存操作可能是重复的。这个时候可以使用@CacheConfig 。它是一个类级别的注解,只能放在类上。上面的例子中就表示getInfo,putInfo,clearInfo所操作的缓存就是simpleCache

属性名含义示例
cacheNames所使用的缓存cacheNames = “simpleCache”
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  cache