目录
一、Jedis 依赖
二、Java 客户端操控 redis
2.1、准备工作(ssh 隧道)
2.2、概要
2.2、string
2.3、hash
2.4、list
2.5、set
2.5、zset
一、Jedis 依赖
自己去 中央仓库 上面找.
二、Java 客户端操控 redis
2.1、准备工作(ssh 隧道)
想要连接上云服务器上的 redis ,就需要开放 6379 端口,但是一旦开饭这个端口是十分危险的!!!(不出 3 天,你的服务器就会被黑客攻击)
我们有两种办法
- 将 java 程序打包成 jar 包,放到 linux 服务器上执行(过于麻烦,不推荐);
- 匹配 ssh 端口转发,把云服务器的 redis 端口,映射到本地主机(推荐).
因此我们来讲讲第二种办法~
我们在本地 windows 主机上,使用 xshell 连接远程服务器,主要就是通过 ssh 协议(默认走 22 端口)实现通信的,他有一个很重要的特性,就是支持端口转发! 我们只需要配置 ssh 程序监听本地端口,映射到云服务器的端口,就可以实现通过 windows 主机,访问云服务器的 6379 端口.
具体的,我们只需要再 xshell 上配置连接信息即可:
Ps:当配置了端口准发后,一定要断开之前的连接,重新连接才能生效.
2.2、概要
基于前面对 redis 指令的学习,这里使用 jedis 这个库来操控 redis。
Ps:一个优秀的库,一定是非常容易上手的,因此接下来介绍的操作,不会覆盖到所有指令,会挑选出一些重要 / 代表性的命令,来进行演示~
使用 jedis 操控 redis 类似于 JDBC ,我们可以先创建 连接池,然后拿到连接,执行完操作后再释放连接,因此后续讲的所有操作,都是基于以下代码:
public static void main(String[] args) {//这里只是开发阶段这么写,要是部署到云服务器上,就需要根据实际情况来配置这个的 ip 和端口号.JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");//使用 try,出代码块时自动释放 jedis 连接(不是关闭,而是放回到连接池中)try(Jedis jedis = jedisPool.getResource()) {// 测试场景:// ......test1(jedis);}}
2.2、string
public class JedisString {private static void test1(Jedis jedis) {System.out.println("------------------------------------");System.out.println("mget 和 mset");//这里为了演示效果,使用之前会先释放所有 keyjedis.flushAll();jedis.mset("key1", "value1", "key2", "value2", "key3", "value3");List<String> values = jedis.mget("key1", "key2", "key3", "key100");System.out.println(values);}private static void test2(Jedis jedis) {System.out.println("------------------------------------");System.out.println("getrange 和 setrange");jedis.flushAll();jedis.set("key", "helloworld");String value1 = jedis.getrange("key", 2, 5);System.out.println("value1: " + value1);jedis.setrange("key", 2, "cykkk");String value2 = jedis.get("key");System.out.println("value2: " + value2);}private static void test3(Jedis jedis) {System.out.println("------------------------------------");System.out.println("append");jedis.flushAll();jedis.set("key1", "hello");jedis.append("key1", " world");String value1 = jedis.get("key1");System.out.println("value1: " + value1);}private static void test4(Jedis jedis) {System.out.println("------------------------------------");System.out.println("incr 和 decr");jedis.flushAll();jedis.set("key1", "10");jedis.incr("key1");System.out.println("inct key1: " + jedis.get("key1"));jedis.set("key1", "10");jedis.decr("key1");System.out.println("decr key2: " + jedis.get("key1"));}public static void main(String[] args) {//这里只是开发阶段这么写,要是部署到云服务器上,就需要根据实际情况来配置这个的 ip 和端口号.JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");//使用 try,出代码块时自动释放 jedis 连接(不是关闭,而是放回到连接池中)try(Jedis jedis = jedisPool.getResource()) {// 测试场景:// ......test1(jedis);test2(jedis);test3(jedis);test4(jedis);}}}
2.3、hash
public class JedisHash {private static void test1(Jedis jedis) {System.out.println("-----------------------------------");System.out.println("hset 和 hget");jedis.flushAll();jedis.hset("key1", "f1", "v1");String result = jedis.hget("key1", "f1");System.out.println("result: " + result);Map<String, String> map = new HashMap<>();map.put("f1", "v1");map.put("f2", "v2");map.put("f3", "v3");jedis.hset("key2", map);System.out.println("key2: " + jedis.hgetAll("key2"));}private static void test2(Jedis jedis) {System.out.println("-----------------------------------");System.out.println("hexists");jedis.flushAll();jedis.hset("key1", "f1", "v1");boolean result1 = jedis.hexists("key1", "f1");boolean result2 = jedis.hexists("key1", "f2");System.out.println("result1: " + result1);System.out.println("result2: " + result2);}private static void test3(Jedis jedis) {System.out.println("-----------------------------------");System.out.println("hdel");jedis.flushAll();jedis.hset("key1", "f1", "v1");jedis.hset("key1", "f2", "v2");jedis.hset("key1", "f3", "v3");long result = jedis.hdel("key1", "f1");System.out.println("result: " + result);System.out.println("f1: " + jedis.hget("key1", "f1"));System.out.println("f2: " + jedis.hget("key1", "f2"));System.out.println("f3: " + jedis.hget("key1", "f3"));}private static void test4(Jedis jedis) {System.out.println("-----------------------------------");System.out.println("hkeys 和 hvals");jedis.flushAll();Map<String, String> map = new HashMap<>();map.put("f1", "v1");map.put("f2", "v2");map.put("f3", "v3");jedis.hmset("key1", map);Set<String> set = jedis.hkeys("key1");List<String> list = jedis.hvals("key1");System.out.println("key1 -> field:" + set);System.out.println("key2 -> value:" + list);}public static void main(String[] args) {JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");try(Jedis jedis = jedisPool.getResource()) {test1(jedis);test2(jedis);test3(jedis);test4(jedis);}}}
2.4、list
public class JedisList {private static void test1(Jedis jedis) {System.out.println("---------------------------");System.out.println("lpush 和 lrange");jedis.flushAll();jedis.lpush("key1", "value1", "value2", "value3");List<String> result = jedis.lrange("key1", 0 ,-1);System.out.println(result);}private static void test2(Jedis jedis) {System.out.println("---------------------------");System.out.println("rpush");jedis.flushAll();jedis.rpush("key1", "value1", "value2", "value3");List<String> result = jedis.lrange("key1", 0 ,-1);System.out.println(result);}private static void test3(Jedis jedis) throws InterruptedException {System.out.println("---------------------------");System.out.println("blpop");jedis.flushAll();List<String> result = jedis.blpop(100, "key1");System.out.println(result.get(0));System.out.println(result.get(1));}public static void main(String[] args) throws InterruptedException {JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");try(Jedis jedis = jedisPool.getResource()) {test1(jedis);test2(jedis);test3(jedis);}}}
2.5、set
public class JedisSet {private static void test1(Jedis jedis) {System.out.println("-------------------------");System.out.println("sadd 和 smembers");jedis.flushAll();jedis.sadd("key1", "m1", "m2", "m3");Set<String> set = jedis.smembers("key1");System.out.println(set);}private static void test2(Jedis jedis) {System.out.println("-------------------------");System.out.println("sismember");jedis.flushAll();jedis.sadd("key1", "m1", "m2", "m3");boolean result1 = jedis.sismember("key1", "m2");boolean result2 = jedis.sismember("key1", "m100");System.out.println("result1: " + result1);System.out.println("result2: " + result2);}private static void test3(Jedis jedis) {System.out.println("-------------------------");System.out.println("scard");jedis.flushAll();jedis.sadd("key1", "m1", "m1", "m3");long result = jedis.scard("key1");System.out.println("result: " + result);}private static void test4(Jedis jedis) {System.out.println("-------------------------");System.out.println("spop");jedis.flushAll();jedis.sadd("key1", "m1", "m1", "m3");String result = jedis.spop("key1");System.out.println("result: " + result);}private static void test5(Jedis jedis) {System.out.println("-------------------------");System.out.println("sinter");jedis.flushAll();jedis.sadd("key1", "m1", "m2", "m3");jedis.sadd("key2", "m2", "m3", "m4");Set<String> set = jedis.sinter("key1", "key2");System.out.println(set);}private static void test6(Jedis jedis) {System.out.println("-------------------------");System.out.println("sinter");jedis.flushAll();jedis.sadd("key1", "m1", "m2", "m3");jedis.sadd("key2", "m2", "m3", "m4");long len = jedis.sinterstore("keyStore", "key1", "key2");System.out.println("len: " + len);System.out.println("keyStore: " + jedis.smembers("keyStore"));}public static void main(String[] args) {JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");try(Jedis jedis = jedisPool.getResource()) {test1(jedis);test2(jedis);test3(jedis);test4(jedis);test5(jedis);test6(jedis);}}}
2.5、zset
public class JedisZset {private static void test1(Jedis jedis) {System.out.println("-----------------------------");System.out.println("zadd 和 zrange");jedis.flushAll();jedis.zadd("key1", 10, "aaa");Map<String, Double> map = new HashMap<>();map.put("bbb", 20.0);map.put("ccc", 30.0);jedis.zadd("key1", map);List<String> list = jedis.zrange("key1", 0 ,-1);System.out.println(list);}private static void test2(Jedis jedis) {System.out.println("-----------------------------");System.out.println("zcard");jedis.flushAll();jedis.zadd("key1", 10, "aaa");jedis.zadd("key1", 20, "bbb");jedis.zadd("key1", 30, "ccc");long len = jedis.zcard("key1");System.out.println(len);}private static void test3(Jedis jedis) {System.out.println("-----------------------------");System.out.println("zrem");jedis.flushAll();jedis.zadd("key1", 10, "aaa");jedis.zadd("key1", 20, "bbb");jedis.zadd("key1", 30, "ccc");long count = jedis.zrem("key1", "aaa", "bbb", "xxx");System.out.println(count);}private static void test4(Jedis jedis) {System.out.println("-----------------------------");System.out.println("zscore 和 zrank");jedis.flushAll();jedis.zadd("key1", 10, "aaa");jedis.zadd("key1", 20, "bbb");jedis.zadd("key1", 30, "ccc");Double score = jedis.zscore("key1", "bbb");Long rank = jedis.zrank("key1", "bbb");System.out.println("score: " + score);System.out.println("rank: " + rank);}public static void main(String[] args) {JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");try(Jedis jedis = jedisPool.getResource()) {test1(jedis);test2(jedis);test3(jedis);test4(jedis);}}}