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

Spring與Redis集成的正確方式流程詳解

 更新時間:2023年06月20日 14:19:40   作者:右耳菌  
這篇文章主要為大家介紹了Spring與Redis集成的正確方式流程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

1. 引入RedisTemplate

據(jù)以前的情況,我們在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); // 嘗試獲取數(shù)據(jù)
            if (userStr != null && !userStr.isEmpty()) { // 如果獲取到有效數(shù)據(jù),則轉(zhuǎn)換后返回
                user = JSONObject.parseObject(userStr, User.class);
            } else {// 如果沒有獲取到數(shù)據(jù),則查詢數(shù)據(jù)庫返回
                user = userMapper.findUserById(id);
                if (user != null) jedis.set("user_" + id, JSONObject.toJSONString(user)); // 設(shè)置到redis中
            }
        } finally {
            // 記得關(guān)閉Jedis,因為這里使用的是JedisPool,所以這里的關(guān)閉并不是直接關(guān)閉連接,而是釋放,以供其他的業(yè)務(wù)使用
            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);
        //設(shè)置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);
        // 設(shè)置到redis中
        if (user != null) redisTemplate.opsForValue().set("user_" + id, user);
        return user;
    }

大概看一下關(guān)于RedisTemplate的方法

看了以上的內(nèi)容,可以看到引入了RedisTemplate其實已經(jīng)很簡潔了,但是明顯還不夠,下面我們將考慮引入 “注解”

2. 引入注解

  • 開啟緩存 @EnableCaching
    AppConfig.java
@Configuration
@EnableCaching
public class AppConfig {
  ...
}
  • 引入@Cacheable,表示這個方法將會訪問緩存,如果無法命中緩存的話,會將方法返回的值存入redis,假設(shè)有注解為 @Cacheable(value="user", key = "#id"),那么生成的key值為 user::{id},即如果id為1 那么生成的 key就是 user::1
    @Override
    @Cacheable(value="user", key = "#id")
    // 這里返回的值會被存放到redis,key-value格式,其中生成的key值(假設(shè)id為1): user::1
    public User findUserById(Integer id) {
        User user = userMapper.findUserById(id);
        return user;
    }

但是這樣還不夠,因為Spring并不清楚緩存的方式是什么,這就涉及到CacheManager

  • 設(shè)置CacheManager,在AppConfig中加入以下內(nèi)容
    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setHostName(host); // 這里是redis的ip
        redisStandaloneConfiguration.setPort(port);// 這里是redis的端口
        // 自適應(yīng)集群變化
        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實現(xiàn)緩存

  • 引入AOP相關(guān)的包
    <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>
  • 創(chuàng)建@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的規(guī)則,可以使用springEL表達式,可以使用方法執(zhí)行的一些參數(shù)
     */
    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);
        // 自適應(yīng)集群變化
        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;
    }
}
  • 創(chuàng)建 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();
            // 創(chuàng)建解析器
            ExpressionParser parser = new SpelExpressionParser();
            Expression expression = parser.parseExpression(cacheKey);
            EvaluationContext context = new StandardEvaluationContext(); // 參數(shù)
            // 添加參數(shù)
            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、不存在則繼續(xù)行方法
            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);
    }
  • 實際實現(xiàn)方法 getUserNameById,使用方式
    @Override
    @CustomCache(value = "custom_user", key = "#id")
    public String getUserNameById(Integer id) {
        return userMapper.findUserNameById(id);
    }

以上就是Spring與Redis集成的正確方式詳解的詳細內(nèi)容,更多關(guān)于Spring Redis集成方式的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論

镇巴县| 永城市| 璧山县| 鹰潭市| 张家界市| 察哈| 连云港市| 鄢陵县| 温宿县| 云安县| 岗巴县| 孟津县| 马山县| 盘锦市| 来宾市| 崇阳县| 临武县| 封丘县| 兴国县| 乌兰浩特市| 辉县市| 遵化市| 商丘市| 察雅县| 阜阳市| 札达县| 乌什县| 若尔盖县| 康平县| 华宁县| 土默特右旗| 孝昌县| 龙川县| 卓尼县| 万山特区| 霍林郭勒市| 东宁县| 从化市| 织金县| 乐亭县| 新余市|