SpringBoot整合Redis

整合前需要自己下載redis

 

因爲整合過程比較簡單,所以我這裏不說太多

   1.導入redis 起步依賴

     <!-- 配置使用redis啓動器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

2. 在需要 使用redis 緩存的類中 引入redis模板類

    @Resource
    private RedisTemplate<String, String> redisTemplate;

param1:  redis key的參數類型, param2  redis value 值的類型

 

配置redis的ip,端口,在application.yml或application.properties裏

spring.redis.host=127.0.0.1
spring.redis.port=6379

3. 使用redis提供的類查詢數據

       查詢key = user.findall 的數據

String jsonUserList = redisTemplate.boundValueOps("user.findall").get();

 

4. 使用redis 存入數據

  redisTemplate.boundValueOps("user.findall").set(jsonList);

5.設置插入數據的存活時間爲1分鐘

redisTemplate.boundValueOps("user.findall").set(jsonList,60*1000);

 

6.刪除數據

redisTemplate.delete("user.findall");

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章