Spring与Redis集成的正确方式流程详解

 更新时间:2023年06月20日 14:19:40   作者:右耳菌  
这篇文章主要为大家介绍了Spring与Redis集成的正确方式流程详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

1. 引入RedisTemplate

据以前的情况,我们在Java中使用Redis时一般是使用Jedis来操作的,大致的一段代码如下所示

    @Override
    public User findUserById(Integer id) {
        User user = null;
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String userStr = jedis.get("user_" + id); // 尝试获取数据
            if (userStr != null && !userStr.isEmpty()) { // 如果获取到有效数据,则转换后返回
                user = JSONObject.parseObject(userStr, User.class);
            } else {// 如果没有获取到数据,则查询数据库返回
                user = userMapper.findUserById(id);
                if (user != null) jedis.set("user_" + id, JSONObject.toJSONString(user)); // 设置到redis中
            }
        } finally {
            // 记得关闭Jedis,因为这里使用的是JedisPool,所以这里的关闭并不是直接关闭连接,而是释放,以供其他的业务使用
            if (jedis != null) jedis.close();
        }
        return user;
    }

上边的这样的一段代码其实是有些臃肿的,但是如果我们引入RedisTemplate,其实会简化不少。

  • maven 引入 spring-data-redis
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>3.9.0</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-redis</artifactId>
      <version>2.2.13.RELEASE</version>
    </dependency>
  • 将RedisTemplate 加入Bean容器中,让Spring进行管理。
    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setHostName(host);
        redisStandaloneConfiguration.setPort(port);
        RedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory(redisStandaloneConfiguration);
        return redisConnectionFactory;
    }
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        //设置key值的序列化方式,默认是JDK的形式
        redisTemplate.setKeySerializer(StringRedisSerializer.UTF_8);
        return redisTemplate;
    }
  • 如果使用RedisTemplate的替换的话,会简洁很多。
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public User findUserById(Integer id) {
        Object result = redisTemplate.opsForValue().get("user_" + id);
        if (result != null) return (User) result;
        User user = userMapper.findUserById(id);
        // 设置到redis中
        if (user != null) redisTemplate.opsForValue().set("user_" + id, user);
        return user;
    }

大概看一下关于RedisTemplate的方法

看了以上的内容,可以看到引入了RedisTemplate其实已经很简洁了,但是明显还不够,下面我们将考虑引入 “注解”

2. 引入注解

  • 开启缓存 @EnableCaching
    AppConfig.java
@Configuration
@EnableCaching
public class AppConfig {
  ...
}
  • 引入@Cacheable,表示这个方法将会访问缓存,如果无法命中缓存的话,会将方法返回的值存入redis,假设有注解为 @Cacheable(value="user", key = "#id"),那么生成的key值为 user::{id},即如果id为1 那么生成的 key就是 user::1
    @Override
    @Cacheable(value="user", key = "#id")
    // 这里返回的值会被存放到redis,key-value格式,其中生成的key值(假设id为1): user::1
    public User findUserById(Integer id) {
        User user = userMapper.findUserById(id);
        return user;
    }

但是这样还不够,因为Spring并不清楚缓存的方式是什么,这就涉及到CacheManager

  • 设置CacheManager,在AppConfig中加入以下内容
    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setHostName(host); // 这里是redis的ip
        redisStandaloneConfiguration.setPort(port);// 这里是redis的端口
        // 自适应集群变化
        RedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory(redisStandaloneConfiguration);
        return redisConnectionFactory;
    }
    @Bean
    public RedisCacheConfiguration redisCacheConfiguration() {
        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.string()))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.json()));
        return redisCacheConfiguration;
    }
    @Bean
    public RedisCacheWriter redisCacheWriter(RedisConnectionFactory redisConnectionFactory) {
        RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);
        return redisCacheWriter;
    }
    @Bean
    public CacheManager cacheManager(RedisCacheWriter redisCacheWriter, RedisCacheConfiguration redisCacheConfiguration) {
        CacheManager cacheManager = new RedisCacheManager(redisCacheWriter, redisCacheConfiguration);
        ((RedisCacheManager) cacheManager).isTransactionAware();
        return cacheManager;
    }

3. 扩展 - 自行通过注解和AOP实现缓存

  • 引入AOP相关的包
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>5.3.22</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aspects</artifactId>
      <version>5.3.22</version>
    </dependency>
    <!-- Jackson JSON Processor -->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.9.8</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.8</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.9.8</version>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.28</version>
    </dependency>
  • 创建@CustomCache
package cn.lazyfennec.cache.redis.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomCache {
    /**
     * key的规则,可以使用springEL表达式,可以使用方法执行的一些参数
     */
    String key();
    /**
     *  类似前缀
     * @return
     */
    String value();
}
  • 修改AppConfig
@EnableAspectJAutoProxy // 开启AOP自动代理
public class AppConfig {
    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setHostName(host);
        redisStandaloneConfiguration.setPort(port);
        // 自适应集群变化
        RedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory(redisStandaloneConfiguration);
        return redisConnectionFactory;
    }
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        redisTemplate.setKeySerializer(StringRedisSerializer.UTF_8);
        redisTemplate.setValueSerializer(StringRedisSerializer.UTF_8);
        return redisTemplate;
    }
}
  • 创建 CustomCacheAspect
package cn.lazyfennec.cache.redis.annotation.aop;
import cn.lazyfennec.cache.redis.annotation.CustomCache;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
/**
 * @Author: Neco
 * @Description:
 * @Date: create in 2022/8/24 22:18
 */
