天天看点

分布式系列教程(03) -分布式Redis缓存(SpringBoot整合Redis)

代码已上传至Github,有兴趣的同学可以下载来看看:https://github.com/ylw-github/SpringBoot-Redis-Demo

在上一篇博客中《分布式系列教程(02) -分布式Redis缓存(一)》,我们知道了Redis的一些基础概念、优势与弊端、发布订阅以及安装的过程。这篇博客将讲解SpringBoot整合Redis的一些内容。

SpringBoot整合Redis

1.创建Maven项目

2.添加maven依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.RELEASE</version>
</parent>
<dependencies>
    <!-- SpringBoot web 核心组件 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
</dependencies>
      

3.配置文件

spring:
  redis:
    database: 0
    host: 192.168.162.130
    port: 6379
    password: 123
    jedis:
      pool:
        max-active: 8
        max-wait: -1
        max-idle: 8
        min-idle: 0
    timeout: 10000
      

4.启动类

@SpringBootApplication
public class App {

	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}

}
      

5.Controller层

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

import java.util.HashSet;
import java.util.Set;

@RestController
public class IndexController {

    @Autowired
    private RedisService redisService;

    @RequestMapping("/setString")
    public String setString(String key, String value) {
        redisService.set(key, value, 60l);
        return "success";
    }

    @RequestMapping("/getString")
    public String getString(String key) {
        return redisService.getString(key);
    }

    @RequestMapping("/setSet")
    public String setSet() {
        Set<String> set = new HashSet<String>();
        set.add("zhangsan");
        set.add("lisi");
        redisService.setSet("setTest", set);
        return "success";
    }
}
      
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public void set(String key, Object object, Long time) {
        // 存放String 类型
        if (object instanceof String) {
            setString(key, object);
        }
        // 存放 set类型
        if (object instanceof Set) {
            setSet(key, object);
        }
        // 设置有效期 以秒为单位
        stringRedisTemplate.expire(key, time, TimeUnit.SECONDS);
    }

    public void setString(String key, Object object) {
        // 如果是String 类型
        String value = (String) object;
        stringRedisTemplate.opsForValue().set(key, value);
    }

    public void setSet(String key, Object object) {
        Set<String> value = (Set<String>) object;
        for (String oj : value) {
            stringRedisTemplate.opsForSet().add(key, oj);
        }
    }

    public String getString(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

}