一、前言
Redis现在已经普遍应用于电商项目中,它是用C语言开发的一个高性能键值对数据库,用于数据缓存,主要用于处理大量数据的高访问负载,本文记录Redis的安装和启动,并以存储验证短信验证码为例。
二、在windows下的安装和启动
1、下载redis:https://github.com/MicrosoftArchive/redis/releases
2、下载完成后,解压到指定目录下
3、启动redis
打开cmd命令提示框进入redis的解压目录
执行启动命令
redis-server.exe redis.windows.conf
至此,redis启动完成,默认启用6379端口。
三、项目整合redis
1、在pom.xml文件中添加redis所需要的依赖包
<!--redis依赖配置--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
2、在配置文件application.yml中添加redis配置
#项目启动端口 server: port: 10077 spring: #连接mysql数据库 datasource: url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai username: root password: root #连接redis缓存数据库 redis: host: localhost #redis服务器地址 database: 0 #redis数据库索引(默认为0) port: 6379 #redis服务器连接端口 password: #redis连接密码(默认为空) #redis连接池 jedis: pool: max-wait: -1ms #连接池最大阻塞等待时间(使用负值表示没有限制) min-idle: 0 #连接池中的最小空闲连接 max-idle: 8 #连接池中的最大空闲连接 max-active: 8 #连接池最大连接数(使用负值表示没有限制) timeout: 3000ms #连接超时时间(毫秒) #redis自定义key redis: key: prefix: authCode: "portal:authCode:" expire: authCode: 120 #mybatis映射xml文件路径 mybatis: mapper-locations: classpath: com/zzb/test/admin/mapper/*.xml
3、在service包下新建redis的操作接口
package com.zzb.test.admin.service; /** * redis进行操作的Service * 对象和数组都已json形式进行存储 * Created by zzb on 2019/11/19 18:53 */ public interface RedisService { /** * 存储数据 * @param key * @param value */ void set(String key,String value); /** * 获取数据 * @param key * @return */ String get(String key); /** * 设置超期时间 * @param key * @param expire * @return */ boolean expire(String key, long expire); /** * 删除数据 * @param key */ void remove(String key); /** * 自增操作 * @param key * @param delta 自增步长 * @return */ Long increment(String key, long delta); }
4、在impl包下新建RedisService接口的实现类RedisServiceImpl
package com.zzb.test.admin.service.impl; import com.zzb.test.admin.service.RedisService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.concurrent.TimeUnit; /** * RedisService的实现类 * Created by zzb on 2019/11/19 19:01 */ @Service @Transactional public class RedisServiceImpl implements RedisService { @Autowired private StringRedisTemplate stringRedisTemplate; @Override public void set(String key, String value) { stringRedisTemplate.opsForValue().set(key,value); } @Override public String get(String key) { return stringRedisTemplate.opsForValue().get(key); } @Override public boolean expire(String key, long expire) { return stringRedisTemplate.expire(key,expire, TimeUnit.SECONDS); } @Override public void remove(String key) { stringRedisTemplate.delete(key); } @Override public Long increment(String key, long delta) { return stringRedisTemplate.opsForValue().increment(key,delta); } }
5、在controller包下新建会员登录注册管理类UmsMemberController
package com.zzb.test.admin.controller; import com.zzb.test.admin.common.CommonResult; import com.zzb.test.admin.service.UmsMemberService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; /** * 会员登录注册管理controller * Created by zzb on 2019/11/19 19:13 */ @Api(tags = "UmsMemberController", description = "会员登录注册管理") @Controller public class UmsMemberController { @Autowired private UmsMemberService umsMemberService; @ApiOperation("获取手机验证码") @RequestMapping(value = "/admin/member/getSmsCode", method = RequestMethod.POST) @ResponseBody public CommonResult getSmsCode(String telephone){ String smsCode = umsMemberService.generateSmsCode(telephone); if (!StringUtils.isEmpty(smsCode)) { return CommonResult.success(smsCode,"获取验证码成功"); } return CommonResult.failed("获取验证码失败"); } @ApiOperation("校验输入验证码") @RequestMapping(value = "/admin/member/validSmsCode",method = RequestMethod.POST) @ResponseBody public CommonResult validSmsCode(String telephone,String smsCode){ return umsMemberService.validSmsCode(telephone,smsCode); } }
6、在service包下新建会员管理接口UmsMemberService
package com.zzb.test.admin.service; import com.zzb.test.admin.common.CommonResult; /** * 会员管理接口service * Created by zzb on 2019/11/19 19:44 */ public interface UmsMemberService { /** * 根据手机生成手机验证码 * @param telephone * @return */ String generateSmsCode(String telephone); /** * 校验输入的手机验证码 * @param telephone * @param smsCode * @return */ CommonResult validSmsCode(String telephone, String smsCode); }
7、在impl包下新建UmsMemberService接口的实现类UmsMemberServiceImpl
package com.zzb.test.admin.service.impl; import com.zzb.test.admin.common.CommonResult; import com.zzb.test.admin.service.RedisService; import com.zzb.test.admin.service.UmsMemberService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.StringUtils; import java.util.Random; /** * UmsMemberService的实现类 * Created by zzb on 2019/11/19 19:47 */ @Service @Transactional public class UmsMemberServiceImpl implements UmsMemberService { @Autowired private RedisService redisService; @Value("${redis.key.prefix.authCode}") private String REDIS_KEY_PREFIX_AUTH_CODE; @Value("${redis.key.expire.authCode}") private Long AUTH_CODE_EXPIRE_SECONDS; @Override public String generateSmsCode(String telephone) { //生成随机的6位数验证码 StringBuilder sb = new StringBuilder(); Random random = new Random(); for (int i=0;i<6;i++) { sb.append(random.nextInt(10)); } //将生成的验证码存入redis缓存数据库 redisService.set(REDIS_KEY_PREFIX_AUTH_CODE + telephone,sb.toString()); //设置过期时间 redisService.expire(REDIS_KEY_PREFIX_AUTH_CODE + telephone,AUTH_CODE_EXPIRE_SECONDS); return sb.toString(); } @Override public CommonResult validSmsCode(String telephone, String smsCode) { //从redis缓存数据库中取出验证码 String realSms = redisService.get(REDIS_KEY_PREFIX_AUTH_CODE + telephone); if (StringUtils.isEmpty(realSms)) { return CommonResult.failed("验证码已过期"); } if (smsCode.equals(realSms)) { return CommonResult.success("校验成功"); } return CommonResult.failed("输入验证码不正确"); } }
8、测试接口获取手机验证码
手机验证码为862061
9、测试接口校验手机验证码
输入正确的验证码返回
输入错误的验证码返回
两分钟以后验证码过期,输入正确的验证码返回