欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何打造redis緩存組件

 更新時間:2024年12月09日 17:08:38   作者:阿花落知多少  
文章介紹了如何使用熱插拔AOP、反射、Redis自定義注解和SpringEL表達式來打造一個優(yōu)雅的Redis緩存組件,通過這種方式,可以重構和簡化緩存代碼,并提供了Redis配置和自定義注解的詳細說明,文章還包含了AOP測試的總結,并鼓勵讀者參考和支持

打造redis緩存組件

使用熱插拔aop+反射+redis自定義注解+spring EL表達式打造redis緩存組件,優(yōu)雅重構緩存代碼

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查詢或叫進行后續(xù)業(yè)務邏輯
            //-------aop精華部分,才去找findUserById方法干活
            //userMapper.selectByPrimaryKey(id);
            result = joinPoint.proceed();//主業(yè)務邏輯查詢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生成規(guī)則需要程序員用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服務器優(yōu)化方式

    Redis服務器優(yōu)化方式

    文章分享了常見的Redis服務器優(yōu)化技巧和策略,主要包括內存管理、持久化配置、連接配置和網絡優(yōu)化四個方面,內存管理主要是設置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 存儲系統(tǒng),是跨平臺的非關系型數據庫,是一個開源的使用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

最新評論