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