}
//加入多个key和value
jedis.mset(“name1”,“zs”,“name2”,“ls”,“name3”,“ww”);
List mget = jedis.mget(“name1”, “name2”);
System.out.println(mget);//[zs, ls]
}
//list
private static void test02(){
Jedis jedis = new Jedis(“192.168.194.131”, 6379);
jedis.lpush(“key1”,“01”,“02”,“03”);
List values = jedis.lrange(“key1”,0,-1);
System.out.println(values);//[03, 02, 01]
}
//set
private static void test03(){
Jedis jedis = new Jedis(“192.168.194.131”, 6379);
jedis.sadd(“username”,“zs”,“ls”,“ww”);
Set names = jedis.smembers(“username”);
System.out.println(names);//[ww, zs, ls]
}
//hash
private static void test04(){
Jedis jedis = new Jedis(“192.168.194.131”, 6379);
jedis.hset(“users”,“age”, “20”);
String hget = jedis.hget(“users”,“age”);
System.out.println(hget);
}
//zset
private static void test05(){
Jedis jedis = new Jedis(“192.168.194.131”, 6379);
jedis.zadd(“china”,100d,“shanghai”);
Set names = jedis.zrange(“china”,0,-1);
System.out.println(names);//[shanghai]
}
}
六、手机验证码功能代码实例
package com.guor.redis;
import redis.clients.jedis.Jedis;
import java.util.Random;
public class PhoneCode {
public static void main(String[] args) {
verifyCode(“10086”);//795258
getRedisCode(“10086”,“795258”);//success.
}
//1、生成6位数字验证码
public static String getCode(){
Random random = new Random();
String code = “”;
for (int i = 0; i < 6; i++) {
int rand = random.nextInt(10);
code += rand;
}
return code;//849130
}
//2、每个手机每天只能发送三次,验证码放到redis中,设置过期时间
public static void verifyCode(String phone){
Jedis jedis = new Jedis(“192.168.194.131”, 6379);
//拼接key
//手机发送次数key
String countKey = “VerifyCode” + phone + “:count”;
//验证码key
String codeKey = “VerifyCode” + phone + “:code”;
//每个手机每天只能发送三次
String count = jedis.get(countKey);
if(count == null){
//设置过期时间
jedis.setex(countKey,246060,“1”);
}else if(Integer.parseInt(count)<=2){
//发送次数+1
jedis.incr(countKey);
}else if(Integer.parseInt(count)>2){
System.out.println(“今天的发送次数已经超过三次”);
jedis.close();
}
String vCode = getCode();
jedis.setex(codeKey,120,vCode);
jedis.close();
}
//3、验证码校验
public static void getRedisCode(String phone, String code){
//从redis中获取验证码
Jedis jedis = new Jedis(“192.168.194.131”, 6379);
//验证码key
String codeKey = “VerifyCode” + phone + “:code”;
String redisCode = jedis.get(codeKey);
if(redisCode.equals(code)){
System.out.println(“success.”);
}else{
System.out.println(“error”);
}
jedis.close();
}
}
当超过三次时:
七、SpringBoot整合Redis
1、建工程,引入pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=“http://maven.apache.org/POM/4.0.0” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd”>
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.2.1.RELEASE
com.guor
redisspringboot
0.0.1-SNAPSHOT
redisspringboot
Demo project for Spring Boot
<java.version>1.8</java.version>
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-data-redis
2.4.5
org.apache.commons
commons-pool2
2.9.0
org.springframework.boot
spring-boot-maven-plugin
2、配置类
(1)application.properties
Redis数据库索引(默认为0)
spring.redis.database=0
Redis服务器地址
spring.redis.host=192.168.194.131
Redis服务器连接端口
spring.redis.port=6379
Redis服务器连接密码(默认为空)
spring.redis.password=
连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=20
连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1
连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=10
连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
连接超时时间(毫秒)
spring.redis.timeout=1000
(2)RedisConfig