三分钟学会一个新技能——使用Java操作Redis

目录

1、前置准备操作

1.1、为什么要进行前置准备操作 

1.2、本地如何访问到云服务上Redis的6379端口号

1.3、配置步骤:

2、配置后本地主机如何操作 

3、常用命令举例

3.1、通用命令举例

3.2、string相关命令举例

3.3、hash相关命令举例

3.4、list相关命令举例

3.5、set相关命令举例

3.6、zset相关命令举例


1、前置准备操作

1.1、为什么要进行前置准备操作 

        我们之前学习Redis命令时,直接在Linux服务器中所提供的redis客户端上使用的,也就是说Redis服务器和客户端在同一台主机上。而现在我们使用Java去操作Redis,一般Java开发都是在Windows上,也就是在自己的电脑上的,就会导致一个问题,客户端和服务器不在同一台主机上。        

        对于这种问题:

  • 第一,我们连接Redis服务器时要修改为外网IP
  • 第二,Redis的默认端口是6379,这个端口是默认会被云服务器的防火墙保护起来,不能从外部进行访问。这里又会导致一个问题,我们自己也访问不了了~

这种情况下,切记可不敢去云服务器上把这个端口开放哈!!!

因为太容易被攻击了~

这个情况,我们有两个解决办法~

1.2、本地如何访问到云服务上Redis的6379端口号

方案一:

        就使用6379端口,但不能在本地运行,将Java程序编写成功后,打成jar部署到云服务器上来运行。这种方法,就会有些复杂,需要我们频繁地打包-部署,太麻烦了~

方案二:【推荐】

        使用SSH来解决——配置SSH端口转发,把云服务器的Redis端口,映射到本地主机~

        例如,我们配置的本地主机端口号:6666 

        当本地主机访问6666端口时【SSH程序是一直在监听6666这个端口,SSH默认端口号是22】,会通过ssh的22端口来将6666的端口数据发送到云服务上,配置时也会让你选组云服务器的端口号,正常情况下,你就选择redis的默认端口6379即可。ssh就会将本地主机端口号6666数据转发到云服务器上的端口号6379的程序~

1.3、配置步骤:

我这里是使用xshell举例:

在此之前,进入Redis自带的客户端,在配置中,修改Redis的绑定的ip和关闭保护模式,具体操作可看我以前的博客:http://t.csdn.cn/OdKZJ

(1):连接回话后,点击属性选项

(2):

(3):

(4):检查是否连接:

看到这句话即可~


2、配置后本地主机如何操作 

第一步:新建一个Maven项目

第二步:去中央仓库搜索jedis,选择【例如我选择的是jedis4.4.2,选稳定的版本~】:

    <!-- https://mvnrepository.com/artifact/redis.clients/jedis --><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>4.4.2</version></dependency>

把这段代码放入到pom.xml中~

第三步:新建一个Java文件:

贴上代码:

