SpringBoot:redis分布式緩存


Redis分布式緩存介紹

大家都知道springboot項目都是微服務部署,A服務和B服務分開部署,那么它們如何更新或者獲取共有模塊的緩存數據,或者給A服務做分布式集群負載,如何確保A服務的所有集群都能同步公共模塊的緩存數據,這些都涉及到分布式系統緩存的實現。

如果緩存位於虛擬機內,需要解決的問題是,當數據改變的時候,需要通知其他應用系統的緩存系統讓其緩存失效或者重新加載緩存,ehcache就是通過Terracotta來組件一個緩存集群 。使用reids作為緩存,reids作為單獨的緩存服務器,分布式應用系統通過局域網訪問redis,如下圖

 

 

Spring Boot 緩存

Spring Boot 本身提供了一個基於ConcurrentHashMap 的緩存機制,也集成了EhCache2.x,JCache(JSR-107,EhCache3.x,Hazelcast,Infinispan),還有Couchbase,Redies等。Spring Boot應用通過注解的方式使用統一的使用緩存,只需在方法上使用緩存注解即可,其緩存的具體實現依賴於你選擇的目標緩存管理器。如下使用@Cacheable
   @Service
    public class MenuServiceImpl implements MenuService {
        
        @Cacheable("menu")
        public Menu getMenu(Long id) {...}
            
    }
MenuService實例作為一個容器管理bean,Spring將會生成代理類,在實際調用MenuService.getMenu方法前,會調用緩存管理器,取得名"menu"的緩存,此時,緩存的key就是方法參數id,如果緩存命中,則返回此值,如果沒有找到,則進入實際的MenuService.getMenu方法,在返回調用結果給調用者之前,還會將此查詢結果緩存以備下次使用。

集成Spring cache

集成Spring Cache,只需要在pom中使用如下依賴

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

如果你使用Spring自帶的內存的緩存管理器,需要在appliaction.properties里配置屬性

spring.cache.type=Simple

Simple只適合單機應用或者開發環境使用或者是一個小微系統,通常你的應用是分布式應用,Spring Boot 還支持集成更多的緩存服務器。

  • simple: 基於ConcurrentHashMap實現的緩存,適合單機或者開發環境使用。

  • none:關閉緩存,比如開發階段先確保功能正確,可以先禁止使用緩存

  • redis:使用redis作為緩存,你還需要在pom里增加redis依賴。本章緩存將重點介紹redis緩存以及擴展redis實現一二級緩存

  • Generic,用戶自定義緩存實現,用戶需要實現一個org.springframework.cache.CacheManager的實現

  • 其他還有JCache,EhCache 2.x,Hazelcast等,為了保持本書的簡單,將不在這里一一介紹。

最后,需要使用注解 @EnableCaching 打開緩存功能。

@EnableCaching  //開啟緩存
@MapperScan("com.meng.demo.mapper")
@SpringBootApplication
public class DemoCacheApplication {

Redis配置

在application.yml中配置redis信息

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

其他相關配置

# Redis數據庫索引(默認為0)
spring.redis.database=0  
# Redis服務器地址
spring.redis.host=127.0.0.1
# Redis服務器連接端口
spring.redis.port=6379  
# Redis服務器連接密碼(默認為空)
spring.redis.password=
# 連接池最大連接數(使用負值表示沒有限制)
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  

  

創建RedisConfig配置類

序列化機制

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

public class UserPO implements Serializable {

如果只是讓JavaBean實現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作為默認緩存工具
 * @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.*;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.lang.reflect.Method;


/**
 * Redis配置類
 */
@Configuration
public class RedisConfig extends CachingConfigurerSupport {

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

    }


    /**
     * RedisTemplate相關配置(序列化機制對象)
     * @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;
    }
}

  

使用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("數據庫更新");
        userMapper.update(po);
        return po;
    }

    @Cacheable(value = "user", key = "'user_'+#id")
    public UserPO getUser(Integer id){
        System.out.println("訪問數據庫:"+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;
    for(int i=0;i<5;i++){
        System.out.println("查詢("+i+")");
        UserPO user1 = userService.getUser(id);
    }
}

測試結果:第一次查詢的時候訪問了數據庫,其余都是從Redis緩存中取數據

 通過redis-cli 可以查看到數據已經保存到了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中定義redis操作對象

/**
     * 對hash類型的數據操作
     * @param redisTemplate
     * @return
     */
    @Bean
    public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForHash();
    }

    /**
     * 對redis字符串類型數據操作
     * @param redisTemplate
     * @return
     */
    @Bean
    public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForValue();
    }

    /**
     * 對鏈表類型的數據操作
     * @param redisTemplate
     * @return
     */
    @Bean
    public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForList();
    }

    /**
     * 對無序集合類型的數據操作
     * @param redisTemplate
     * @return
     */
    @Bean
    public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForSet();
    }

    /**
     * 對有序集合類型的數據操作
     * @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工具類
 */
@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;


    /**=============================共同操作============================*/
    /**
     * 指定緩存失效時間
     * @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;
        }
    }

    /**
     * 根據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表中放入數據,如果不存在將創建
     * @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表中放入數據,如果不存在將創建
     * @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遞增 如果不存在,就會創建一個 並把新增后的值返回
     * @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=============================
     /**
     * 根據key獲取Set中的所有值
     * @param key 鍵
     * @return
     */
    public Set<Object> sGet(String key){
        try {
            return setOperations.members(key);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 根據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;
        }
    }

    /**
     * 將數據放入set緩存
     * @param key 鍵
     * @param values 值 可以是多個
     * @return 成功個數
     */
    public long sSet(String key, Object...values) {
        try {
            return setOperations.add(key, values);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 將set數據放入緩存
     * @param key 鍵
     * @param time 時間(秒)
     * @param values 值 可以是多個
     * @return 成功個數
     */
    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 移除的個數
     */
    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緩存的內容
     * @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緩存的所有內容
     * @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倒數第二個元素,依次類推
     * @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;
        }
    }
    /**
     * 根據索引修改list中的某條數據
     * @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 移除的個數
     */
    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);
    }

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM