数据缓存Cache
目录
1.Spring缓存支持
Spring定义了org.springframework.cache.CacheManager和org.springframework.cache.Cache接口用来统一不同的缓存的技术,其中CacheManager是Spring提供的各种缓存技术的抽象接口,Cache接口包含缓存的各种操作(增加、删除、获取缓存,我们一般不会直接与此接口打交道)
1.1 Spring支持的CacheManager
针对不同的缓存技术,需要实现不同的CacheManager
1.2 声明式缓存注解
@Cacheable 表明Spring在调用方法之前,首先应该在缓存中查找方法的返回值。如果这个值能够找到,就会返回缓存的值。否则的话,这个方法就会被调用,返回值会放到缓存之中
@CachePut 表明Spring应该将方法的返回值放到缓存中。在方法的调用前并不会检查缓存,方法始终都会被调用
@CacheEvict 将一条或多条数据从缓存中删除
@Caching 组合多个注解策略在一个方法上
具体的属性可以看源码
2.Spring Boot的支持
在Spring Boot环境中,只需要导入相关缓存技术的依赖包即可,再在配置文件中加上@EnableCaching注解开启缓存支持。
在配置文件中可以用来指定缓存的类型:
spring.cache.type=ehcache
其中可以设置的使用的自动配置的缓存,可选的缓存技术可以参考type里面的选项(来自spring-boot 1.5.14版本):
/*
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.cache;
/**
* Supported cache types (defined in order of precedence).
*
* @author Stephane Nicoll
* @author Phillip Webb
* @author Eddú Meléndez
* @since 1.3.0
*/
public enum CacheType {
/**
* Generic caching using 'Cache' beans from the context.
*/
GENERIC,
/**
* JCache (JSR-107) backed caching.
*/
JCACHE,
/**
* EhCache backed caching.
*/
EHCACHE,
/**
* Hazelcast backed caching.
*/
HAZELCAST,
/**
* Infinispan backed caching.
*/
INFINISPAN,
/**
* Couchbase backed caching.
*/
COUCHBASE,
/**
* Redis backed caching.
*/
REDIS,
/**
* Caffeine backed caching.
*/
CAFFEINE,
/**
* Guava backed caching.
*/
@Deprecated
GUAVA,
/**
* Simple in-memory caching.
*/
SIMPLE,
/**
* No caching.
*/
NONE;
}
实例:
第一种:按照springboot默认的缓存
新建springboot项目,依赖cache,jpa,web
实体类:
package com.just.springbootcache.domain;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.io.Serializable;
@Entity
public class Person implements Serializable {
@Id
@GeneratedValue
private Long id;
private String name;
private Integer age;
private String address;
public Person(){
super();
}
public Person(Long id, String name, Integer age, String address) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
/**省略setter、getter**/
}
实体类repository
package com.just.springbootcache.dao;
import com.just.springbootcache.domain.Person;
import org.springframework.data.jpa.repository.JpaRepository;
public interface PersonRepository extends JpaRepository<Person,Long> {
}
service实现类(service接口就省略了,直接看实现类)
package com.just.springbootcache.service;
import com.just.springbootcache.dao.PersonRepository;
import com.just.springbootcache.domain.Person;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* spring boot cache默认以ConcurrentMapCacheManager作为缓存技术
* 切换其他缓存技术只需要加上其他缓存技术的依赖以及配置
*/
//@CacheConfig(cacheNames = "people") //公用的缓存设置,本示例可以不用,因为下面的方法都定义了value,缓存名
//在用ehcache时要定义好缓存名,每个缓存可以根据业务情况配置自己的缓存参数,否则用的是默认配置
/**
* 定制节点路径people,默认节点路径是persons
*/
@Transactional
@Service
public class PersonServiceImpl