最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

如何打造redis緩存組件

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

打造redis緩存組件

使用熱插拔aop+反射+redis自定義注解+spring EL表達(dá)式打造redis緩存組件,優(yōu)雅重構(gò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  開(kāi)啟AOP自動(dòng)代理
public class RedisConfig
{
    /**
     * @param lettuceConnectionFactory
     * @return
     *
     * redis序列化的工具配置類(lèi),下面這個(gè)請(qǐng)一定開(kāi)啟配置
     * 127.0.0.1:6379> keys *
     * 1) "ord:102"  序列化過(guò)
     * 2) "\xac\xed\x00\x05t\x00\aord:102"   野生,沒(méi)有序列化過(guò)
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory)
    {
        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();

        redisTemplate.setConnectionFactory(lettuceConnectionFactory);
        //設(shè)置key序列化方式string
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        //設(shè)置value的序列化方式j(luò)son
        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自動(dòng)代理
{
    //約等于鍵的前綴prefix,
    String keyPrefix();

    //SpringEL表達(dá)式,解析占位符對(duì)應(yīng)的匹配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;

    //配置織入點(diǎn)
    @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 確定方法名后獲得該方法上面配置的注解標(biāo)簽MyRedisCache
            MyRedisCache myRedisCacheAnnotation = method.getAnnotation(MyRedisCache.class);

            //3 拿到了MyRedisCache這個(gè)注解標(biāo)簽,獲得該注解上面配置的參數(shù)進(jìn)行封裝和調(diào)用
            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 獲得方法里面的形參個(gè)數(shù)
            Object[] args = joinPoint.getArgs();
            DefaultParameterNameDiscoverer discoverer = new DefaultParameterNameDiscoverer();
            String[] parameterNames = discoverer.getParameterNames(method);
            for (int i = 0; i < parameterNames.length; i++)
            {
                System.out.println("獲得方法里參數(shù)名和值: "+parameterNames[i] + "\t" + args[i].toString());
                context.setVariable(parameterNames[i], args[i].toString());
            }

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

            //7 先去redis里面查詢看有沒(méi)有
            result = redisTemplate.opsForValue().get(key);
            if (result != null)
            {
                System.out.println("------redis里面有,我直接返回結(jié)果不再打擾mysql: " + result);
                return result;
            }

            //8 redis里面沒(méi)有,去找msyql查詢或叫進(jìn)行后續(xù)業(yè)務(wù)邏輯
            //-------aop精華部分,才去找findUserById方法干活
            //userMapper.selectByPrimaryKey(id);
            result = joinPoint.proceed();//主業(yè)務(wù)邏輯查詢mysql,放行放行放行

            //9 mysql步驟結(jié)束,還需要把結(jié)果存入redis一次,緩存補(bǔ)償
            if (result != null)
            {
                System.out.println("------redis里面無(wú),還需要把結(jié)果存入redis一次,緩存補(bǔ)償: " + result);
                redisTemplate.opsForValue().set(key, result);
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }

        return result;
    }
}

測(cè)試

    /**
     * 會(huì)將返回值存進(jìn)redis里,key生成規(guī)則需要程序員用SpEL表達(dá)式自己指定,value就是程序從mysql查出并返回的user
     * redis的key 等于  keyPrefix:matchValue
     */
    @Override
    @MyRedisCache(keyPrefix = "user",matchValue = "#id")
    public User getUserById(Integer id)
    {
        return userMapper.selectByPrimaryKey(id);
    }

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

拉萨市| 南江县| 株洲市| 丹凤县| 射洪县| 比如县| 错那县| 高要市| 德阳市| 信宜市| 罗田县| 磐石市| 东丽区| 永平县| 清镇市| 新田县| 安远县| 永吉县| 色达县| 开封市| 珲春市| 姜堰市| 怀安县| 陆良县| 朝阳县| 勐海县| 武宁县| 余庆县| 嘉荫县| 兴海县| 石泉县| 舞钢市| 宜宾市| 政和县| 苍溪县| 宜君县| 三门县| 株洲县| 五指山市| 修水县| 沅江市|