import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool;/*** Created with IntelliJ IDEA.* Description:* User:龙宝* Date:2023-09-14* Time:14:28*/ public class redisPing {public static void main(String[] args) {JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:6666");try(Jedis jedis = jedisPool.getResource()) {testPint(jedis);}}private static void testPint(Jedis jedis) {String result = jedis.ping();System.out.println("result:" + result);} } 

 代码解释:

  • jedispool就类似于与线程池,它属于redis的一个连接池,后面的参数,我们能看到是tcp,也就是我们建立的是tcp连接,后面的6666就是我们刚才指定的端口~
  • try后面的括号中,则是从redis连接池中取出一个连接来使用
  • 连接使用完成后需要释放这个连接,但这里的释放并不是说要断开tcp,而是将其放回到池子中~
  • try花括号中便可以开始操作Redis了~
  • 下面这个方法,则是redis中的命令ping,输入ping,返回pong
  • 执行程序,查看结果:

        补充说明:为什么是使用jedis,使用其他工具当然也可以,但jedis中提供的命令几乎和redis的原生命令一样,降低了学习成本~ 


3、常用命令举例

        没有Redis基础的小伙伴可以看看我之前的博客,稍微了解一下Redis中常用的数据类型的常用的命令~ 更有助于后的学习~ 

3.1、通用命令举例

import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool;import java.util.Set;/*** Created with IntelliJ IDEA.* Description:* User:龙宝* Date:2023-09-14* Time:14:18*/ public class redisCommonDemo {public static void main(String[] args) throws InterruptedException {JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:6666");try(Jedis jedis = jedisPool.getResource()) {System.out.println("set/get举例:");commonDemo1(jedis);System.out.println("exists举例:");commonDemo2(jedis);System.out.println("del举例:");commondemo3(jedis);System.out.println("keys举例:");commondemo4(jedis);System.out.println("expire/ttl举例:");commondemo5(jedis);System.out.println("type举例:");commondemo6(jedis);}}private static void commondemo6(Jedis jedis) {jedis.flushAll();jedis.set("key1","1");String res = jedis.type("key1");System.out.println("key1的类型:" + res);res = jedis.objectEncoding("key1");System.out.println("key1的内部编码:" + res);}private static void commondemo5(Jedis jedis) throws InterruptedException {jedis.flushAll();jedis.set("key1","1");jedis.expire("key1",100);Thread.sleep(1000);Long res = jedis.ttl("key1");System.out.println("剩余时长:" + res);}private static void commondemo4(Jedis jedis) {jedis.flushAll();//删除,避免干扰jedis.mset("key1","1","key2","2","key3","3");Set<String> res = jedis.keys("*");System.out.println(res);}private static void commondemo3(Jedis jedis) {Long res = jedis.del("key1");System.out.println("成功删除key的个数:" + res);Boolean result = jedis.exists("key1");System.out.println("key1已经不存在了:" +result);}private static void commonDemo2(Jedis jedis) {Boolean res = jedis.exists("key1");System.out.println("res存在:" + res);res = jedis.exists("key10000");System.out.println("res不存在:"+ res);}private static void commonDemo1(Jedis jedis) {//先清空,避免干扰//jedis.flushAll();String result = jedis.set("key1","111");System.out.println("result:" + result);String res = jedis.get("key1");System.out.println("key1-value:"+res);} } 

小技巧,你不知返回值有什么来接收时,将鼠标放在函数上:

结果:

3.2、string相关命令举例

import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool;import java.util.List; import java.util.Set;/*** Created with IntelliJ IDEA.* Description:* User:龙宝* Date:2023-09-14* Time:15:36*/ public class redisStringDemo {public static void main(String[] args) {JedisPool jedispool = new JedisPool("tcp://127.0.0.1:6666");try(Jedis jedis = jedispool.getResource()) {System.out.println("get/set举例");stringDemo1(jedis);System.out.println("mget/mset举例:");stringDemo2(jedis);System.out.println("getrange/setrange");stringdemo3(jedis);System.out.println("append举例:");stringDemo4(jedis);System.out.println("incr举例:");stringDemo5(jedis);}}private static void stringDemo5(Jedis jedis) {Long res = jedis.incr("key2");System.out.println("key2自增后:" + res);}private static void stringDemo4(Jedis jedis) {Long result = jedis.append("key1","xxxxx");System.out.println("append的返回值" + result);String res = jedis.getrange("key1",0,-1);System.out.println("key1:" + res);}private static void stringdemo3(Jedis jedis) {//先不删除,使用上一个方法遗留的key1 2 3//jedis.flushAll();String res = jedis.getrange("key1",0,-1);System.out.println("keys:" + res);Long result = jedis.setrange("key1",1,"hhhhhh");System.out.println("setrange的返回值:" + result);res = jedis.getrange("key1",0,-1);System.out.println("keys:" + res);}private static void stringDemo2(Jedis jedis) {jedis.flushAll();String res = jedis.mset("key1","1","key2","2","key3","3");System.out.println("新增是否成功:" + res);//取出当中key,检查对应值是否正确List<String> result = jedis.mget("key1","key2","key3","key4");System.out.println("result" + result);}private static void stringDemo1(Jedis jedis) {//先清空,避免干扰jedis.flushAll();String result = jedis.set("key1","111");System.out.println("result:" + result);String res = jedis.get("key1");System.out.println("key1-value:"+res);} } 

结果:

3.3、hash相关命令举例

import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool;import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set;/*** Created with IntelliJ IDEA.* Description:* User:龙宝* Date:2023-09-14* Time:15:57*/ public class redisHashDemo {public static void main(String[] args) {JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:6666");try(Jedis jedis = jedisPool.getResource()) {System.out.println("hset/hget举例:");hashDemo1(jedis);System.out.println("hexists举例:");hashDemo2(jedis);System.out.println("hdel举例:");hashDemo3(jedis);System.out.println("hkeys/hvals");hashDemo4(jedis);}}private static void hashDemo4(Jedis jedis) {jedis.flushAll();jedis.hset("key1","f1","111");jedis.hset("key1","f2","222");jedis.hset("key2","f1","333");Set<String> res = jedis.hkeys("key1");System.out.println("hkeys的返回值:" + res);List<String> list = jedis.hvals("key1");System.out.println("hvals的返回值:" + list);}private static void hashDemo3(Jedis jedis) {Long res = jedis.hdel("key1","f1");System.out.println("hdel的返回值:" + res);}private static void hashDemo2(Jedis jedis) {Boolean res = jedis.hexists("key1","f1");System.out.println("key1的f1存在:" + res);}private static void hashDemo1(Jedis jedis) {jedis.flushAll();Long res = jedis.hset("key1","f1","111");System.out.println("hset的返回值:" + res);String result = jedis.hget("key1","f1");System.out.println("key1的f1对应的value值:" + result);} } 

结果:

3.4、list相关命令举例

import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import sun.rmi.runtime.Log;import java.util.List;/*** Created with IntelliJ IDEA.* Description:* User:龙宝* Date:2023-09-14* Time:16:26*/ public class rediListDemo {public static void main(String[] args) {JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:6666");try(Jedis jedis = jedisPool.getResource()) {System.out.println("lpsuh/rpush举例:");listDemo1(jedis);System.out.println("lrange举例:");listDemo2(jedis);System.out.println("rpop/lpop举例:");listDemo3(jedis);System.out.println("blpop/brpop举例:");listDemo4(jedis);System.out.println("llen举例:");listDemo5(jedis);}}private static void listDemo5(Jedis jedis) {Long res = jedis.llen("key1");System.out.println("key1此时的长度:" + res);}private static void listDemo4(Jedis jedis) {//这里具体使用可以看我以前的文章,这里不详细阐述List<String> res = jedis.blpop(1000,"key1","key2");System.out.println("key左:" + res);res = jedis.brpop(1000,"key1","key2");System.out.println("key右:" + res);}private static void listDemo3(Jedis jedis) {//继续使用第一个方法的残留String res = jedis.lpop("key1");System.out.println("key1左侧弹出:" + res);res = jedis.rpop("key1");System.out.println("key2右侧弹出:" + res);}private static void listDemo2(Jedis jedis) {//使用上一个方法中新增的list//1 2 3 4是lpush,5 6 7 是rpush 因此列表的顺序为:4 3 2 1 5 6 7List<String> res = jedis.lrange("key1",0,-1);System.out.println("key1:" + res);}private static void listDemo1(Jedis jedis) {//  清理数据jedis.flushAll();Long res = jedis.lpush("key1","1","2","3","4");System.out.println("lpush的返回值:" + res);res = jedis.rpush("key1","5","6","7");System.out.println("rpush的返回值:" + res);} } 

结果:

3.5、set相关命令举例

import com.sun.org.apache.xpath.internal.operations.Bool; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool;import java.util.Set; import java.util.Stack;/*** Created with IntelliJ IDEA.* Description:* User:龙宝* Date:2023-09-14* Time:22:18*/ public class redisSetDemo {public static void main(String[] args) {JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:6666");try(Jedis jedis = jedisPool.getResource()) {System.out.println("sadd/smembers举例");setDemo1(jedis);System.out.println("sismember举例:");setDemo2(jedis);System.out.println("scard举例:");setDemo3(jedis);System.out.println("spop举例:");setDemo4(jedis);System.out.println("sinter/sinterstore举例:");setDemo5(jedis);}}private static void setDemo5(Jedis jedis) {//取交集//清理数据jedis.flushAll();jedis.sadd("key1","1","2","3");jedis.sadd("key2","1","2","4");Set<String> res = jedis.sinter("key1","key2");System.out.println("交集:" + res);jedis.sinterstore("key","key1","key2");res = jedis.smembers("key");System.out.println("交集"+ res);}private static void setDemo4(Jedis jedis) {//随机删除一个值String res = jedis.spop("key1");System.out.println("key1中删除:" + res);}private static void setDemo3(Jedis jedis) {//获取长度Long res = jedis.scard("key1");System.out.println("key1长度:" + res);}private static void setDemo2(Jedis jedis) {Boolean res = jedis.sismember("key1","2");System.out.println("key1中存在2:" + res);res = jedis.sismember("key1","100");System.out.println("key1中存在100:" + res);}private static void setDemo1(Jedis jedis) {//清除数据jedis.flushAll();Long res = jedis.sadd("key1","1","2","3","4");System.out.println("sadd的返回值:" + res);Set<String> result = jedis.smembers("key1");System.out.println("key1:" + result);} } 

结果:

3.6、zset相关命令举例

import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool;import java.util.HashMap; import java.util.List; import java.util.Map;/*** Created with IntelliJ IDEA.* Description:* User:龙宝* Date:2023-09-14* Time:22:42*/ public class redisZsetDemo {public static void main(String[] args) {JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:6666");try(Jedis jedis = jedisPool.getResource()) {System.out.println("zadd/zrange举例:");zsetDemo1(jedis);System.out.println("zcard举例:");zsetDemo2(jedis);System.out.println("zrem举例:");zsetDemo3(jedis);System.out.println("zscore举例:");zsetDemo4(jedis);System.out.println("zrank举例:");zsetDemo5(jedis);}}private static void zsetDemo5(Jedis jedis) {//zrank:jedis.flushAll();Map<String,Double> map = new HashMap<>();map.put("zhangsan",100.0);map.put("lisi",95.0);map.put("wangwu",90.0);Long res = jedis.zadd("key1",map);Long result = jedis.zrank("key1","lisi");System.out.println("key1中lisi的排名:" + result);}private static void zsetDemo4(Jedis jedis) {//zscore:Double res = jedis.zscore("key1","lisi");System.out.println("key1的lisi的分数:" + res);}private static void zsetDemo3(Jedis jedis) {//zrem:Long res = jedis.zrem("key1","zhangsan");System.out.println("key1删除成功?" + res);}private static void zsetDemo2(Jedis jedis) {//zcard:Long res = jedis.zcard("key1");System.out.println("key1长度:" + res);}private static void zsetDemo1(Jedis jedis) {//清理数据jedis.flushAll();Map<String,Double> map = new HashMap<>();map.put("zhangsan",100.0);map.put("lisi",95.0);Long res = jedis.zadd("key1",map);System.out.println("zadd的返回值:" + res);List<String> list = jedis.zrange("key1",0,-1);System.out.println("key1:" + list);} } 

结果:

先介绍这些啦,下期见~ 

赞(0)
分享到

相关推荐