如何打造redis缓存组件

 更新时间:2024年12月09日 17:08:38   作者:阿花落知多少  
文章介绍了如何使用热插拔AOP、反射、Redis自定义注解和SpringEL表达式来打造一个优雅的Redis缓存组件,通过这种方式,可以重构和简化缓存代码,并提供了Redis配置和自定义注解的详细说明,文章还包含了AOP测试的总结,并鼓励读者参考和支持

打造redis缓存组件

使用热插拔aop+反射+redis自定义注解+spring EL表达式打造redis缓存组件,优雅重构缓存代码

redis配置

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
@EnableAspectJAutoProxy //V2  开启AOP自动代理
public class RedisConfig
{
    /**
     * @param lettuceConnectionFactory
     * @return
     *
     * redis序列化的工具配置类,下面这个请一定开启配置
     * 127.0.0.1:6379> keys *
     * 1) "ord:102"  序列化过
     * 2) "\xac\xed\x00\x05t\x00\aord:102"   野生,没有序列化过
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory)
    {
        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();

        redisTemplate.setConnectionFactory(lettuceConnectionFactory);
        //设置key序列化方式string
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        //设置value的序列化方式json
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());

        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

        redisTemplate.afterPropertiesSet();

        return redisTemplate;
    }
}

自定义注解

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 MyRedisCache     //@EnableAspectJAutoProxy //启AOP自动代理
{
    //约等于键的前缀prefix,
    String keyPrefix();

    //SpringEL表达式,解析占位符对应的匹配value值
    String matchValue();
}

AOP

import jakarta.annotation.Resource;
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.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 org.aspectj.lang.reflect.MethodSignature;

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


@Component
@Aspect
public class MyRedisCacheAspect
{
    @Resource
    private RedisTemplate redisTemplate;

    //配置织入点
    @Pointcut("@annotation(com.atguigu.interview2.annotations.MyRedisCache)")
    public void cachePointCut(){}

    @Around("cachePointCut()")
    public Object doCache(ProceedingJoinPoint joinPoint)
    {
        Object result = null;


        /**
         *     @MyRedisCache(keyPrefix = "user",matchValue = "#id")
         *     public User getUserById(Integer id)
         *     {
         *         return userMapper.selectByPrimaryKey(id);
         *     }
         */


        try
        {
            //1 获得重载后的方法名
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            Method method = signature.getMethod();

            //2 确定方法名后获得该方法上面配置的注解标签MyRedisCache
            MyRedisCache myRedisCacheAnnotation = method.getAnnotation(MyRedisCache.class);

            //3 拿到了MyRedisCache这个注解标签,获得该注解上面配置的参数进行封装和调用
            String keyPrefix = myRedisCacheAnnotation.keyPrefix();
            String matchValueSpringEL = myRedisCacheAnnotation.matchValue();

            //4 SpringEL 解析器
            ExpressionParser parser = new SpelExpressionParser();
            Expression expression = parser.parseExpression(matchValueSpringEL);//#id
            EvaluationContext context = new StandardEvaluationContext();

            //5 获得方法里面的形参个数
            Object[] args = joinPoint.getArgs();
            DefaultParameterNameDiscoverer discoverer = new DefaultParameterNameDiscoverer();
            String[] parameterNames = discoverer.getParameterNames(method);
            for (int i = 0; i < parameterNames.length; i++)
            {
                System.out.println("获得方法里参数名和值: "+parameterNames[i] + "\t" + args[i].toString());
                context.setVariable(parameterNames[i], args[i].toString());
            }

            //6 通过上述,拼接redis的最终key形式
            String key = keyPrefix + ":" + expression.getValue(context).toString();
            System.out.println("------拼接redis的最终key形式: " + key);

            //7 先去redis里面查询看有没有
            result = redisTemplate.opsForValue().get(key);
            if (result != null)
            {
                System.out.println("------redis里面有,我直接返回结果不再打扰mysql: " + result);
                return result;
            }

            //8 redis里面没有,去找msyql查询或叫进行后续业务逻辑
            //-------aop精华部分,才去找findUserById方法干活
            //userMapper.selectByPrimaryKey(id);
            result = joinPoint.proceed();//主业务逻辑查询mysql,放行放行放行

            //9 mysql步骤结束,还需要把结果存入redis一次,缓存补偿
            if (result != null)
            {
                System.out.println("------redis里面无,还需要把结果存入redis一次,缓存补偿: " + result);
                redisTemplate.opsForValue().set(key, result);
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }

        return result;
    }
}

