Spring Boot入门+深入(十)-缓存Redis

目录

一、Spring Boot与缓存

1.JSR107-缓存规范

2.Spring缓存抽象

3.缓存注解

二、缓存使用

一、搭建基本环境

二、快速体验缓存

@Cacheable

@CachePut

@CacheEvict

@Caching

@CacheConfig

三、整合redis作为缓存


一、Spring Boot与缓存

1.JSR107-缓存规范

Java Caching定义了5个核心接口,分别是CachingProvider, CacheManager, Cache, Entry 和 Expiry。

CachingProvider定义了创建、配置、获取、管理和控制多个CacheManager。一个应用可以在运行期访问多个CachingProvider。

CacheManager定义了创建、配置、获取、管理和控制多个唯一命名的Cache,这些Cache 存在于CacheManager的上下文中。一个CacheManager仅被一个CachingProvider所拥有。

Cache是一个类似Map的数据结构并临时存储以Key为索引的值。一个Cache仅被一个 CacheManager所拥有。

Entry是一个存储在Cache中的key-value对。

Expiry 每一个存储在Cache中的条目有一个定义的有效期。一旦超过这个时间,条目为过期的状态。一旦过期,条目将不可访问、更新和删除。缓存有效期可以通过ExpiryPolicy设置。

2.Spring缓存抽象

Spring从3.1开始定义了org.springframework.cache.Cache 和org.springframework.cache.CacheManager接口来统一不同的缓存技术; 并支持使用JCache(JSR-107)注解简化我们开发;

Cache接口为缓存的组件规范定义,包含缓存的各种操作集合;

Cache接口下Spring提供了各种xxxCache的实现;如RedisCache,EhCacheCache , ConcurrentMapCache等;

每次调用需要缓存功能的方法时,Spring会检查检查指定参数的指定的目标方法是否已经被调用过;如果有就直接从缓存中获取方法调用后的结果,如果没有就调用方法并缓存结果后返回给用户。下次调用直接从缓存中获取。

使用Spring缓存抽象时我们需要关注以下两点;

        1、确定方法需要被缓存以及他们的缓存策略

        2、从缓存中读取之前缓存存储的数据

3.缓存注解

