溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用SpringBoot怎么實(shí)現(xiàn)redis分布式緩存

發(fā)布時(shí)間:2021-04-17 17:22:49 來源:億速云 閱讀:153 作者:Leah 欄目:編程語言

今天就跟大家聊聊有關(guān)使用SpringBoot怎么實(shí)現(xiàn)redis分布式緩存,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

架構(gòu)圖:

使用SpringBoot怎么實(shí)現(xiàn)redis分布式緩存

一二級(jí)緩存服務(wù)器

使用Redis緩存,通過網(wǎng)絡(luò)訪問還是不如從內(nèi)存中獲取性能好,所以通常稱之為二級(jí)緩存,從內(nèi)存中取得的緩存數(shù)據(jù)稱之為一級(jí)緩存。當(dāng)應(yīng)用系統(tǒng)需要查詢緩存的時(shí)候,先從一級(jí)緩存里查找,如果有,則返回,如果沒有查找到,則再查詢二級(jí)緩存,架構(gòu)圖如下

使用SpringBoot怎么實(shí)現(xiàn)redis分布式緩存

Spring Boot 2 自帶了前面?zhèn)z種緩存的實(shí)現(xiàn)方式,本文將簡(jiǎn)單實(shí)現(xiàn)第三種,高速一二級(jí)緩存實(shí)現(xiàn)

Redis分布式緩存

引入redis的starter

配置Redis

在application.yml中配置redis信息

spring:
 redis:
  database: 0
  host: 192.168.0.146
  port: 6379
  timeout: 5000

其他相關(guān)配置

# Redis數(shù)據(jù)庫索引(默認(rèn)為0)
spring.redis.database=0 
# Redis服務(wù)器地址
spring.redis.host=127.0.0.1
# Redis服務(wù)器連接端口
spring.redis.port=6379 
# Redis服務(wù)器連接密碼(默認(rèn)為空)
spring.redis.password=
# 連接池最大連接數(shù)(使用負(fù)值表示沒有限制)
spring.redis.pool.max-active=8 
# 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒有限制)
spring.redis.pool.max-wait=-1 
# 連接池中的最大空閑連接
spring.redis.pool.max-idle=8 
# 連接池中的最小空閑連接
spring.redis.pool.min-idle=0 
# 連接超時(shí)時(shí)間(毫秒)
spring.redis.timeout=0

配置Redis緩存序列化機(jī)制

有的時(shí)候需要將對(duì)象存進(jìn)redis(例如一個(gè)JavaBean對(duì)象),但是如果對(duì)象不是可Serializable的,因此需要讓JavaBean對(duì)象實(shí)現(xiàn)Serializable接口

public class UserPO implements Serializable {

如果只是讓JavaBean實(shí)現(xiàn)Serializable接口也是可以存儲(chǔ)的,但是并不好看,那么能不能將JavaBean弄成Json的樣式放進(jìn)redis呢。直接的方式就是自己轉(zhuǎn)換,但是未免有點(diǎn)麻煩,那就只能修改RedisTemplate的序列化機(jī)制了,在配置類中配置上序列化的方法即可

@Bean
 public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
 
   RedisTemplate<String, Object> template = new RedisTemplate();
   template.setConnectionFactory(factory);
 
   Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class);
   ObjectMapper om = new ObjectMapper();
   om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
   om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
   jacksonSeial.setObjectMapper(om);
 
   // 值采用json序列化
   template.setValueSerializer(jacksonSeial);
   //使用StringRedisSerializer來序列化和反序列化redis的key值
   template.setKeySerializer(new StringRedisSerializer());
   template.setHashKeySerializer(new StringRedisSerializer());
   template.setHashValueSerializer(jacksonSeial);
   template.afterPropertiesSet();
 
   return template;
 }

自定義CacheManager

/**
  * 選擇Redis作為默認(rèn)緩存工具
  * @param redisTemplate
  * @return
  */
 @Bean
 public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
 
   RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
   return RedisCacheManager
       .builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
       .cacheDefaults(redisCacheConfiguration).build();
 
 }

RedisConfig完整代碼

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
 
import java.lang.reflect.Method;
import java.net.UnknownHostException;
 
 
 
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
 
  /**
   * 選擇Redis作為默認(rèn)緩存工具
   * @param redisTemplate
   * @return
   */
  @Bean
  public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
 
    RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
    return RedisCacheManager
        .builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
        .cacheDefaults(redisCacheConfiguration).build();
 
  }
 
 
  /**
   * retemplate相關(guān)配置(序列化機(jī)制對(duì)象)
   * @param factory
   * @return
   */
  @Bean
  public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
 
    RedisTemplate<String, Object> template = new RedisTemplate();
    // 配置連接工廠
    template.setConnectionFactory(factory);
 
    //使用Jackson2JsonRedisSerializer來序列化和反序列化redis的value值(默認(rèn)使用JDK的序列化方式)
    Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class);
 
    ObjectMapper om = new ObjectMapper();
    // 指定要序列化的域,field,get和set,以及修飾符范圍,ANY是都有包括private和public
    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    // 指定序列化輸入的類型,類必須是非final修飾的,final修飾的類,比如String,Integer等會(huì)跑出異常
    om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    jacksonSeial.setObjectMapper(om);
 
    // 值采用json序列化
    template.setValueSerializer(jacksonSeial);
    //使用StringRedisSerializer來序列化和反序列化redis的key值
    template.setKeySerializer(new StringRedisSerializer());
 
    // 設(shè)置hash key 和value序列化模式
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(jacksonSeial);
    template.afterPropertiesSet();
 
    return template;
  }
 
 
  /**
   * 自定義key的生成策略
   * @return
   */
  @Bean
  public KeyGenerator myKeyGenerator(){
    return new KeyGenerator() {
      @Override
      public Object generate(Object target, Method method, Object... params) {
        StringBuilder sb = new StringBuilder();
        sb.append(target.getClass().getName());
        sb.append(method.getName());
        for (Object obj : params) {
          sb.append(obj.toString());
        }
        return sb.toString();
      }
    };
  }
}

使用Redi緩存注解

service:

@CachePut(value = "user",key = "'user_'+#result.id")
  public UserPO save(UserPO po) {
    userJpaMapper.save(po);
    return po;
  }
 
  @CachePut(value = "user",key = "'user_'+#result.id")
  public UserPO update(UserPO po) {
    System.out.println("數(shù)據(jù)庫更新");
    userMapper.update(po);
    return po;
  }
 
  @Cacheable(value = "user", key = "'user_'+#id")
  public UserPO getUser(Integer id){
    System.out.println("訪問數(shù)據(jù)庫:"+id);
    return userJpaMapper.getOne(id);
  }
 
  @CacheEvict(value = "user", key = "'user_'+#id")
  public void delete(Integer id) {
    userJpaMapper.deleteById(id);
  }

測(cè)試類:

@Test
 void contextLoads() {
   Integer id = 2;
   UserPO user1 = userService.getUser(id);
   System.out.println("第一次查詢:"+user1.getUserName());
 
   UserPO user2 = userService.getUser(id);
   System.out.println("第二次查詢:"+user2.getUserName());
 }

測(cè)試結(jié)果:第一次查詢的時(shí)候訪問了數(shù)據(jù)庫,第二次查詢的時(shí)候并沒有訪問數(shù)據(jù)庫

使用SpringBoot怎么實(shí)現(xiàn)redis分布式緩存

通過redis-cli 可以查看到數(shù)據(jù)已經(jīng)保存到了redis上面

使用SpringBoot怎么實(shí)現(xiàn)redis分布式緩存

測(cè)試類:

@Test
 void updataUser() {
   Integer id = 4;
   UserPO user1 = userService.getUser(id);
   System.out.println("第一次查詢:"+user1.getUserName()+", 年齡:"+user1.getAge());
 
   user1.setAge(60);
   userService.update(user1);
 
   UserPO user2 = userService.getUser(id);
   System.out.println("第二次查詢:"+user2.getUserName()+", 年齡:"+user2.getAge());
 }

測(cè)試結(jié)果:

使用SpringBoot怎么實(shí)現(xiàn)redis分布式緩存

Redis工具類(redisUtil.java)

1.在RedisConfig中定義redisTemplate操作對(duì)象