测试

    /**
     * 会将返回值存进redis里,key生成规则需要程序员用SpEL表达式自己指定,value就是程序从mysql查出并返回的user
     * redis的key 等于  keyPrefix:matchValue
     */
    @Override
    @MyRedisCache(keyPrefix = "user",matchValue = "#id")
    public User getUserById(Integer id)
    {
        return userMapper.selectByPrimaryKey(id);
    }

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • CentOS下Redis数据库的基本安装与配置教程

    CentOS下Redis数据库的基本安装与配置教程

    这篇文章主要介绍了CentOS下Redis数据库的基本安装与配置教程,Redis一般被用作基于内存的缓存式数据存储,要的朋友可以参考下
    2015-12-12
  • Redis字符串类型的常用命令小结

    Redis字符串类型的常用命令小结

    这篇文章给大家整理了在操作Redis字符串类型中的常用命令,文章总结的很全面,对大家学习Redis具有一定的参考借鉴价值,下面来一起看看吧。
    2016-09-09
  • Redis服务器优化方式

    Redis服务器优化方式

    文章分享了常见的Redis服务器优化技巧和策略,主要包括内存管理、持久化配置、连接配置和网络优化四个方面,内存管理主要是设置maxmemory参数和选择合适的内存淘汰策略,持久化配置包括RDB持久化和AOF持久化
    2024-09-09
  • Redis过期Key删除策略和内存淘汰策略的实现

    Redis过期Key删除策略和内存淘汰策略的实现

    当内存使用达到上限,就无法存储更多数据了,为了解决这个问题,Redis内部会有两套内存回收的策略,过期Key删除策略和内存淘汰策略,本文就来详细的介绍一下这两种方法,感兴趣的可以了解一下
    2024-02-02
  • redis适合场景八点总结

    redis适合场景八点总结

    在本篇文章中我们给大家整理了关于redis适合什么场景的8点知识点内容,需要的朋友们参考下。
    2019-06-06
  • RedisTemplate中boundHashOps的使用小结

    RedisTemplate中boundHashOps的使用小结

    redisTemplate.boundHashOps(key) 是 RedisTemplate 类的一个方法,本文主要介绍了RedisTemplate中boundHashOps的使用小结,具有一定的参考价值,感兴趣的可以了解一下
    2024-04-04
  • 详解Redis基本命令与使用场景

    详解Redis基本命令与使用场景

    REmote DIctionary Server(Redis)是一个由Salvatore Sanfilippo写的key-value 存储系统,是跨平台的非关系型数据库,是一个开源的使用ANSI C语言编写、遵守BSD协议、支持网络、可基于内存、分布式、可选持久性的键值对(Key-Value)存储数据库,并提供多种语言的 API。
    2021-06-06
  • Redis集群Lettuce主从切换问题解决方案

    Redis集群Lettuce主从切换问题解决方案

    这篇文章主要为大家介绍了Redis集群Lettuce主从切换问题解决方案,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-07-07
  • redis-cli -p 6379 info命令详解

    redis-cli -p 6379 info命令详解

    这篇文章主要介绍了redis-cli -p 6379 info命令详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12
  • 使用Redis命令操作数据库的常见错误及解决方法

    使用Redis命令操作数据库的常见错误及解决方法

    由于Redis是内存数据库,因此可能会存在一些安全问题,下面这篇文章主要给大家介绍了关于使用Redis命令操作数据库的常见错误及解决方法,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-02-02

最新评论