SpringBoot------自定义Redis缓存Cache

发布时间:2022-07-05 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了SpringBoot------自定义Redis缓存Cache脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

SPRingBoot自带Cache存在问题:

1.生成Key过于简单,容易冲突 默认为cachenames + “:” + Key2.无法设置过期时间,默认时间是永久3.配置序列化方式,默认是JDKSerializable,可能造成乱码

 自定义Cache分三个步骤:

1.自定义keygenerator2.自定义cacheManager 设置缓存过期时间3.自定义序列化方式 JackSon

 

1.修改pom.XMl文件

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

 

2.添加Redisconfig.java配置

import com.fasterxML.jackson.annotation.JsonAutodetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWrITer;
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.time.Duration;
import java.util.HashMap;
import java.util.Map;

/**
 * @EnableCaching 开启SpringBoot的Cache缓存
 *
 * */
@Configuration
@EnableCaching
public class RedisConfig {
    @Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory){

        RedisTemplate<String, String> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        return template;
    }

    /**
     * 自定义key值格式
     *
     * */
    @Bean
    public KeyGenerator simpleKeyGenerator(){
        return (o, method, objects) -> {
          StringBuilder builder = new StringBuilder();
          //获取类名称
          builder.append(o.getClass().getSimpleName());
          builder.append(".");
          //获取方法名
          builder.append(method.getName());
          builder.append("[");
          //遍历方法参数
          for (Object obj : objects){
              builder.append(obj.toString());
          }
          builder.append("]");
          return builder.toString();
        };
    }

    @Bean
    public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory){
        return new RedisCacheManager(
                RedisCacheWriter.lockingRedisCacheWriter(redisConnectionFactory),
                //默认Cache缓存的超时配置
                this.getRedisCacheConfigurationWithTTL(30),
                //自定义Cache缓存Key的超时配置
                this.getRedisCacheConfigurationMap()
        );
    }

    /**
     * 配置自定义Cache缓存Key的超时规则
     *
     * */
    public Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap(){
        Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();
        redisCacheConfigurationMap.put("UserInfoCacheList", this.getRedisCacheConfigurationWithTtl(20));
//        redisCacheConfigurationMap.put("UserInfoCacheAnother", this.getRedisCacheConfigurationWithTtl(30));
        return redisCacheConfigurationMap;
    }

    /**
     * 自定义Cache缓存超时规则
     *
     * */
    public RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds){
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);

        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
                //修改Key的命名规则 默认:cacheName + ":"
//                .COMputePrefixWith(cacheName -> cacheName.concat(":"))
//                .serializeKeysWith(RedisSerializationContext.SerializationPair.FromSerializer(new StringRedisSerializer()));

        redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith(
                RedisSerializationContext
                    .SerializationPair
                    .fromSerializer(jackson2JsonRedisSerializer)
        ).entryTtl(Duration.ofSeconds(seconds));
        return redisCacheConfiguration;
    }
}

 

3.添加Service配置

备注:

只有最后一个是自定义的 前面的是springboot自带的方法

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.theng.shopuser.entity.UserInfo;
import com.theng.shopuser.entity.dto.UserInfoDto;
import com.theng.shopuser.mapper.UserInfoMapper;
import com.theng.shopuser.service.UserInfoservice;
import com.baomidou.mybatisplus.extension.service.impl.Serviceimpl;
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.lang.Nullable;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tianheng
 * @since 2020-02-09
 */
@Service
@CacheConfig(cacheNames = "userInfoCache")
public class UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;/**
     * 获取缓存中的UserInfo
     * #p0: 对应方法参数 0表示第一个参数
     *
     * */
    @override
    @Nullable
    @Cacheable(key = "#p0", unless = "#result == null")
    public UserInfo findUserInfoCache(String code){

        return userInfoMapper.findUserInfoByCode(code);
    }

    /**
     * 向缓存中插入UserInfo
     * return: 必须要有返回值 否则无法保存到缓存中
     *
     * */
    @Override
    @CachePut(key = "#p0.userCode")
    public UserInfo insertUserInfoCache(UserInfo userInfo){
        return null;
    }

    /**
     * 向缓存中插入UserInfo
     * return: 必须要有返回值 否则无法保存到缓存中
     *
     * */
    @Override
    @CachePut(key = "#p0.userCode")
    public UserInfo updateUserInfoCache(UserInfo userInfo){
        return null;
    }

    /**
     * 删除cacheNames = "userInfoCache",key值为指定值的缓存
     *
     * */
    @Override
    @CacheEvict(key = "#p0")
    public void deleteUserInfoCache(String userCode){

    }

    /**
     * 清除cacheNames = "userInfoCache"下的所有缓存
     * 如果失败了,则不会清除
     *
     * */
    @Override
    @CacheEvict(allEntries = true)
    public void deleteallUserInfoCache(){

    }

    @Nullable
    @Cacheable(value = "UserInfoCacheList", keyGenerator = "simpleKeyGenerator")
    public UserInfo customUserInfoCache(){

        return null;
    }
}

 

4.修改pom.xml文件

spring:
  redis:
    database: 0
    host: localhost
    port: 6379
    password:

 

5.输出结果

自定义

SpringBoot------自定义Redis缓存Cache

 

脚本宝典总结

以上是脚本宝典为你收集整理的SpringBoot------自定义Redis缓存Cache全部内容,希望文章能够帮你解决SpringBoot------自定义Redis缓存Cache所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。