Spring对缓存的支持,org.springframework.cache.CacheManager是Spring提供的各种缓存技术抽象接口,org.springframework.cache.Cache接口包含缓存的各种操作(增删查)
1、spring 定义的CacheManager实现
CacheManager | 描述 |
---|---|
SimpleCacheManager | 使用简单的Collection来存储缓存,主要用来测试用途 |
ConcurrentMapCacheManager | 使用ConcurrentMap来存储缓存 |
NoOpCacheManager | 仅测试用途,不会实际存储缓存 |
EhCacheCacheManager | 使用EhCache作为缓存技术 |
GuavaCacheManager | 使用google Guava 的GuavaCache作为缓存技术 |
HazelcastCacheManager | 使用Hazelcast作为缓存技术 |
JCacheCacheManager | 使用JCache(JSR-107)标准的实现作为缓存技术,如Apache Commons JCS |
RedisCacheManager | 使用Redis作为缓存技术 |
2、声明式缓存注解
注解 | 解释 |
---|---|
@Cacheable | 在方法执行前Spring先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,调用方法并将方法返回值放进缓存 |
@CachePut | 无论怎样,都会将方法的返回值放到缓存中,@CachePut属性与@Cacheable保持一致 |
@CacheEvict | 将一条或多条数据从缓存中删除 |
@Caching | 可以通过@Caching注解组合多个注解策略在一个方法上 |
注:缓存结构类似于Map<k1, Map<k2, v>>,注解的value属性对应k1,key属性对应k2
3、SpringBoot对CacheManager的支持
1)自动配置路径:org.springframework.boot.autoconfigure.cache中
2)默认使用SimpleCacheConfiguration
3)配置中使用spring.cache为前缀
4、Demo
public class Result {
private static final int SUCCESS_CODE = 200;
private static final String SUCCESS_MSG = "SUCCESS";
private int code;
private String msg;
private Object data;
public Result(int code, String msg, Object data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public Result(Object data) {
this(SUCCESS_CODE, SUCCESS_MSG, data);
}
public Result() {
this(null);
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class Person {
private Long id;
private String name;
private Integer age;
public Person() {
}
public Person(Long id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Person{");
sb.append("\"id\":")
.append(id);
sb.append(",\"name\":\"")
.append(name).append('\"');
sb.append(",\"age\":")
.append(age);
sb.append('}');
return sb.toString();
}
}
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableCaching
public class SpringCacheConf {
}
import com.zhanghao.demo.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/cache/test")
public class SpringCacheController {
@Autowired
private SpringCacheService cacheService;
@RequestMapping("/add")
public Result testAdd(Long id, String name, Integer age) {
cacheService.addPerson(new Person(id, name, age));
return new Result();
}
@RequestMapping("/get")
public Result testGet(Long id) {
Person person = cacheService.getPerson(new Person(id, null, null));
return new Result(person);
}
@RequestMapping("/delete")
public Result testDelete(Long id) {
cacheService.deletePerson(id);
return new Result();
}
}
public interface SpringCacheService {
Person addPerson(Person person);
Person getPerson(Person person);
void deletePerson(Long id);
}
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class SpringCacheServiceImpl implements SpringCacheService {
@Override
@CachePut(value = "person", key = "#person.id")
public Person addPerson(Person person) {
System.out.println("insert to db " + person);
return person;
}
@Override
@Cacheable(value = "person", key = "#person.id")
public Person getPerson(Person person) {
person.setName("DB中名字");
person.setAge(18);
System.out.println("get from db " + person);
return person;
}
@Override
@CacheEvict(value = "person")
public void deletePerson(Long id) {
System.out.println("delete from db id=" + id);
}
}
注:
1)如果没有指定key,则方法参数作为key保存到缓存中
2)@CachePut和@Cacheable都是将方法的返回值记录到缓存中,前者方法一定会执行且会更新缓存,后者若缓存中有则不执行方法直接从缓存中获取,若缓存中没有则执行方法并将返回值放到缓存中
3)切换缓存媒介:添加相应依赖,SpringBoot会自动配置相应CacheManager实现