jedis的操作redis与redis和springboot整合

发布于 2021-04-22  878 次阅读


一,Jedis

Jedis是针对于java对redis操作的客户端工具,类似于jdbc用于mysql数据操作一样(但jdbc也可以操作其他SQL数据库)
Jedis测试:
1,导入pom依赖
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>3.2.0</version>
</dependency>
2,建立连接
连接对象分两种:
1,普通连接对象:new Jedis
2,集群连接对象:new jedisCluster(具体方法和普通连接对象差不多)
//创建jedis普通对象,建立连接
Jedis jedis =new Jedis("192.168.68.153",6379);
3,具体操作
jedis对象有所有的我们命令行操作的方式的方法,通过jedis调用进行,不详细说了
案例:模拟手机验证码发送
步骤:
  1. 输入手机号,点击发送后随机生成6位数的验证码,2分种有效
  2. 输入验证码,点击验证,返回成功或者失败
  3. 每一个手机号每天只能输入3次
 
分步解决:
  • 随机生成6位数的验证码:有java的random随机类实现
  • 验证码两分钟内有效:把验证码放入redis中并设置过期时间120秒
  • 判断验证码是否一致:把输入的验证码和redis中保存的验证码做比较
  • 每次手机每天只能发3次验证码:通过incr每次发送加1大于2的时候,提交不能发送
public class redis_photo {

static Jedis jedis =new Jedis("192.168.68.153",6379);

public static void main(String[] args) {

Boolean exit=exist_math("15526446827");

if(exit){

System.out.println("验证成功!");

}else {

System.out.println("验证失败!");
}
}


//随机产生6位数手机验证码

public static String getrandomath(){

Random random =new Random();

String randommath ="";

for(int i=0;i<=5;i++){

int math = random.nextInt(10);

randommath+=math;

}

return randommath;

}

//把验证码放入redis中

public static void into_redis(String photo){

String userphoto="userphoto:"+photo;

//判断数据库中的手机号是否发送验证码超过三次

if(jedis.get(userphoto)==null){//如果手机号值为空,说明它没有发送过验证码

jedis.set(userphoto,"0");//设置一个map,key为手机号,value为次数

}

if(Integer.parseInt(jedis.get(userphoto))>=2){//判断次数是否大于2

System.out.println("今天发送次数超过!");

jedis.close();
}

jedis.incr(userphoto);

String randommath = getrandomath();//获取验证码

jedis.setex(randommath,2*60,randommath);//设置验证码和过期时间
}

//判断验证码是否一致

public static boolean exist_math(String photo){

into_redis(photo);

Scanner scanner = new Scanner(System.in);

System.out.println("请输入验证码!!");

String math=scanner.next();

if(jedis.get(math)==math){

return true;

}

return false;
}

二,SpringBoot整合redis

第一步:导入pom依赖

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

<!--Springboot2.0x之后需要的common-pool连接池-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.0</version>
</dependency>

第二步:在springboot的application.yml中配置redis连接

#redis的服务器地址
spring.redis.host=192.168.68.153

#redis的服务器端口
spring.redis.port=6379

#redis的数据库索引(默认为0)
spring.redis.database=0

#连接超时时间
spring.redis.timeout=1800000

#连接池最大连接数(负数表示没有连接数)
spring.redis.lettuce.pool.max-active=20

#最大堵塞等待时间(负数表示没有限制)
spring.redis.lettuce.pool.max-wait=2

#连接池的最大空闲连接
spring.redis.lettuce.pool.max-idle=8

#连接池最小空闲连接
spring.redis.lettuce.pool.min-idle=0

第三步:redis自动配置类RedisAutoConfiguration

@Configuration(proxyBeanMethods = false)

@ConditionalOnClass(RedisOperations.class)

@EnableConfigurationProperties(RedisProperties.class)

@Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })

public class RedisAutoConfiguration {

@Bean
@ConditionalOnMissingBean(name = "redisTemplate")

public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)//redis调用的模板

throws UnknownHostException {

RedisTemplate<Object, Object> template = new RedisTemplate<>();

template.setConnectionFactory(redisConnectionFactory);

return template;

}

@Bean
@ConditionalOnMissingBean

public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory)//字符串操作调用的模板

throws UnknownHostException {

StringRedisTemplate template = new StringRedisTemplate();

template.setConnectionFactory(redisConnectionFactory);

return template;}}
 
redistemplate:springboot操作redis的模板类,针对于K-V形式
 
stringredistemplate:springboot操作redis的模板类,针对于String数据形式
 
redistemplate和stringredistemplate的区别:
  • redistemplate采用的是JDK的序列化策略,会把对象和字符串序列化存入redis中,取的时候在反序列化回来进行显示
  • stringredistemplate采用的是String的序列化策略,都已string类型保存和取出
区别用法:
  • 当你想直接保存对象或者其他序列化时,使用redistemplate,但他存储在redis中查看是不友好的,只有再进行反序列化才能正常显示
  • 当你想直接以字符串保存和取出,直接用stringredistemplate
注:用redistemplate取不存在序列化的数据时返回值为空

第四步:自定义redis配置类

@EnableCaching

@Configuration

public class redisconfig {

@Bean

//自定义redistemplate的序列化器

public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {

RedisTemplate<Object, Object> template = new RedisTemplate();

template.setConnectionFactory(redisConnectionFactory);

//使用GenericJackson2JsonRedisSerializer这个redis的序列化器代替JDK序列化器

GenericJackson2JsonRedisSerializer generic =new GenericJackson2JsonRedisSerializer();

//将generic设置进redis的默认序列化器中

template.setDefaultSerializer(generic);

return template;

}

@Bean

public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {

StringRedisTemplate template = new StringRedisTemplate();

template.setConnectionFactory(redisConnectionFactory);

return template;

}}
使用redis的GenericJackson2JsonRedisSerializer 序列化器替换默认JDK的序列化器

第五步:测试:注入redistemplate和stringredistemplate进行操作

常用方法:
opsForValue:操作string类型
opsForList:操作列表类型
opsForZset:操作有序集合
opsForSet:操作集合
opsForHash:操作hash
 
ops代表操作那种类型,具体方法和命令行指令是一样的,不详细说
@Autowired

RedisTemplate redisTemplate;

@Autowired

StringRedisTemplate stringRedisTemplate;

@DisplayName("Springboot和redis整合测试!")

@Test

void redis_test() {

String s = (String) redisTemplate.opsForValue().get("userphoto:15526446827");//获取不到

String s1 = stringRedisTemplate.opsForValue().get("userphoto:15526446827");//可以获取到

System.out.println(s);

System.out.println(s1);

}

路漫漫其修远兮,吾将上下而求索