溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

SpringBoot中怎么利用redis實現(xiàn)分布式緩存

發(fā)布時間:2021-08-06 16:42:55 來源:億速云 閱讀:115 作者:Leah 欄目:編程語言

這期內(nèi)容當中小編將會給大家?guī)碛嘘PSpringBoot中怎么利用redis實現(xiàn)分布式緩存,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

一二級緩存服務器

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

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

Redis分布式緩存

引入redis的starter

配置Redis

在application.yml中配置redis信息

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

其他相關配置

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

配置Redis緩存序列化機制

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

public class UserPO implements Serializable {

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

@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作為默認緩存工具  * @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;   @Configurationpublic class RedisConfig extends CachingConfigurerSupport {   /**   * 選擇Redis作為默認緩存工具   * @param redisTemplate   * @return   */  @Bean  public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {     RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();    return RedisCacheManager        .builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))        .cacheDefaults(redisCacheConfiguration).build();   }    /**   * retemplate相關配置(序列化機制對象)   * @param factory   * @return   */  @Bean  public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {     RedisTemplate<String, Object> template = new RedisTemplate();    // 配置連接工廠    template.setConnectionFactory(factory);     //使用Jackson2JsonRedisSerializer來序列化和反序列化redis的value值(默認使用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等會跑出異常    om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);    jacksonSeial.setObjectMapper(om);     // 值采用json序列化    template.setValueSerializer(jacksonSeial);    //使用StringRedisSerializer來序列化和反序列化redis的key值    template.setKeySerializer(new StringRedisSerializer());     // 設置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);  }

測試類:

@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()); }

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

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

測試類:

@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()); }

測試結果:

Redis工具類(redisUtil.java)

1.在RedisConfig中定義redisTemplate操作對象

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

2.在redisUtil工具類中使用這些對象,并構建其操作方法

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工具類 */@Componentpublic 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;    /**=============================共同操作============================*/  /**   * 指定緩存失效時間   * @param key 鍵   * @param time 時間(秒)   * @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 獲取過期時間   * @param key 鍵不能為null   * @return 時間(秒) 返回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 可以傳一個值或多個   */  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;    }  }  /**   * 普通緩存放入并設置時間   * @param key 鍵   * @param value 值   * @param expireTime 時間(秒) expireTime要大于0 如果expireTime小于等于0 將設置無限期   * @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 項 不能為null   * @return 值   */  public Object hget(String key,String item){    return hashOperations.get(key, item);  }   /**   * 獲取hashKey對應的所有鍵值   * @param key 鍵   * @return 對應的多個鍵值   */  public Map<String, Object> hmget(String key){    return hashOperations.entries(key);  }   /**   * HashSet   * @param key 鍵   * @param map 對應多個鍵值   * @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 并設置時間   * @param key 鍵   * @param map 對應多個鍵值   * @param time 時間(秒)   * @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 項   * @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 項   * @param value 值   * @param time 時間(秒) 注意:如果已存在的hash表有時間,這里將會替換原有的時間   * @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 項 可以使多個 不能為null   */  public void hdel(String key, Object... item){    hashOperations.delete(key,item);  }   /**   * 判斷hash表中是否有該項的值   * @param key 鍵 不能為null   * @param item 項 不能為null   * @return true 存在 false不存在   */  public boolean hHasKey(String key, String item){    return hashOperations.hasKey(key, item);  }   /**   * hash遞增 如果不存在,就會創(chuàng)建一個 并把新增后的值返回   * @param key 鍵   * @param item 項   * @param by 要增加幾(大于0)   * @return   */  public double hincr(String key, String item,double by){    return hashOperations.increment(key, item, by);  }   /**   * hash遞減   * @param key 鍵   * @param item 項   * @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從一個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 值 可以是多個   * @return 成功個數(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 時間(秒)   * @param values 值 可以是多個   * @return 成功個數(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緩存的長度   * @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 值 可以是多個   * @return 移除的個數(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 結束 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緩存的長度   * @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時, 0 表頭,1 第二個元素,依次類推;index<0時,-1,表尾,-2倒數(shù)第二個元素,依次類推   * @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 時間(秒)   * @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 時間(秒)   * @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個值為value   * @param key 鍵   * @param count 移除多少個   * @param value 值   * @return 移除的個數(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.測試

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

上述就是小編為大家分享的SpringBoot中怎么利用redis實現(xiàn)分布式緩存了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

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

AI