百度360必应搜狗淘宝本站头条
当前位置:网站首页 > IT技术 > 正文

在Java中使用Redis的方法(java中如何使用redis)

wptr33 2025-07-19 23:05 4 浏览

前提

可供访问的Redis服务器 可以自己在本地启动虚拟机

如何在本地启动一个Redis参考bilibili尚硅谷Redis6

SpringBoot项目中需要添加的依赖

<dependency>

<groupId>redis.clients</groupId>

<artifactId>jedis</artifactId>

<version>3.2.0</version>

</dependency>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>RELEASE</version>

<scope>compile</scope>

</dependency>

常见的用法

工具类 用于获取Redis连接

public class JedisUtils {

public static Jedis getJedisClient(){

Jedis jedis = new Jedis("192.168.110.101",6379);

return jedis;

}

}

检测本地Redis是否可以使用

@Test

public void pingTest(String[] args) {

Jedis jedis = new Jedis("192.168.110.101",6379);

String ping = jedis.ping();

// 当Redis能使用时 会输出pong

System.out.println(ping);

}

关于Redis String 类型的API

@Test

public void StringTest(){

Jedis jedis = new Jedis("192.168.110.101",6379);

jedis.set("name","lucy");

//redis 批量添加多个k v ?

jedis.mset("k1","v1","k2","v2");

List<String> mget = jedis.mget("k1", "k2");

String name = jedis.get("name");

System.out.println(name);

}

关于Jedis set 类型API

@Test

public void setTest(){

//操作set 集合

Jedis jedisClient = JedisUtils.getJedisClient();

//set中添加元素

jedisClient.sadd("name1","lucy","mary","jack");

Set<String> name = jedisClient.smembers("name1");

//set中删除元素

jedisClient.srem("name1","lucy");

Set<String> name1 = jedisClient.smembers("name1");

System.out.println(name1);

System.out.println(name);

}

关于Jedis hash类型的API

@Test

public void hashTest(){

// hash的两种添加值方式以及取值方式

Jedis jedisClient = JedisUtils.getJedisClient();

jedisClient.hset("hset","lucy","20");

String hget = jedisClient.hget("hset", "lucy");

System.out.println(hget);

Map<String,String> hashTsetMap = new HashMap<>(16);

hashTsetMap.put("jack","30");

jedisClient.hset("hset",hashTsetMap);

List<String> age = jedisClient.hmget("hset","lucy");

System.out.println(age);

jedisClient.hdel("hset","lucy");

String lucy = jedisClient.hget("hset", "lucy");

System.out.println(lucy);

}

zSet类型API

@Test

public void zSetTest(){

Jedis jedisClient = JedisUtils.getJedisClient();

jedisClient.zadd("china",100d,"shanghai");

Set<String> china = jedisClient.zrange("china", 0, -1);

System.out.println(china);

}

使用Redis实现一个简易版短信注册功能

/**

* 注册功能

* 生成手机验证码 且五分钟内有效

* 1.连接redis

* 2.判断验证码生成的次数

* 3.生成验证码 调用短信发送API

* 4.前台回传验证码 ,校验验证码的有效性

* 5.注册成功

* @param args

*/

//

public void getMessage() {

try (Jedis jedisClient=JedisUtils.getJedisClient()){

String phone = "12345678";

if (getGenerateTimes(phone,jedisClient)){

String verificationCode = getVerificationCode(phone, jedisClient);

System.out.println(verificationCode);

}else {

System.out.println("超过短信次数");

}

} catch (Exception e) {

e.printStackTrace();

}

}

public static final String KEY = "generateTime";

public static final Integer Max_TIME = 2;

public static final Integer MINI_TIME = 0;

// 计算一天发送短信的次数 不能超过三次

public static boolean getGenerateTimes(String phone, Jedis client){

String times = client.get(phone+KEY);

if (times==null){

client.setex(phone+KEY, 24*60*60,MINI_TIME.toString());

}

// 使用Interger.valueOf不能转换?

//基本类的包装类无法自动拆箱进行相互比较

if (Integer.parseInt(times)>Max_TIME.intValue()){

return false;

}

client.incr(phone+KEY);

return true;

}

/**

* 生成六位数验证码 调用api发送短信到手机

* @param phone

* @param client

* @return

*/

