springboot整合jedis操作redis库

本文介绍了如何在Spring Boot项目中集成Redis,包括依赖引入、配置文件设置、启动类、Service与Controller层实现。通过实例展示如何操作字符串和集合,并提供了一个简单的HTTP接口供测试。
摘要由CSDN通过智能技术生成
  1. 导入相关依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.sxd</groupId>
    <artifactId>springboot-redis</artifactId>
    <version>1.0.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.10.RELEASE</version>
    </parent>

    <dependencies>
        <!-- web组件 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
    </dependencies>
</project>
  1. 配置文件
spring:
  redis:
    host: 192.168.6.128
    port: 6379
    password: "!QAZ2wsx"
    database: 0
    jedis:
      pool:
        max-wait: -1  # 连接池最大阻塞等待时间,单位:毫秒。默认为-1,表示不限制
        max-active: 8 # 连接池最大连接数,默认为8。使用负数表示没有限制。
        max-idle: 8   # 默认连接数量最大空闲的连接数,默认为:8.使用负数表示没有限制
        min-idle: 0   # 默认连接数量最小空闲的连接数,默认为:0.允许设置0和正数
    timeout: 10000    # redis 连接超时时间,单位:毫秒
  1. 启动类
package com.sxd;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class AppStart {
    public static void main(String[] args) {
        SpringApplication.run(AppStart.class, args);
    }
}
  1. sercice层
package com.sxd.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * redis 操作类
 */
@Service
public class RedisService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

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

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

    private void setSet(Object object) {
        Set<String> values = (Set<String>)object;
        values.forEach(value -> stringRedisTemplate.opsForSet().add(value));
    }

    private void setString(String key, Object object) {
        String value = (String)object;
        stringRedisTemplate.opsForValue().set(key, value);
    }
}
  1. controller层
package com.sxd.controller;

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

@RestController
@RequestMapping("/redis")
public class RedisController {
    @Autowired
    private RedisService redisService;

    @GetMapping("/set")
    public String set(@RequestParam String key, @RequestParam Object value) {
        redisService.setString(key, value, 60L);
        return "Success!";
    }

    @GetMapping("get")
    public String get(@RequestParam String key) {
        return redisService.getString(key);
    }
}
  1. 启动项目查看效果
    根据具体路由访问:http://localhost:8080/redis/set?key=name&value=123
    设置redis键值对
    http://localhost:8080/redis/get?key=name
    从redis中获取value
  2. 总结
    springboot整合redis步骤如下:
    导入相关依赖
    配置文件配置
    启动类
    service层开发 (redis的查询/新增)
    controller层
    项目结构:
    项目结构
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值