首页 > 数据库 >springboot中redis使用和工具

springboot中redis使用和工具

时间:2023-02-27 18:56:12浏览次数:57  
标签:code springboot redis telephone springframework 工具 data String

application.properties

#Redis相关配置
spring.data.redis.host=localhost
#端口
spring.data.redis.port=6379
#reids 数据库索引
spring.data.redis.database = 0

RedisDao.java

package com.bank.dao;

public interface RedisDao {
    // 存储验证码
    boolean save(String telephone,String code);

    // 获取验证码
    String getCode(String telephone);

}
package com.bank.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import java.util.concurrent.TimeUnit;

@Repository
public class RedisDaoImpl implements RedisDao{

//  RedisTemplate类型对象将来将从java配置类中进行注入
    @Autowired
    private RedisTemplate redisTemplate;

////  过期时长
//    private final Long DURATION = 1 * 24 * 60 * 60 * 1000L;

    @Override
    public boolean save(String telephone, String code) {
        ValueOperations op = redisTemplate.opsForValue();
//        验证码失效时长2分钟
        try{
            //设置当前的key以及value值并且设置过期时间
            op.set(telephone,code,120,TimeUnit.SECONDS);
            return true;
        } catch(Exception e){
            return false;
        }
    }

    @Override
    public String getCode(String telephone) {
        ValueOperations op = redisTemplate.opsForValue();
        //返回key中字符串的子字符
        String code = (String)op.get(telephone);
        return code;
    }
}

依赖

<!-- Redis相关依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>

(11条消息) RedisTemplate最全的常用方法_redistemplate常用方法_GavinYCF的博客-CSDN博客

标签:code,springboot,redis,telephone,springframework,工具,data,String
From: https://www.cnblogs.com/liweimingbk/p/17161477.html

相关文章

  • springboot脱包部署
    <plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration>......
  • centos 7 时间同步使用的是chrony工具
    centos7时间同步使用的是chrony工具1、检测chrony包是否安装[root@martin~]#rpm-qa|grepchrony2、安装chrony[root@martin~]#yuminstallchrony-y3、启动chrony并......
  • springboot条件注册Condition注解
    环境识别importorg.springframework.context.annotation.Condition;importorg.springframework.context.annotation.ConditionContext;importorg.springframework.c......
  • 基于SpringBoot WebMagic爬虫爬取大乐透双色球
    大乐透网页地址:https://kjh.55128.cn/dlt-history-360.htm双色球网页地址:https://kjh.55128.cn/ssq-history-120.htm 注:程序仅用于个人兴趣爱好,不得用于商业行为,本......
  • Redis 缓存穿透,击穿,雪崩 并发 之 读写数据编码实战
     什么是缓存穿透  缓存穿透是指查询一个缓存中和数据库中都不存在的数据,导致每次查询这条数据都会透过缓存,直接查库,最后返回空。当用户使用这条不存在的数据疯狂......
  • SpringBoot项目打包部署
    转载自:https://blog.csdn.net/yw_2022/article/details/122649955========= SpringBoot项目打包在linux服务器中运行:jar类型项目会打成jar包:jar类型项目使用SpringBoo......
  • 如何将BI 工具与业务系统进行单点登录对接,实现用户权限通用
    首先来看下两套系统的用户体系功能,左边是BI工具,右边是业务系统,需要实现用户权限对接和打通:单点登录体系及用户场景• 场景1.用户登录WynBI页面使用第三方业务系统......
  • 可视化之数据可视化最强工具推荐
    在数据科学领域,数据可视化不仅仅是一个词。这是一个完整的过程,为我们今天面临的许多问题提供了解决方案。无论是我们需要分析的大数据,还是我们需要为利益相关者制作的演示......
  • java netty socket实例:报文长度+报文内容,springboot
    前言说实话,javanetty方面的资料不算多,尤其是自定义报文格式的,少之又少自己写了个简单的收发:报文长度+报文内容发送的话,没有写自动组装格式,自己看需求吧,需要的话,自己完......
  • SpringBoot中对拦截器和过滤器的理解
    1、两者的关系   2、过滤器的使用步骤实现Filter接口,并加上@WebFilter注解@Slf4j//filterName为该过滤器的名称;//urlPatterns对哪些url进行拦截@WebFil......