/**
   * 對(duì)hash類型的數(shù)據(jù)操作
   * @param redisTemplate
   * @return
   */
  @Bean
  public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
    return redisTemplate.opsForHash();
  }
 
  /**
   * 對(duì)redis字符串類型數(shù)據(jù)操作
   * @param redisTemplate
   * @return
   */
  @Bean
  public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {
    return redisTemplate.opsForValue();
  }
 
  /**
   * 對(duì)鏈表類型的數(shù)據(jù)操作
   * @param redisTemplate
   * @return
   */
  @Bean
  public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
    return redisTemplate.opsForList();
  }
 
  /**
   * 對(duì)無序集合類型的數(shù)據(jù)操作
   * @param redisTemplate
   * @return
   */
  @Bean
  public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
    return redisTemplate.opsForSet();
  }
 
  /**
   * 對(duì)有序集合類型的數(shù)據(jù)操作
   * @param redisTemplate
   * @return
   */
  @Bean
  public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
    return redisTemplate.opsForZSet();
  }

2.在redisUtil工具類中使用這些對(duì)象,并構(gòu)建其操作方法

package com.meng.demo.utils;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
 
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
 
 
/**
 * redis工具類
 */
@Component
public class RedisUtil {
  @Autowired
  private RedisTemplate<String, Object> redisTemplate;
  @Autowired
  private ValueOperations<String, Object> valueOperations;
  @Autowired
  private HashOperations<String, String, Object> hashOperations;
  @Autowired
  private ListOperations<String, Object> listOperations;
  @Autowired
  private SetOperations<String, Object> setOperations;
  @Autowired
  private ZSetOperations<String, Object> zSetOperations;
 
 
  /**=============================共同操作============================*/
  /**
   * 指定緩存失效時(shí)間
   * @param key 鍵
   * @param time 時(shí)間(秒)
   * @return
   */
  public boolean expire(String key,long time){
    try {
      if(time>0){
        redisTemplate.expire(key, time, TimeUnit.SECONDS);
      }
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
 
  /**
   * 根據(jù)key 獲取過期時(shí)間
   * @param key 鍵不能為null
   * @return 時(shí)間(秒) 返回0代表為永久有效
   */
  public long getExpire(String key){
    return redisTemplate.getExpire(key,TimeUnit.SECONDS);
  }
  /**
   * 判斷key是否存在
   * @param key 鍵
   * @return true-存在、false-不存在
   */
  public boolean hasKey(String key){
    try {
      return redisTemplate.hasKey(key);
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
  /**
   * 刪除緩存
   * @param key 可以傳一個(gè)值或多個(gè)
   */
  public void del(String ... key){
    if(key!=null&&key.length>0){
      if(key.length==1){
        redisTemplate.delete(key[0]);
      }else{
        redisTemplate.delete(CollectionUtils.arrayToList(key));
      }
    }
  }
  /**============================ValueOperations操作=============================*/
  /**
   * 普通緩存放入
   * @param key 鍵
   * @param value 值
   * @return true-成功、 false-失敗
   */
  public boolean set(String key,Object value) {
    try {
      valueOperations.set(key, value);
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
  /**
   * 普通緩存放入并設(shè)置時(shí)間
   * @param key 鍵
   * @param value 值
   * @param expireTime 時(shí)間(秒) expireTime要大于0 如果expireTime小于等于0 將設(shè)置無限期
   * @return true成功 false失敗
   */
  public boolean set(String key,Object value,long expireTime){
    try {
      if(expireTime > 0){
        valueOperations.set(key, value, expireTime, TimeUnit.SECONDS);
      }else{
        set(key, value);
      }
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
  /**
   * 普通緩存獲取
   * @param key 鍵
   * @return 值
   */
  public Object get(String key){
    return key==null ? null:valueOperations.get(key);
  }
  /**
   * 遞增
   * @param key 鍵
   * @return
   */
  public long incr(String key, long delta){
    if(delta<0){
      throw new RuntimeException("遞增因子必須大于0");
    }
    return valueOperations.increment(key, delta);
  }
  /**
   * 遞減
   * @param key
   * @param delta 要減少幾(小于0)
   * @return
   */
  public long decr(String key, long delta){
    if(delta<0){
      throw new RuntimeException("遞減因子必須大于0");
    }
    return valueOperations.increment(key, -delta);
  }
 
  /**================================HashOperations操作=================================*/
  /**
   * HashGet
   * @param key 鍵 不能為null
   * @param item 項(xiàng) 不能為null
   * @return 值
   */
  public Object hget(String key,String item){
    return hashOperations.get(key, item);
  }
 
  /**
   * 獲取hashKey對(duì)應(yīng)的所有鍵值
   * @param key 鍵
   * @return 對(duì)應(yīng)的多個(gè)鍵值
   */
  public Map<String, Object> hmget(String key){
    return hashOperations.entries(key);
  }
 
  /**
   * HashSet
   * @param key 鍵
   * @param map 對(duì)應(yīng)多個(gè)鍵值
   * @return true 成功 false 失敗
   */
  public boolean hmset(String key, Map<String,Object> map){
    try {
      hashOperations.putAll(key, map);
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
 
  /**
   * HashSet 并設(shè)置時(shí)間
   * @param key 鍵
   * @param map 對(duì)應(yīng)多個(gè)鍵值
   * @param time 時(shí)間(秒)
   * @return true成功 false失敗
   */
  public boolean hmset(String key, Map<String,Object> map, long time){
    try {
      hashOperations.putAll(key, map);
      if(time>0){
        expire(key, time);
      }
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
 
  /**
   * 向一張hash表中放入數(shù)據(jù),如果不存在將創(chuàng)建
   * @param key 鍵
   * @param item 項(xiàng)
   * @param value 值
   * @return true 成功 false失敗
   */
  public boolean hset(String key,String item,Object value) {
    try {
      hashOperations.put(key, item, value);
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
 
  /**
   * 向一張hash表中放入數(shù)據(jù),如果不存在將創(chuàng)建
   * @param key 鍵
   * @param item 項(xiàng)
   * @param value 值
   * @param time 時(shí)間(秒) 注意:如果已存在的hash表有時(shí)間,這里將會(huì)替換原有的時(shí)間
   * @return true 成功 false失敗
   */
  public boolean hset(String key,String item,Object value,long time) {
    try {
      hashOperations.put(key, item, value);
      if(time>0){
        expire(key, time);
      }
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
 
  /**
   * 刪除hash表中的值
   * @param key 鍵 不能為null
   * @param item 項(xiàng) 可以使多個(gè) 不能為null
   */
  public void hdel(String key, Object... item){
    hashOperations.delete(key,item);
  }
 
  /**
   * 判斷hash表中是否有該項(xiàng)的值
   * @param key 鍵 不能為null
   * @param item 項(xiàng) 不能為null
   * @return true 存在 false不存在
   */
  public boolean hHasKey(String key, String item){
    return hashOperations.hasKey(key, item);
  }
 
  /**
   * hash遞增 如果不存在,就會(huì)創(chuàng)建一個(gè) 并把新增后的值返回
   * @param key 鍵
   * @param item 項(xiàng)
   * @param by 要增加幾(大于0)
   * @return
   */
  public double hincr(String key, String item,double by){
    return hashOperations.increment(key, item, by);
  }
 
  /**
   * hash遞減
   * @param key 鍵
   * @param item 項(xiàng)
   * @param by 要減少記(小于0)
   * @return
   */
  public double hdecr(String key, String item,double by){
    return hashOperations.increment(key, item,-by);
  }
 
  /**============================set=============================
   /**
   * 根據(jù)key獲取Set中的所有值
   * @param key 鍵
   * @return
   */
  public Set<Object> sGet(String key){
    try {
      return setOperations.members(key);
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
 
  /**
   * 根據(jù)value從一個(gè)set中查詢,是否存在
   * @param key 鍵
   * @param value 值
   * @return true 存在 false不存在
   */
  public boolean sHasKey(String key,Object value){
    try {
      return setOperations.isMember(key, value);
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
 
  /**
   * 將數(shù)據(jù)放入set緩存
   * @param key 鍵
   * @param values 值 可以是多個(gè)
   * @return 成功個(gè)數(shù)
   */
  public long sSet(String key, Object...values) {
    try {
      return setOperations.add(key, values);
    } catch (Exception e) {
      e.printStackTrace();
      return 0;
    }
  }
 
  /**
   * 將set數(shù)據(jù)放入緩存
   * @param key 鍵
   * @param time 時(shí)間(秒)
   * @param values 值 可以是多個(gè)
   * @return 成功個(gè)數(shù)
   */
  public long sSetAndTime(String key,long time,Object...values) {
    try {
      Long count = setOperations.add(key, values);
      if(time>0){
        expire(key, time);
      }
      return count;
    } catch (Exception e) {
      e.printStackTrace();
      return 0;
    }
  }
 
  /**
   * 獲取set緩存的長(zhǎng)度
   * @param key 鍵
   * @return
   */
  public long sGetSetSize(String key){
    try {
      return setOperations.size(key);
    } catch (Exception e) {
      e.printStackTrace();
      return 0;
    }
  }
 
  /**
   * 移除值為value的
   * @param key 鍵
   * @param values 值 可以是多個(gè)
   * @return 移除的個(gè)數(shù)
   */
  public long setRemove(String key, Object ...values) {
    try {
      Long count = setOperations.remove(key, values);
      return count;
    } catch (Exception e) {
      e.printStackTrace();
      return 0;
    }
  }
  /**===============================ListOperations=================================
   /**
   * 獲取list緩存的內(nèi)容
   * @param key 鍵
   * @param start 開始
   * @param end 結(jié)束 0 到 -1代表所有值
   * @return
   */
  public List<Object> lGet(String key, long start, long end){
    try {
      return listOperations.range(key, start, end);
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
  /**
   * 獲取list緩存的所有內(nèi)容
   * @param key
   * @return
   */
  public List<Object> lGetAll(String key){
    return lGet(key,0,-1);
  }
 
  /**
   * 獲取list緩存的長(zhǎng)度
   * @param key 鍵
   * @return
   */
  public long lGetListSize(String key){
    try {
      return listOperations.size(key);
    } catch (Exception e) {
      e.printStackTrace();
      return 0;
    }
  }
  /**
   * 通過索引 獲取list中的值
   * @param key 鍵
   * @param index 索引 index>=0時(shí), 0 表頭,1 第二個(gè)元素,依次類推;index<0時(shí),-1,表尾,-2倒數(shù)第二個(gè)元素,依次類推
   * @return
   */
  public Object lGetIndex(String key,long index){
    try {
      return listOperations.index(key, index);
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
  /**
   * 將list放入緩存
   * @param key 鍵
   * @param value 值
   * @return
   */
  public boolean lSet(String key, Object value) {
    try {
      listOperations.rightPush(key, value);
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
  /**
   * 將list放入緩存
   * @param key 鍵
   * @param value 值
   * @param time 時(shí)間(秒)
   * @return
   */
  public boolean lSet(String key, Object value, long time) {
    try {
      listOperations.rightPush(key, value);
      if (time > 0){
        expire(key, time);
      }
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
  /**
   * 將list放入緩存
   * @param key 鍵
   * @param value 值
   * @return
   */
  public boolean lSet(String key, List<Object> value) {
    try {
      listOperations.rightPushAll(key, value);
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
 
  /**
   * 將list放入緩存
   * @param key 鍵
   * @param value 值
   * @param time 時(shí)間(秒)
   * @return
   */
  public boolean lSet(String key, List<Object> value, long time) {
    try {
      listOperations.rightPushAll(key, value);
      if (time > 0) {
        expire(key, time);
      }
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
  /**
   * 根據(jù)索引修改list中的某條數(shù)據(jù)
   * @param key 鍵
   * @param index 索引
   * @param value 值
   * @return
   */
  public boolean lUpdateIndex(String key, long index,Object value) {
    try {
      listOperations.set(key, index, value);
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
 
  /**
   * 移除N個(gè)值為value
   * @param key 鍵
   * @param count 移除多少個(gè)
   * @param value 值
   * @return 移除的個(gè)數(shù)
   */
  public long lRemove(String key,long count,Object value) {
    try {
      Long remove = listOperations.remove(key, count, value);
      return remove;
    } catch (Exception e) {
      e.printStackTrace();
      return 0;
    }
  }
}

3.測(cè)試

@Test
 void test01(){
   redisUtil.set("meng","yang");
   Object key = redisUtil.get("meng");
   System.out.println(key);
 }

 使用SpringBoot怎么實(shí)現(xiàn)redis分布式緩存

看完上述內(nèi)容,你們對(duì)使用SpringBoot怎么實(shí)現(xiàn)redis分布式緩存有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI