SpringBoot整合Redis

本文介绍了如何在SpringBoot项目中整合Redis,包括Redis的安装、配置、RedisController的编写以及RedisUtil工具类的实现。通过Postman进行接口测试,展示了基本的set和get操作,并讨论了在实际开发中使用工具类的重要性。同时,提到了异常处理和自定义异常工具类的创建,以及在遇到问题时如何优化用户体验。
摘要由CSDN通过智能技术生成

本地安装redis及客户端工具请参考:redis安装详细步骤   Redis Desktop Manager安装详细步骤

此demo继续在此前SpringBootMybtis项目上做延伸扩展,感兴趣的请参考:SpringBoot+MyBatis+Mysql整合详细步骤

1、pom文件引入redis架包

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

2、application.yml文件配置redis信息

spring:
  #redis配置
  redis:
    host: 127.0.0.1
    port: 6379
    password:
    lettuce:
    pool:
        #最大连接数
        max-active: 8
        #最大阻塞等待时间
        max-wait: 6000
        #最大空闲连接
        max-idle: 8
        #最小空闲连接
        min-idle: 0
        timeout: 5000
    database: 0

3、编写RedisController.java

package com.example.api.testRedis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value="/redis", produces = "application/json; charset=UTF-8")
public class RedisController {

    @Autowired
    private RedisTemplate  redisTemplate;

  

    @RequestMapping( "/set")
    public String setUserName( String name) {
        redisTemplate.opsForValue().set("1", name);
        return "succees";
    }

    @RequestMapping( "/get")
    public String getUserName( String key) {
        return  redisTemplate.opsForValue().get(key).toString();
    }



}

4、启动项目,使用Postman进行接口测试redis的数据操作

1)set操作如下图

2)get操作

当然如上操作只是最基本的操作,在实际开发过程中不建议直接这样使用,因为不够灵活,所以在现有的基础上继续扩展

5、编写RedisUtil工具类(便于开发使用工具类进行开发提高效率,更加方便快捷)

package com.example.api.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * CreateTime: 2019-08-01 18:14
 * ClassName: RedisUtil
 * Package: com.example.api.utils
 * Describe:Redis工具类
 *
 * @author XieZhiXin
 */
@Component
public class RedisUtil {

    private static final Long SUCCESS = 1L;


    @Autowired
    private RedisTemplate  redisTemplate;

    //---------------------- common --------------------------

    /**
     * 指定缓存失效时间
     *
     * @param key  key值
     * @param time 缓存时间
     */
    public void expire(String key, long time) {
        if (time > 0) {
            redisTemplate.expire(key, time, TimeUnit.SECONDS);
        } else {
            throw MyExceptionUtils.mxe("设置的时间不能为0或者小于0!!");
        }
    }

    /**
     * 判断key是否存在
     *
     * @param key 传入ke值
     * @return true 存在  false  不存在
     */
    public Boolean existsKey(String key) {
        return redisTemplate.hasKey(key);
    }

    /**
     * 判断key存储的值类型
     *
     * @param key key值
     * @return DataType[string、list、set、zset、hash]
     */
    public DataType typeKey(String key) {
        return redisTemplate.type(key);
    }

    /**
     * 删除指定的一个数据
     *
     * @param key key值
     * @return true 删除成功,否则返回异常信息
     */
    public Boolean  deleteKey(String key) {
        try {
             redisTemplate.delete(key);
             return true;
        } catch (Exception ex) {
            throw MyExceptionUtils.mxe("删除失败!", ex);
        }
    }

    /**
     * 删除多个数据
     *
     * @param keys key的集合
     * @return true删除成功,false删除失败
     */
    public Boolean deleteKey(Collection<String> keys) {
        try {
            redisTemplate.delete(keys);
            return true;
        } catch
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值