Cache缓存接口,定义缓存操作。实现有:RedisCache、EhCacheCache、 ConcurrentMapCache等
CacheManager缓存管理器,管理各种缓存(Cache)组件
@Cacheable主要针对方法配置,能够根据方法的请求参数对其结果进行缓存
@CacheEvict清空缓存
@CachePut保证方法被调用,又希望结果被缓存。
@EnableCaching开启基于注解的缓存
keyGenerator缓存数据时key生成策略
serialize缓存数据时value序列化策略
@Cacheable/@CachePut/@CacheEvict 主要的参数
value缓存的名称,在 spring 配置文件中定义,必须指定 至少一个例如: @Cacheable(value=”mycache”) 或者 @Cacheable(value={”cache1”,”cache2”}
key缓存的 key,可以为空,如果指定要按照 SpEL 表达 式编写,如果不指定,则缺省按照方法的所有参数 进行组合例如: @Cacheable(value=”testcache”,key=”#userName”)
condition缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存/清除缓存,在 调用方法之前之后都能判断例如: @Cacheable(value=”testcache”,condition=”#userNam e.length()>2”)
allEntries (@CacheEvict )是否清空所有缓存内容,缺省为 false,如果指定为 true,则方法调用后将立即清空所有缓存例如: @CachEvict(value=”testcache”,allEntries=true)
beforeInvocation (@CacheEvict)是否在方法执行前就清空,缺省为 false,如果指定 为 true,则在方法还没有执行的时候就清空缓存, 缺省情况下,如果方法执行抛出异常,则不会清空 缓存例如: @CachEvict(value=”testcache”, beforeInvocation=true)
unless (@CachePut) (@Cacheable)用于否决缓存的,不像condition,该表达式只在方 法执行之后判断,此时可以拿到返回值result进行判 断。条件为true不会缓存,fasle才缓存例如: @Cacheable(value=”testcache”,unless=”#result == null”)

二、缓存使用

新建Spring Boot工程引入Cache :spring-boot-starter-cache

pom.xml

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-cache</artifactId>
</dependency>

例子:

一、搭建基本环境

1.导入sql表

CREATE TABLE `department` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `departmentName` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `employee` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `lastName` varchar(255) DEFAULT NULL,
  `email` varchar(255) DEFAULT NULL,
  `gender` int(2) DEFAULT NULL,
  `d_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `employee` VALUES (1, '张无忌', 'zwj@163.com', 1, 1);
INSERT INTO `employee` VALUES (2, '张三丰', 'zsf@163.com', 1, 2);

INSERT INTO `department` VALUES (1, '开发部');
INSERT INTO `department` VALUES (2, '财务部');

2.创建JavaBean

public class Department {
	
	private Integer id;
	private String departmentName;
	
	public Department() {
		super();
	}
	public Department(Integer id, String departmentName) {
		super();
		this.id = id;
		this.departmentName = departmentName;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getDepartmentName() {
		return departmentName;
	}
	public void setDepartmentName(String departmentName) {
		this.departmentName = departmentName;
	}
	@Override
	public String toString() {
		return "Department [id=" + id + ", departmentName=" + departmentName + "]";
	}

}
public class Employee implements Serializable {
	
	private Integer id;
	private String lastName;
	private String email;
	private Integer gender; //性别 1男  0女
	private Integer dId;
	
	public Employee() {
		super();
	}
	
	public Employee(Integer id, String lastName, String email, 
        Integer gender, Integer dId) {
		super();
		this.id = id;
		this.lastName = lastName;
		this.email = email;
		this.gender = gender;
		this.dId = dId;
	}
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public Integer getGender() {
		return gender;
	}
	public void setGender(Integer gender) {
		this.gender = gender;
	}
	public Integer getdId() {
		return dId;
	}
	public void setdId(Integer dId) {
		this.dId = dId;
	}
	@Override
	public String toString() {
		return "Employee [id=" + id + ", lastName=" + lastName 
        + ", email=" + email + ", gender=" + gender + ", dId="
		+ dId + "]";
	}

}

3.整合MyBatis

application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/spring_cache
spring.datasource.username=root
spring.datasource.password=root

# 开启驼峰命名匹配规则
mybatis.configuration.map-underscore-to-camel-case=true

创建mapper

@Mapper
public interface EmployeeMapper {

    @Select("SELECT * FROM employee WHERE id = #{id}")
    public Employee getEmpById(Integer id);

    @Update("UPDATE employee SET lastName=#{lastName},email=#{email},gender=#{gender},d_id=#{dId} WHERE id=#{id}")
    public void updateEmp(Employee employee);

    @Delete("DELETE FROM employee WHERE id=#{id}")
    public void deleteEmpById(Integer id);

    @Insert("INSERT INTO employee(lastName,email,gender,d_id) VALUES(#{lastName},#{email},#{gender},#{dId})")
    public void insertEmployee(Employee employee);

    @Select("SELECT * FROM employee WHERE lastName = #{lastName}")
    Employee getEmpByLastName(String lastName);
}
@Mapper
public interface DepartmentMapper {

    @Select("SELECT * FROM department WHERE id = #{id}")
    Department getDeptById(Integer id);
}

创建Controller

@RestController
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;

    @GetMapping("/emp/{id}")
    public Employee getEmployee(@PathVariable("id") Integer id){
        Employee employee = employeeService.getEmp(id);
        return employee;
    }

    @GetMapping("/emp")
    public Employee update(Employee employee){
        Employee emp = employeeService.updateEmp(employee);
        return emp;
    }

    @GetMapping("/delemp")
    public String deleteEmp(Integer id){
        employeeService.deleteEmp(id);
        return "success";
    }

    @GetMapping("/emp/lastname/{lastName}")
    public Employee getEmpByLastName(@PathVariable("lastName") String lastName){
       return employeeService.getEmpByLastName(lastName);
    }

}
@RestController
public class DeptController {

    @Autowired
    DeptService deptService;

    @GetMapping("/dept/{id}")
    public Department getDept(@PathVariable("id") Integer id){
        return deptService.getDeptById(id);
    }
}

创建Service

@Service
public class EmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;

    public Employee getEmp(Integer id){
        System.out.println("查询"+id+"号员工");
        Employee emp = employeeMapper.getEmpById(id);
        return emp;
    }
}

启动项目:访问:http://localhost:8080/emp/1

返回数据:{"id":1,"lastName":"张无忌","email":"zwj@163.com","gender":1,"dId":1}

---------------------------

二、快速体验缓存

步骤:
1、开启基于注解的缓存 @EnableCaching
2、标注缓存注解即可
        @Cacheable
        @CacheEvict
        @CachePut

默认使用的是ConcurrentMapCacheManager==ConcurrentMapCache;将数据保存在    ConcurrentMap<Object, Object>中

开发中使用缓存中间件;redis、memcached、ehcache;

1.启动类上标注@EnableCaching

@SpringBootApplication
@EnableCaching
public class SpringbootCacheApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootCacheApplication.class, args);
	}
}

2.Service    @Cacheable(cacheNames = {"emp"})

@Service
public class EmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;

    @Cacheable(cacheNames = {"emp"})
    public Employee getEmp(Integer id){
        System.out.println("查询"+id+"号员工");
        Employee emp = employeeMapper.getEmpById(id);
        return emp;
    }
}

启动项目:访问:http://localhost:8080/emp/1

返回数据:{"id":1,"lastName":"张无忌","email":"zwj@163.com","gender":1,"dId":1}

多次访问相同的请求,后台不打印日志,但是依旧会返回数据,数据从缓存中读取。

@Cacheable

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Cacheable {
    //cacheNames/value:指定缓存组件的名字;将方法的返回结果放在哪个缓存中,
    //是数组的方式,可以指定多个缓存;
	@AliasFor("cacheNames")
	String[] value() default {};

	@AliasFor("value")
	String[] cacheNames() default {};

    //key:缓存数据使用的key;可以用它来指定。默认是使用方法参数的值 1-方法的返回值
    //编写SpEL; #id;参数id的值   #a0  #p0  #root.args[0]
	String key() default "";

    //keyGenerator:key的生成器;可以自己指定key的生成器的组件id
    //key/keyGenerator:二选一使用;
	String keyGenerator() default "";

    //cacheManager:指定缓存管理器;或者cacheResolver指定获取解析器
	String cacheManager() default "";

    //cacheResolver指定获取解析器
	String cacheResolver() default "";

    //condition:指定符合条件的情况下才缓存;
    //condition = "#id>0"
    //condition = "#a0>1":第一个参数的值>1的时候才进行缓存
	String condition() default "";

    //unless:否定缓存;当unless指定的条件为true,方法的返回值就不会被缓存;
    //可以获取到结果进行判断,unless = "#result == null"
    //unless = "#a0==2":如果第一个参数的值是2,结果不缓存;
	String unless() default "";

    //sync:是否使用异步模式
	boolean sync() default false;
}

缓存的配置原理:

自动配置类;CacheAutoConfiguration.java

@Import(CacheConfigurationImportSelector.class)
public class CacheAutoConfiguration {

	static final String VALIDATOR_BEAN_NAME = "cacheAutoConfigurationValidator";

缓存配置选择器CacheConfigurationImportSelector.class

static class CacheConfigurationImportSelector implements ImportSelector {

	@Override
	public String[] selectImports(AnnotationMetadata importingClassMetadata) {
		CacheType[] types = CacheType.values();
		String[] imports = new String[types.length];
		for (int i = 0; i < types.length; i++) {
			imports[i] = CacheConfigurations.getConfigurationClass(types[i]);
		}
		return imports;
	}

}

imports结果:

2、缓存的配置类
org.springframework.boot.autoconfigure.cache.GenericCacheConfiguration
org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration
org.springframework.boot.autoconfigure.cache.EhCacheCacheConfiguration
org.springframework.boot.autoconfigure.cache.HazelcastCacheConfiguration
org.springframework.boot.autoconfigure.cache.InfinispanCacheConfiguration
org.springframework.boot.autoconfigure.cache.CouchbaseCacheConfiguration
org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration
org.springframework.boot.autoconfigure.cache.CaffeineCacheConfiguration
org.springframework.boot.autoconfigure.cache.GuavaCacheConfiguration
org.springframework.boot.autoconfigure.cache.SimpleCacheConfiguration【默认】
org.springframework.boot.autoconfigure.cache.NoOpCacheConfiguration

想了解哪一个默认生效 在application.properties中增加 

debug=true

重启项目控制台日志搜索 CacheConfiguration结果如下默认匹配上SimpleCacheConfiguration

SimpleCacheConfiguration matched:

CaffeineCacheConfiguration:
    Did not match:

CouchbaseCacheConfiguration:
    Did not match:

EhCacheCacheConfiguration:
    Did not match:

GenericCacheConfiguration:
    Did not match:

GuavaCacheConfiguration:
    Did not match:

HazelcastCacheConfiguration:
    Did not match:

InfinispanCacheConfiguration:
    Did not match:

JCacheCacheConfiguration:
    Did not match:

NoOpCacheConfiguration:
    Did not match:

RedisCacheConfiguration:
    Did not match:

SimpleCacheConfiguration.java:给容器中注册了一个CacheManager:ConcurrentMapCacheManager可以获取和创建ConcurrentMapCache类型的缓存组件;它的作用将数据保存在ConcurrentMap中;

@Configuration
@ConditionalOnMissingBean(CacheManager.class)
@Conditional(CacheCondition.class)
class SimpleCacheConfiguration {

	private final CacheProperties cacheProperties;

	private final CacheManagerCustomizers customizerInvoker;

	SimpleCacheConfiguration(CacheProperties cacheProperties,
			CacheManagerCustomizers customizerInvoker) {
		this.cacheProperties = cacheProperties;
		this.customizerInvoker = customizerInvoker;
	}

	@Bean
	public ConcurrentMapCacheManager cacheManager() {
		ConcurrentMapCacheManager cacheManager = new ConcurrentMapCacheManager();
		List<String> cacheNames = this.cacheProperties.getCacheNames();
		if (!cacheNames.isEmpty()) {
			cacheManager.setCacheNames(cacheNames);
		}
		return this.customizerInvoker.customize(cacheManager);
	}

}

ConcurrentMapCacheManager 实现CacheManager接口,getCache方法等

public class ConcurrentMapCacheManager implements CacheManager, BeanClassLoaderAware {

---CacheManager
public interface CacheManager {

	/**
	 * Return the cache associated with the given name.
	 * @param name the cache identifier (must not be {@code null})
	 * @return the associated cache, or {@code null} if none found
	 */
	Cache getCache(String name);

	/**
	 * Return a collection of the cache names known by this manager.
	 * @return the names of all caches known by the cache manager
	 */
	Collection<String> getCacheNames();

}

缓存运行流程:@Cacheable为例

1、@Cacheable标注的方法运行之前,先去查询Cache(缓存组件),按照cacheNames指定的名字获取;(CacheManager先获取相应的缓存),第一次获取缓存如果没有Cache组件会自动创建。

ConcurrentMapCacheManager.java中的getCache方法

	@Override
	public Cache getCache(String name) {
		Cache cache = this.cacheMap.get(name);
		if (cache == null && this.dynamic) {
			synchronized (this.cacheMap) {
				cache = this.cacheMap.get(name);
				if (cache == null) {
					cache = createConcurrentMapCache(name);
					this.cacheMap.put(name, cache);
				}
			}
		}
		return cache;
	}

2、去Cache中查找缓存的内容,使用一个key,默认就是方法的参数;
        key是按照某种策略生成的;默认是使用keyGenerator生成的,默认使用SimpleKeyGenerator生成key;
        SimpleKeyGenerator生成key的默认策略;
                如果没有参数;key=new SimpleKey();
                如果有一个参数:key=参数的值
                如果有多个参数:key=new SimpleKey(params);

ConcurrentMapCache.java中的lookup方法

	@Override
	protected Object lookup(Object key) {
		return this.store.get(key);
	}

3、没有查到缓存就调用目标方法;

4、将目标方法返回的结果,放进缓存中

ConcurrentMapCache.java中的put方法

	@Override
	public void put(Object key, Object value) {
		this.store.put(key, toStoreValue(value));
	} 

总结:

@Cacheable标注的方法执行之前先来检查缓存中有没有这个数据,默认按照参数的值作为key去查询缓存,如果没有就运行方法并将结果放入缓存;以后再来调用就可以直接使用缓存中的数据;

核心:

        1、使用CacheManager【ConcurrentMapCacheManager】按照名字得到Cache【ConcurrentMapCache】组件
        2、key使用keyGenerator生成的,默认是SimpleKeyGenerator

继续项目例子

@Cacheable  KeyGenerator

自定义KeyGenerator生成策略

创建MyCacheConfig定义KeyGenerator 返回。

import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.lang.reflect.Method;
import java.util.Arrays;

@Configuration
public class MyCacheConfig {

    @Bean("myKeyGenerator")
    public KeyGenerator keyGenerator(){
        return new KeyGenerator(){

            @Override
            public Object generate(Object target, Method method, Object... params) {
                return method.getName()+"["+ Arrays.asList(params).toString()+"]";
            }
        };
    }
}

在Service中方法上指定KeyGenerator

@Service
public class EmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;

    @Cacheable(cacheNames = {"emp"},keyGenerator = "myKeyGenerator")
    public Employee getEmp(Integer id){
        System.out.println("查询"+id+"号员工");
        Employee emp = employeeMapper.getEmpById(id);
        return emp;
    }
}

debug 启动项目在MyCacheConfig 打断点:访问:http://localhost:8080/emp/1

走到断点处代表按照自定义生成策略进行生成key。

@Cacheable  condition

添加条件condition

condition = "#a0>1":第一个参数的值>1的时候才进行缓存

@Service
public class EmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;

    @Cacheable(cacheNames = {"emp"},condition = "#a0>1")
    public Employee getEmp(Integer id){
        System.out.println("查询"+id+"号员工");
        Employee emp = employeeMapper.getEmpById(id);
        return emp;
    }
}

@Cacheable  unless 

unless = "#a0==2":如果第一个参数的值是2,结果不缓存;

@Service
public class EmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;

    @Cacheable(cacheNames = {"emp"},unless = "#a0==2")
    public Employee getEmp(Integer id){
        System.out.println("查询"+id+"号员工");
        Employee emp = employeeMapper.getEmpById(id);
        return emp;
    }
}

@Cacheable  sync 使用时unless 不支持

@CachePut

@CachePut:既调用方法,又更新缓存数据;同步更新缓存

修改了数据库的某个数据,同时更新缓存;

运行时机:
        1、先调用目标方法
        2、将目标方法的结果缓存起来

测试步骤:
1、查询1号员工;查到的结果会放在缓存中;
2、以后查询还是之前的结果
3、更新1号员工;将方法的返回值也放进缓存了;
4、查询1号员工,应该是更新后的员工?不一定,缓存中是key-value存储,保证key一致

        key = "#employee.id":使用传入的参数的员工id;

        key = "#result.id":使用返回后的id

@Cacheable的key是不能用#result,因为@Cacheable调用时机是在方法运行之前就要获取key,而#result是方法返回值,方法运行后才能得到。

@Service
public class EmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;

    @Cacheable(cacheNames = {"emp"})
    public Employee getEmp(Integer id){
        System.out.println("查询"+id+"号员工");
        Employee emp = employeeMapper.getEmpById(id);
        return emp;
    }

    @CachePut(value = "emp",key = "#result.id")
    public Employee updateEmp(Employee employee){
        System.out.println("updateEmp:"+employee);
        employeeMapper.updateEmp(employee);
        return employee;
    }
}

@CacheEvict

@CacheEvict:缓存清除
key:指定要清除的数据
allEntries = true:指定清除这个缓存中所有的数据
beforeInvocation = false:缓存的清除是否在方法之前执行
默认代表缓存清除操作是在方法执行之后执行;如果出现异常缓存就不会清除
beforeInvocation = true:代表清除缓存操作是在方法运行之前执行,无论方法是否出现异常,缓存都清除

@Service
public class EmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;

    @Cacheable(cacheNames = {"emp"})
    public Employee getEmp(Integer id){
        System.out.println("查询"+id+"号员工");
        Employee emp = employeeMapper.getEmpById(id);
        return emp;
    }

    @CachePut(value = "emp",key = "#result.id")
    public Employee updateEmp(Employee employee){
        System.out.println("updateEmp:"+employee);
        employeeMapper.updateEmp(employee);
        return employee;
    }

    @CacheEvict(value="emp",beforeInvocation = true/*key = "#id",*/)
    public void deleteEmp(Integer id){
        System.out.println("deleteEmp:"+id);
        //employeeMapper.deleteEmpById(id);
    }
}

@Caching

@Caching 定义复杂的缓存规则

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Caching {

	Cacheable[] cacheable() default {};

	CachePut[] put() default {};

	CacheEvict[] evict() default {};

}
@Service
public class EmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;

    @Cacheable(cacheNames = {"emp"})
    public Employee getEmp(Integer id){
        System.out.println("查询"+id+"号员工");
        Employee emp = employeeMapper.getEmpById(id);
        return emp;
    }

    @CachePut(value = "emp",key = "#result.id")
    public Employee updateEmp(Employee employee){
        System.out.println("updateEmp:"+employee);
        employeeMapper.updateEmp(employee);
        return employee;
    }

    @CacheEvict(value="emp",beforeInvocation = true/*key = "#id",*/)
    public void deleteEmp(Integer id){
        System.out.println("deleteEmp:"+id);
        //employeeMapper.deleteEmpById(id);
    }

    @Caching(
         cacheable = {
             @Cacheable(/*value="emp",*/key = "#lastName")
         },
         put = {
             @CachePut(/*value="emp",*/key = "#result.id"),
             @CachePut(/*value="emp",*/key = "#result.email")
         }
    )
    public Employee getEmpByLastName(String lastName){
        return employeeMapper.getEmpByLastName(lastName);
    }
}

@CacheConfig

@CacheConfig抽取缓存的公共配置

@CacheConfig(cacheNames="emp")//下面注解可以不用指定缓存名称了
@Service
public class EmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;

    @Cacheable
    public Employee getEmp(Integer id){
        System.out.println("查询"+id+"号员工");
        Employee emp = employeeMapper.getEmpById(id);
        return emp;
    }

    @CachePut(key = "#result.id")
    public Employee updateEmp(Employee employee){
        System.out.println("updateEmp:"+employee);
        employeeMapper.updateEmp(employee);
        return employee;
    }

    @CacheEvict(beforeInvocation = true)
    public void deleteEmp(Integer id){
        System.out.println("deleteEmp:"+id);
        //employeeMapper.deleteEmpById(id);
    }

    @Caching(
         cacheable = {
             @Cacheable(key = "#lastName")
         },
         put = {
             @CachePut(key = "#result.id"),
             @CachePut(key = "#result.email")
         }
    )
    public Employee getEmpByLastName(String lastName){
        return employeeMapper.getEmpByLastName(lastName);
    }
}

三、整合redis作为缓存

Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件。

1、安装redis:使用docker/windows安装;并安装redis-desktop-manager-X.x.x.xxx.exe客户端软件。

windows上可以把redis放到windows服务中配置成开机即启动的一个服务。

 redis-desktop-manager客户端可以连接redis查看数据

redis:redis中文官方网站

redis命令:Redis命令中心(Redis commands) -- Redis中国用户组(CRUG)

 2、引入redis的starter

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

3、配置redis

application.properties

spring.redis.host=localhost

4、测试

测试类:

Spring Boot入门+深入(一):Spring Boot入门+深入(一)

Spring Boot入门+深入(十一):Spring Boot入门+深入(十一)-消息RabbitMQ

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杀神lwz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值