springboot缓存cache

1.创建和数据表,写好实体类

2.配置好数据源,配置好mybatis,开启驼峰命名法

3.在Springboot启动类上加上dao接口所在包的扫描和开启缓存注解

package com.atguigu;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

/**
 * 一、搭建基本环境
 *  1.执行数据库文件,创建department表和employee表
 *  2.创建javabean(即实体类)封装数据
 *  3.整合mybatis
 *      1.配置数据源
 *      2.使用注解版的mybatis
 *          (1)使用@MapperScan注解扫描Mapper接口所在的包
 * 二、快速体验缓存
 *  1、开启基于注解的缓存:在springboot的启动类上使用@EnableCaching注解
 *  2、标注缓存注解即可
 *      @Cacheable  添加缓存
 *      @CacheEvict 删除缓存
 *      @CachePut   更新缓存
 */
@MapperScan(value = "com.atguigu.mapper")
@SpringBootApplication
@EnableCaching//开启基于注解的缓存
public class SpringbootCache01Application {

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

}

  

4.写好dao接口的方法,这里使用注解的方式

package com.atguigu.mapper;

import com.atguigu.bean.Employee;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

public interface EmployeeMapper {
    @Select("select * from employee where id=#{id}")
    public Employee findEmpById(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 insertEmp(Employee employee);
    @Select("select * from employee where lastName=#{lastName}")
    public Employee findEmpByLastName(String lastName);
}

5.写业务层

package com.atguigu.service;

import com.atguigu.bean.Employee;
import com.atguigu.mapper.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.*;
import org.springframework.stereotype.Service;
@CacheConfig(cacheNames = "emp")//抽取缓存的公共配置,这样下面配置缓存的时候就不用了配置cacheNames了
@Service
public class EmployeeService {
    @Autowired
    EmployeeMapper  employeeMapper;

    /**
     * 将方法的运行结果进行缓存;以后再要相同的数据,直接从缓存中获取,不用调用方法;
     * CacheManager管理多个Cache组件的,对缓存的真正CRUD操作在Cache组件中,每一个缓存组件有自己唯一一个名字;
     *

     *
     * 原理:
     *   1、自动配置类;CacheAutoConfiguration
     *   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
     *   3、哪个配置类默认生效:SimpleCacheConfiguration;
     *
     *   4、给容器中注册了一个CacheManager:ConcurrentMapCacheManager
     *   5、可以获取和创建ConcurrentMapCache类型的缓存组件;他的作用将数据保存在ConcurrentMap中;
     *
     *   运行流程:
     *   @Cacheable:
     *   1、方法运行之前,先去查询Cache(缓存组件),按照cacheNames指定的名字获取;
     *      (CacheManager先获取相应的缓存),第一次获取缓存如果没有Cache组件会自动创建。
     *   2、去Cache中查找缓存的内容,使用一个key,默认就是方法的参数;
     *      key是按照某种策略生成的;默认是使用keyGenerator生成的,默认使用SimpleKeyGenerator生成key;
     *          SimpleKeyGenerator生成key的默认策略;
     *                  如果没有参数;key=new SimpleKey();
     *                  如果有一个参数:key=参数的值
     *                  如果有多个参数:key=new SimpleKey(params);
     *   3、没有查到缓存就调用目标方法;
     *   4、将目标方法返回的结果,放进缓存中
     *
     *   @Cacheable标注的方法执行之前先来检查缓存中有没有这个数据,默认按照参数的值作为key去查询缓存,
     *   如果没有就运行方法并将结果放入缓存;以后再来调用就可以直接使用缓存中的数据;
     *
     *   核心:
     *      1)、使用CacheManager【ConcurrentMapCacheManager】按照名字得到Cache【ConcurrentMapCache】组件
     *      2)、key使用keyGenerator生成的,默认是SimpleKeyGenerator
     *
     *
     *   几个属性:
     *      cacheNames/value:指定缓存组件的名字;将方法的返回结果放在哪个缓存中,是数组的方式,可以指定多个缓存;
     *
     *      key:缓存数据使用的key;可以用它来指定。默认是使用方法参数的值  1-方法的返回值
     *              编写SpEL; #i d;参数id的值   #a0  #p0  #root.args[0]
     *              getEmp[2]
     *
     *      keyGenerator:key的生成器;可以自己指定key的生成器的组件id
     *              key/keyGenerator:二选一使用;
     *
     *
     *      cacheManager:指定缓存管理器;或者cacheResolver指定获取解析器
     *
     *      condition:指定符合条件的情况下才缓存;
     *              ,condition = "#id>0"
     *          condition = "#a0>1":第一个参数的值>1的时候才进行缓存
     *
     *      unless:否定缓存;当unless指定的条件为true,方法的返回值就不会被缓存;可以获取到结果进行判断
     *              unless = "#result == null"
     *              unless = "#a0==2":如果第一个参数的值是2,结果不缓存;
     *      sync:是否使用异步模式
     * @param id
     * @return
     *
     */
    @Cacheable(cacheNames = "emp")
    public Employee findEmpById(Integer id){
        System.err.println("查询=====>"+id+" 号员工");
        return employeeMapper.findEmpById(id);
    }

    /**
     * @CachePut:既调用方法,又更新缓存数据;同步更新缓存
     * 修改了数据库的某个数据,同时更新缓存;
     * 运行时机:
     *  1、先调用目标方法
     *  2、将目标方法的结果缓存起来
     *
     * 测试步骤:
     *  1、查询1号员工;查到的结果会放在缓存中;
     *          key:1  value:lastName:张三
     *  2、以后查询还是之前的结果
     *  3、更新1号员工;【lastName:zhangsan;gender:0】
     *          将方法的返回值也放进缓存了;
     *          key:传入的employee对象  值:返回的employee对象;
     *  4、查询1号员工?
     *      应该是更新后的员工;
     *          key = "#employee.id":使用传入的参数的员工id;
     *          key = "#result.id":使用返回后的id
     *             @Cacheable的key是不能用#result
     *      为什么是没更新前的?【1号员工没有在缓存中更新】
     *          因为保存的缓存的key和更新的缓存的key不一样
     *      所以,在使用更新,清除缓存注解的时候一定要保证和添加缓存的注解的key相同
     */
    @CachePut(value = "emp",key ="#employee.id")
    public Employee updateEmp(Employee employee){
        System.err.println("============================>updateEmp调用了");
        employeeMapper.updateEmp(employee);
        return employee;
    }
    /**
     * @CacheEvict:缓存清除
     *  key:指定要清除的数据
     *  allEntries = true:指定清除这个缓存中所有的数据
     *  beforeInvocation = false:缓存的清除是否在方法之前执行
     *      默认代表缓存清除操作是在方法执行之后执行;如果出现异常缓存就不会清除
     *
     *  beforeInvocation = true:
     *      代表清除缓存操作是在方法运行之前执行,无论方法是否出现异常,缓存都清除
     */
    @CacheEvict(value = "emp",key = "#id")
    public void deleteEmpById(Integer id){
        System.err.println("============================>deleteEmpById调用了");
        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){
        System.err.println("============================>getEmpByLastName调用了");
        Employee empByLastName = employeeMapper.findEmpByLastName(lastName);
        return empByLastName;
    }
}

二、Springboot整合redis作为缓存

流程

* 整合redis作为缓存
 * Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件。
 * 1、安装redis:使用docker;
 * 2、引入redis的starter
 * 3、配置redis
 * 4、测试缓存
 *      原理:CacheManager===Cache 缓存组件来实际给缓存中存取数据
 *      1)、引入redis的starter,容器中保存的是 RedisCacheManager;
 *      2)、RedisCacheManager 帮我们创建 RedisCache 来作为缓存组件;RedisCache通过操作redis缓存数据的
 *      3)、默认保存数据 k-v 都是Object;利用序列化保存;如何保存为json
 *          1、引入了redis的starter,cacheManager变为 RedisCacheManager;
 *          2、默认创建的 RedisCacheManager 操作redis的时候使用的是 RedisTemplate<Object, Object>
 *          3、RedisTemplate<Object, Object> 是 默认使用jdk的序列化机制
 *      4)、自定义CacheManager;
*/

1.引入redis的starter

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

2.配置redis

  • 在application.properties中配置redis的主机地址

spring.redis.host=192.168.144.143

  • 启动类与上面相同

@MapperScan(value = "com.atguigu.mapper")
@SpringBootApplication
@EnableCaching//开启基于注解的缓存
public class SpringbootCache01Application {

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

}

  • 自定义cacheMange使用json序列化方式

  

package com.atguigu.config;

import com.atguigu.bean.Department;
import com.atguigu.bean.Employee;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.net.UnknownHostException;

@Configuration
public class MyRedisConfig {
    @Bean
    public RedisTemplate<Object, Employee> employeeRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
        RedisTemplate<Object, Employee> template = new RedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        Jackson2JsonRedisSerializer<Employee> serializer = new Jackson2JsonRedisSerializer<Employee>(Employee.class);
        template.setDefaultSerializer(serializer);
        return template;
    }

    /**
     * 自定义缓存管理器,向redis数据库中存入json格式的数据
     * @param redisConnectionFactory
     * @return
     */
    @Primary
    @Bean
    public CacheManager empCacheManager(RedisConnectionFactory redisConnectionFactory) {
        RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                //.entryTtl(Duration.ofSeconds(10)) //设置过期时间
                //.disableCachingNullValues()   //禁止缓存null对象
                .computePrefixWith(cacheName -> "yourAppName".concat(":").concat(cacheName).concat(":")) //此处定义了cache key的前缀,避免公司不同项目之间的key名称冲突
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())) //定义了key和value的序列化协议,同时hash key和hash value也被定义
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new Jackson2JsonRedisSerializer<>(Employee.class)));
        return RedisCacheManager.builder(redisConnectionFactory).cacheDefaults(cacheConfiguration).build();
    }
}

  • 缓存注解啥的都不变

@Cacheable  添加缓存
@CacheEvict 删除缓存
@CachePut   更新缓存

  

  

原文地址:https://www.cnblogs.com/wxl123/p/12537433.html