在Java中使用Redis的方法

前提

  1. 可供访问的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>

常见的用法

  1. 工具类 用于获取Redis连接
public class JedisUtils {
	public static Jedis getJedisClient(){
		Jedis jedis = new Jedis("192.168.110.101",6379);
		return jedis;
	}
}
  1. 检测本地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);
	}
  1. 关于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);
	}
  1. 关于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);
	}
  1. 关于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);
	}
  1. 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("校验失败");
			}
		}
	}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值