一、在虚拟机安装好redis
我安装的目录是/usr/local
bind 0.0.0.0 #修改bind后面的ip为这个 protected-mode no #protected-mode修改yes为no
三、启动redis
进入bin目录(/usr/local/redis/bin
./redis-server /usr/local/redis/redis.conf
四、查看redis是否启动
自己有的工具RedisDesktopManage
五、后端项目配置好
我的后端项目为boot,利用redis来存短信验证码
1. 依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> </dependency>
2. 配置文件application.yml
spring: redis: host: 192.168.234.100 #需要改成自己的虚拟机地址 port: 6379 lettuce: pool: max-active: 20 #最大连接数,负值表示没有限制,默认8 max-wait: -1 #最大阻塞等待时间,负值表示没限制,默认-1 max-idle: 8 #最大空闲连接,默认8 min-idle: 0 #最小空闲连接,默认0
3.使用
3.1 在需要使用redis服务的代码块,利用spring的自动装配
@Autowired Redistemplate redistemplate;
除了Redistemplate还有StringRedistemplate类,专门针对字符串。StringRedistemplate不能取到通过Redistemplate存入的数据
3.2 常用方法
通过key获取value=> redistemplate.opsForValue().get(codeKey);
通过key删除=> redistemplate.delete(codeKey);
增添数据到redis中=> redistemplate.opsForValue().set(key, value ,number , TimeUnit.MINUTES);//可以换成别的单位,这是过期时间
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。