Spring Boot整合Redis

1.環境及依賴

在使用Redis前,需要在本地安裝和開啓Redis服務,關於如何安裝Redis和啓動Redis服務,可以參考以下文章:

在Windows下安裝Redis

安裝完畢之後快速搭建Spring Boot項目:

 完整的pom依賴如下:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>

注:commons-pool2爲連接池

配置application.properties:

spring.redis.database=0
spring.redis.port=6379
spring.redis.host=127.0.0.1
spring.redis.lettuce.pool.min-idle=5
spring.redis.lettuce.pool.max-idle=10
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-wait=1ms
spring.redis.lettuce.shutdown-timeout=100ms

注:我安裝Redis時並沒有設置連接密碼,如果設置了連接密碼,需要用 spring.redis.password= 進行設置

2.實現

封裝redis操作實現類

package com.chen.service;

import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Service
public class RedisService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    private ValueOperations vo;

    /**
     * 獲取操作字符串的ValueOperations
     *
     * @return
     */
    public ValueOperations<String, Object> getValueOperations() {
        vo = stringRedisTemplate.opsForValue();
        return vo;
    }

    /**
     * 保存數據到redis--永久存儲
     *
     * @param key
     * @param value
     */
    public void set(String key, Object value) {
        getValueOperations().set(key, value);
    }

    /**
     * 保存數據到redis--設置保存時間
     *
     * @param key
     * @param value
     * @param expire 保存時間,以秒爲單位
     */
    public void set(String key, Object value, Long expire) {
        getValueOperations().set(key, value);
        //設置過期時間
        stringRedisTemplate.expire(key, expire, TimeUnit.SECONDS);
    }

    /**
     * 從redis中獲取數據
     *
     * @param key
     * @return
     */
    public Object get(String key) {
        return getValueOperations().get(key);
    }

    /**
     * 根據key刪除對應的數據
     *
     * @param key
     */
    public void delete(String key) {
        RedisOperations<String, Object> operations = getValueOperations().getOperations();
        operations.delete(key);
    }

}

這裏只是簡單地對基本數據類型進行操作,對於集合類不做相關操作。

接下來在controller層進行調用:

package com.chen.controller;

import com.chen.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @Autowired
    private RedisService redisService;

    @RequestMapping("/test")
    public String test(){
        redisService.set("c","測試", (long) 5);
        return "success";
    }
}

 測試結果:

 

 

 

發佈了135 篇原創文章 · 獲贊 76 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章