@Component
@Aspect
public class CustomCacheAspect {
    @Autowired
    private RedisTemplate redisTemplate;
    @Pointcut("@annotation(cn.lazyfennec.cache.redis.annotation.CustomCache)")
    public void cachePointcut() {
    }
    @Around("cachePointcut()")
    public Object doCache(ProceedingJoinPoint joinPoint) {
        Object obj = null;
        try {
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            Method method = joinPoint.getTarget().getClass().getMethod(signature.getName(), signature.getMethod().getParameterTypes());
            CustomCache customCache = method.getAnnotation(CustomCache.class);
            String cacheKey = customCache.key();
            String cacheValue = customCache.value();
            // 创建解析器
            ExpressionParser parser = new SpelExpressionParser();
            Expression expression = parser.parseExpression(cacheKey);
            EvaluationContext context = new StandardEvaluationContext(); // 参数
            // 添加参数
            Object[] args = joinPoint.getArgs();
            DefaultParameterNameDiscoverer discover = new DefaultParameterNameDiscoverer();
            String[] parameterNames = discover.getParameterNames(method);
            for (int i = 0; i &lt; parameterNames.length; i++) {
                context.setVariable(parameterNames[i], args[i].toString());
            }
            // 解析
            String key = cacheValue + "::" + expression.getValue(context).toString();
            // 1、 判定缓存中是否存在
            obj = redisTemplate.opsForValue().get(key);
            if (obj != null) return obj;
            // 2、不存在则继续行方法
            obj = joinPoint.proceed();
            // 3、 同步存储value到缓存。
            redisTemplate.opsForValue().set(key, obj);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return obj;
    }
}
  • 新建方法 getUserNameById
    @RequestMapping("/custom/name/{id}")
    @ResponseBody
    public String getUserNameById(@PathVariable Integer id) {
        return userService.getUserNameById(id);
    }
  • 实际实现方法 getUserNameById,使用方式
    @Override
    @CustomCache(value = "custom_user", key = "#id")
    public String getUserNameById(Integer id) {
        return userMapper.findUserNameById(id);
    }

以上就是Spring与Redis集成的正确方式详解的详细内容,更多关于Spring Redis集成方式的资料请关注脚本之家其它相关文章!

相关文章

  • 详解SpringBoot读取resource目录下properties文件的常见方式

    详解SpringBoot读取resource目录下properties文件的常见方式

    这篇文章主要介绍了SpringBoot读取resource目录下properties文件的常见方式,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-02-02
  • SpringBoot中默认缓存实现方案的示例代码

    SpringBoot中默认缓存实现方案的示例代码

    这篇文章主要介绍了SpringBoot中默认缓存实现方案,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • Java synchronized与死锁深入探究

    Java synchronized与死锁深入探究

    这篇文章主要介绍了Java synchronized与死锁,Java中提供了synchronized关键字,将可能引发安全问题的代码包裹在synchronized代码块中,表示这些代码需要进行线程同步
    2023-01-01
  • java注解实现websocket服务的两种方式

    java注解实现websocket服务的两种方式

    Java WebSocket是一种基于TCP协议的双向全双工消息传输技术,它允许服务器和客户端之间实时通信,具有低延迟和高效率的特点,下面这篇文章主要给大家介绍了关于java注解实现websocket服务的两种方式,需要的朋友可以参考下
    2024-08-08
  • 关于springboot2.4跨域配置问题

    关于springboot2.4跨域配置问题

    这篇文章主要介绍了springboot2.4跨域配置的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-07-07
  • Java双冒号(::)运算符使用详解

    Java双冒号(::)运算符使用详解

    之前没用过::这个东西,今天看flink的时候发现官网有个例子用到了这个符号,本文就详细的来介绍一下Java双冒号(::)运算符使用详解,感兴趣的可以了解一下
    2021-09-09
  • Spring中使用Async进行异步功能开发实战示例(大文件上传为例)

    Spring中使用Async进行异步功能开发实战示例(大文件上传为例)

    本文以大文件上传为例,首先讲解在未进行程序异步化时,程序的运行机制和具体表现,然后讲解如何进行异步化的改造,让程序进行异步执行,通过本文不仅能让你掌握如何进行Event的事件开发,同时还能掌握在Spring中如何进行异步开发,熟悉@Async的具体用法,感兴趣的朋友一起看看吧
    2024-08-08
  • Java使用FFmpeg提取音频的详细指南

    Java使用FFmpeg提取音频的详细指南

    FFmpeg 是一个开源的多媒体处理工具,支持视频、音频的编码、解码、转换等多种功能,本文将详细讲解如何使用 FFmpeg 提取音频,包括常见的音频格式提取、音频质量调整、高级处理操作等,内容浅显易懂,适合初学者快速掌握,需要的朋友可以参考下
    2024-11-11
  • Springboot jar运行时如何将jar内的文件拷贝到文件系统中

    Springboot jar运行时如何将jar内的文件拷贝到文件系统中

    因为执行需要,需要把jar内templates文件夹下的的文件夹及文件加压到宿主机器的某个路径下,以便执行对应的脚本文件,这篇文章主要介绍了Springboot jar运行时如何将jar内的文件拷贝到文件系统中,需要的朋友可以参考下
    2024-06-06
  • MyEclipse10安装Log4E插件

    MyEclipse10安装Log4E插件

    这篇文章主要介绍了MyEclipse10安装Log4E插件的相关资料,需要的朋友可以参考下
    2017-10-10

最新评论