天天看點

springboot整合redis

springboot整合redis

1.redis簡介

redis是一個支援key-value的資料庫,資料全部在記憶體中處理,在在一定時間間隔中将資料固化到磁盤。因為是記憶體操作,是以速度特别快。(這裡我們主要介紹redis作為緩存使用)

總結一下他有以下特點:

  1. 速度快,Redis能讀的速度是110000次/s,寫的速度是81000次/s 。
  2. 能夠存儲String,hash,set,map,zset(有序集合)。且單個value的可以1G。
  3. 功能多,如做緩存(使用最多),消息隊列等。轉載一篇應用場景 https://blog.csdn.net/darkhole/article/details/79135088
  4. 可以配置叢集。
  5. 原子性。

2.為什麼使用redis做緩存?

因為可以提高效率,當用戶端多次通路同一個接口時,會查詢同一個sql多次,如果這個sql比較慢就會使使用者體驗大大降低。那麼我們把上一次查詢出的結果放于緩存中,接下來我們直接從緩存中得到資料就會大大提供速度。

缺點:容易被實體環境所影響。

3.springboot使用redis存儲資料

首先我們先安裝redis。具體不多介紹。然後也可以安裝圖形界面。這樣可以使操作更加的友善。這裡我使用的是RedisDesktopManager。

1.修改pom檔案

然後我們建立一個spring boot項目。然後在pom中加入redis依賴。

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

2.新增配置類

然後需要配置類序列化對象。(如果沒有存儲的是亂碼)

@Configuration
public class RedisConfig {

    @Autowired
    private RedisTemplate redisTemplate;
    
    @Bean
    public RedisTemplate redisTemplateInit() {
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        // 設定序列化Key的執行個體化對象
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        // 設定序列化Value的執行個體化對象
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setHashKeySerializer(jackson2JsonRedisSerializer);
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
        return redisTemplate;
    }
}      

3.修改application.yml

然後在配置application.properties連接配接redis。

spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.port=6379  
spring.redis.password=
      

如果想看到sql,還需要在application.properties中配置以下代碼。

#指定dao所在位置
logging.level.com.example.redis.dao=debug      

4.controller實作業務

之後就可以對redis進行存取了,這裡我們使用RedisTemplate ,RedisTemplate 封裝了對redis操作的方法。

public class RedisController {

Map<String, City> cityMap = new HashMap<String, City>();
@Autowired
private RedisTemplate redisTemplate;

    //增删改查String類型 為json類型
    @RequestMapping("/insertjson")
    @ResponseBody
    public void insertData() {
        ValueOperations<String, City> operations = redisTemplate.opsForValue();
        String key = "city_" + 66;
        boolean hasKey = redisTemplate.hasKey(key);//判斷是否有
        if (hasKey) {
            City city = operations.get(key); //查詢
            //operations.getOperations().delete(key);//删除
            return;
        }
        //再次指派就可以修改
        City entity = new City();
        entity.setId((long) 1);
        entity.setDescription("111111");
        entity.setProvinceId((long) 111111);
        // 插入緩存
        operations.set(key, entity, 10, TimeUnit.SECONDS);
    }

   //增删改查hash
   @RequestMapping("/insertmap")
    @ResponseBody
    public void insertMap() {
        HashOperations operations = redisTemplate.opsForHash();
        String key = "city_" + 33;
        int hashKey = 1;
        boolean hasKey = redisTemplate.hasKey(key);// 判斷是否有
        if (hasKey) {
            Map<Integer, String> map1 = new HashMap<>();
            operations.get(key, hashKey); // 查詢
            // operations.getOperations().delete(key);//删除
            return;
        }
        // 修改map在插入就可以
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "qqq");
        map.put(2, "www");
        map.put(3, "eee");
        map.put(4, "rrr");
        operations.putAll(key, map);

    }

    //增删改查list
    @RequestMapping("/insertlist")
    @ResponseBody
    public void insertList() {
        ListOperations operations = redisTemplate.opsForList();
        String key = "city_" + 44;
        boolean hasKey = redisTemplate.hasKey(key);// 判斷是否有
        if (hasKey) {
            operations.range(key, 0, 3);//查詢
            operations.getOperations().delete(key);//删除
            return;
        }
        List list = new ArrayList<>();
        list.add("aaa");
        list.add("bbb");
        list.add("ccc");
        operations.leftPushAll(key, list);
    }

    //增删改查set
    @RequestMapping("/insertset")
    @ResponseBody
    public void insertSet() {
        SetOperations operations = redisTemplate.opsForSet();
        String key = "city_" + 55;
        boolean hasKey = redisTemplate.hasKey(key);
        if(hasKey) {
            operations.members(key);//查詢
            operations.getOperations().delete(key);//删除
            return;
        }
        operations.add(key, "111");
        operations.add(key, "222");
        operations.add(key, "333");
    }   
}