public static String getVerificationCode(String phone, Jedis client){

Random random = new Random();

StringBuffer stringBuffer = new StringBuffer();

// 有更好的方法生成六位随机数

for (int j = 0; j < 6; j++) {

stringBuffer.append(random.nextInt(10));

}

// 调用短信API发送 并做对应的业务判断

// 发送短信成功 将数据放入redis 并设置过期时间为五分钟

String setex = client.setex(phone, 300, stringBuffer.toString());

return stringBuffer.toString();

}

public static Boolean verification(String phone, String code, Jedis client){

String storeCode = client.get(phone);

if (code!=null&&code.equals(storeCode)){

return Boolean.TRUE;

}

return Boolean.FALSE;

}

@Test

public void verificationTest(){

try(Jedis jedisClient = JedisUtils.getJedisClient()) {

if (verification("12345678","465481",jedisClient)){

System.out.println("校验成功");

}else {

System.out.println("校验失败");

}

}

}

相关推荐

面试官:MySQL的自增ID用完了,怎么办?

来自:Java技术驿站既然这块知识点不清楚,那回头就自己动手实践下。首先,创建一个最简单的表,只包含一个自增id,并插入一条数据。create table t0(id i...

SQL 开发必学:深度解析 NULL 值处理的 6 大核心规则与避坑指南

在数据库开发中,NULL值处理是极易引发逻辑错误的技术难点。本文从SQL标准规范出发,系统梳理NULL值的底层逻辑与工程实践要点,帮助开发者建立完整的NULL值处理知识体系。一、三值逻辑...

SQL查找是否&quot;存在&quot;,别再用count了

根据某一条件从数据库表中查询『有』与『没有』,只有两种状态,那为什么在写SQL的时候,还要SELECTCOUNT(*)呢?无论是刚入道的程序员新星,还是精湛沙场多年的程序员老白,都是一如既往...

一文带你掌握shell脚本中的if条件语句,轻松搞定工作需求

#shell编程##linux#...

一文搞懂MySQL的左、右、内、外连接

一、前言1、MySQL中的左连接...

性能测试:Mysql中的空值陷阱(mysql中空值怎么表示)

SQL是一种声明式的语言,我们只需要描述想要的结果(WHAT),而不关心数据库如何实现(HOW);虽然SQL比较容易学习,但是仍然有一些容易混淆和出错的概念。今天我们就来说说SQL中的空值陷阱...

MySQL--常用函数(MySQL常用函数汇总)

介绍MySQL函数,是一种控制流程函数,属于数据库用语言。MySQL数据库中提供了很丰富的函数。MySQL函数包括数学函数、字符串函数、日期和时间函数、条件判断函数、系统信息函数、加密函数、格式化函数...

MySQL函数详解:IF()、IFNULL()、NULLIF()、ISNULL()、CASE

2025年3月27日,MySQL作为最流行的关系型数据库管理系统之一,其丰富的函数库为开发者提供了强大的数据处理能力。本文将详细解析MySQL中常用的条件判断函数:IF()、IFNULL()、NULL...

java迭代器iterator(java迭代器iterator增加一条记录)

/***iterator迭代器Collection接口继承了Iterable接口iterable可迭代的在Iterable接口中定义了iterator()方法用于生成迭代器...

说说Redis的数据类型(redis中的数据类型)

一句话总结Redis核心数据类型包括:String:存储文本、数字或二进制数据。List:双向链表,支持队列和栈操作。Hash:字段-值映射,适合存储对象。Set:无序唯一集合,支持交并差运算。...

一网打尽-HashMap面试题(hashmap数据结构面试)

全文4896字。读完五分钟,即可获得HashMap理解全部面经和原理。坚持就是胜利1、实现原理...

本地缓存GuavaCache(一)(本地缓存caffeine)

在并发量、吞吐量越来越大的情况下往往是离不开缓存的,使用缓存能减轻数据库的压力,临时存储数据。根据不同的场景选择不同的缓存,分布式缓存有Redis,Memcached、Tair、EVCache、Aer...

想月薪过万吗?计算机安卓开发之&quot;集合&quot;

集合的总结:/***Collection*List(存取有序,有索引,可以重复)*ArrayList*底层是数组实现的,线程不安全,查找和修改快,增和删比较慢...

Spring Boot 控制 Controller 接口的4种方式,哪种更适合你?

环境:SpringBoot3.4.2...

这些Java基础知识,诸佬们都还记得嘛(学习,复习,面试均可)

方法重载和方法重写的区别方法重写重写体现在继承关系上。在Java中,子类继承父类,子类就会具备父类所以的特征,以及父类的方法和变量比如动物类有“叫”的方法,小狗小猫分别继承了动物类,重写方法时就可以...