目录
1.渐进式遍历
2.Java 操作 Redis 客户端
2.1 引入依赖
2.2 配置端口转发
2.3 连接Redis Server
3.基础操作
3.1 set 和 get
3.2 exists 和 del
3.3 keys
3.4 expire 和 ttl
3.5 type
4.字符串操作
4.1 mget 和 mset
4.2 append
4.3 getrange 和 setrange
4.4 incr 和 decr
5.列表操作
5.1 lpush 和 lpop 和 lrange
5.2 rpush 和 rpop
5.3 blpop 和 brpop
5.4 llen
6.哈希表操作
6.1 hset 和 hget
6.2 hexists 和 hdel
6.3 hkeys 和 hvals
7.集合操作
7.1 sadd 和 smembers
7.2 srem 和 sismember
7.3 scard 和 spop
7.4 sinter 和 sinterstore
8.有序集合操作
8.1 zadd 和 zrange
8.2 zrem 和 zcard
8.3 zscore 和 zrank
1.渐进式遍历
在 Redis 中,keys * 可以护球所有的 key,但是直接使用 keys * 获取键时可能会出现阻塞的问题,Redis 使用 scan 命令可以进行渐进式遍历键,每次 scan 命令的时间复杂度是O(1),但是如果想要完整的遍历所有键,就需要执行多次 scan
scan 命令渐进式遍历
1)首次 scan 从 0 开始
2)当 scan 返回的下次位置为 0 时,遍历结束
SCAN
以渐进式的方式进行键的遍历
语法:
SCAN cursor [MATCH pattern] [COUNT count] [TYPE type]
count:限制这一次遍历能够获取到多个元素,默认是10
时间复杂度:O(1)
返回值:下一次 scan 的 cursor 以及本次得到的键
此处的 count 只是给 Redis 服务器一个提示,写入的 count 和实际返回的 key 的个数不一定完全相同,但是不会差太多
除了 scan 以外,Redis 面向哈希类型、集合类型、有序类型分别提供了 hscan、sscan、zscan命令,用法和 scan 基本类似
注意:
渐进式遍历 scan 虽然解决了阻塞问题,但是如果在遍历期间键有所变化(增加、修改、删除),可能导致遍历时键的重复或者遗漏
2.Java 操作 Redis 客户端
2.1 引入依赖
Java 操作 redis 的客户端有很多,这里使用 jedis,创建 maven 项目,引入 jedis 依赖
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.3.2</version>
</dependency>
2.2 配置端口转发
在 xshell 中进行配置
1)右键云服务器的会话,选择属性
2)点击隧道—配置转移规则
3)使用该会话连接服务器
此时,访问本地的 8888 就相当于访问对应服务器的 6379
2.3 连接Redis Server
1)使用 JedisPool 描述 Redis 服务器的位置,使用 url 来表示
2)使用 getResource 和服务器建立连接
3)连接使用完毕需要使用 close 关闭,可以直接使用 try 自动关闭
4)通过 ping 检测连接是否正确建立
public class Redis {
public static void main(String[] args) {
//连接到 Redis 服务器
JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis = jedisPool.getResource()){
//执行 Redis 命令
String pong = jedis.ping();
System.out.println(pong);
}
}
}
此时正确的建立的连接
3.基础操作
3.1 set 和 get
public class Redis {
public static void test(Jedis jedis) {
System.out.println("set 和 get 的使用");
//清空数据库,避免上一组测试的残留数据影响下一组的数据
jedis.flushAll();
jedis.set("key1","111");
String value = jedis.get("key1");
System.out.println("value: " + value);
SetParams params = new SetParams();
params.ex(10);
//xx表示存在才设置
params.xx();
jedis.set("key1","222",params);
value = jedis.get("key1");
System.out.println("value: " + value);
value = jedis.get("key2");
System.out.println("value: " + value);
}
public static void main(String[] args) {
JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis = jedisPool.getResource()){
test(jedis);
}
}
}
3.2 exists 和 del
public class Redis {
public static void test2(Jedis jedis) {
System.out.println("exists 和 del 的使用");
jedis.flushAll();
jedis.mset("key1","111","key2","222");
boolean ret = jedis.exists("key1");
System.out.println("ret: " + ret);
ret = jedis.exists("key3");
System.out.println("ret: " + ret);
}
public static void main(String[] args) {
JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis = jedisPool.getResource()){
test2(jedis);
}
}
}
del 可以删除多个 key,返回值是删除 key 的个数
public class Redis {
public static void test2(Jedis jedis) {
System.out.println("exists 和 del 的使用");
jedis.flushAll();
jedis.mset("key1","111","key2","222");
long ret = jedis.del("key1","key2");
System.out.println("ret: " + ret);
}
public static void main(String[] args) {
JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis = jedisPool.getResource()){
test2(jedis);
}
}
}
3.3 keys
public class Redis {
public static void test3(Jedis jedis) {
System.out.println("keys 的使用");
jedis.flushAll();
jedis.mset("key1","111","key2","222");
Set<String> ret = jedis.keys("key1");
System.out.println("ret: " + ret);
ret = jedis.keys("*");
System.out.println("ret: " + ret);
ret = jedis.keys("key3");
System.out.println("ret: " + ret);
}
public static void main(String[] args) {
JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis = jedisPool.getResource()){
test3(jedis);
}
}
}
3.4 expire 和 ttl
使用 setex 直接设置 key 的同时指定过期时间,单位为秒
public class Redis {
public static void test4(Jedis jedis) {
System.out.println("expire 和 ttl 的使用");
jedis.flushAll();
jedis.setex("key1",10,"111");
long ret = jedis.ttl("key1");
System.out.println("ret: " + ret);
}
public static void main(String[] args) {
JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis = jedisPool.getResource()){
test4(jedis);
}
}
}
3.5 type
public class Redis {
public static void test5(Jedis jedis) {
System.out.println("type 的使用");
jedis.flushAll();
jedis.set("key1","111");
System.out.println(jedis.type("key1"));
jedis.lpush("key2","a","b","c");
System.out.println(jedis.type("key2"));
jedis.hset("key3","name","zhangsan");
System.out.println(jedis.type("key3"));
jedis.sadd("key4","a","a","a");
System.out.println(jedis.type("key4"));
jedis.zadd("key5",90,"zhangsan");
System.out.println(jedis.type("key5"));
}
public static void main(String[] args) {
JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis = jedisPool.getResource()){
test5(jedis);
}
}
}
4.字符串操作
4.1 mget 和 mset
public class RedisString {
public static void test1(Jedis jedis) {
System.out.println("mget 和 mset 的使用");
jedis.flushAll();
jedis.mset("key1","111","key2","222");
List<String> ret = jedis.mget("key1","key2");
System.out.println("ret: " + ret);
}
public static void main(String[] args) {
JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
try(Jedis jedis = jedisPool.getResource()) {
test1(jedis);
}
}
}
4.2 append
public class RedisString {
public static void test2(Jedis jedis) {
System.out.println("append 的使用");
jedis.flushAll();
jedis.set("key1","111");
jedis.append("key1","aaa");
String ret = jedis.get("key1");
System.out.println("ret: " + ret);
}
public static void main(String[] args) {
JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
try(Jedis jedis = jedisPool.getResource()) {
test2(jedis);
}
}
}
4.3 getrange 和 setrange
public class RedisString {
public static void test3(Jedis jedis) {
System.out.println("getrange 和 setrange 的使用");
jedis.flushAll();
jedis.set("key1","abcdefg");
String ret = jedis.getrange("key1",0,-1);
System.out.println("ret: " + ret);
jedis.setrange("key1",1,"111");
ret = jedis.get("key1");
System.out.println("ret: " + ret);
}
public static void main(String[] args) {
JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
try(Jedis jedis = jedisPool.getResource()) {
test3(jedis);
}
}
}
4.4 incr 和 decr
public class RedisString {
public static void test4(Jedis jedis) {
System.out.println("incr 和 decr 的使用");
jedis.flushAll();
jedis.set("key1","1");
long ret = jedis.incr("key1");
System.out.println("ret: " + ret);
ret = jedis.decr("key1");
System.out.println("ret: " + ret);
}
public static void main(String[] args) {
JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
try(Jedis jedis = jedisPool.getResource()) {
test4(jedis);
}
}
}
5.列表操作
5.1 lpush 和 lpop 和 lrange
public class RedisList {
public static void test1(Jedis jedis) {
System.out.println("lpush 和 lpop 和 lrange 的使用");
jedis.flushAll();
jedis.lpush("key1","a","b","c");
List<String> ret = jedis.lrange("key1",0,-1);
System.out.println("ret: " + ret);
String value = jedis.lpop("key1");
System.out.println("value: "+ value);
ret = jedis.lrange("key1",0,-1);
System.out.println("ret: " + ret);
}
public static void main(String[] args) {
JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
try(Jedis jedis = jedisPool.getResource()) {
test1(jedis);
}
}
}
5.2 rpush 和 rpop
public class RedisList {
public static void test2(Jedis jedis) {
System.out.println("rpush 和 rpop 的使用");
jedis.flushAll();
jedis.rpush("key1","a","b","c");
List<String> ret = jedis.lrange("key1",0,-1);
System.out.println("ret: " + ret);
String value = jedis.rpop("key1");
System.out.println("value: "+ value);
ret = jedis.lrange("key1",0,-1);
System.out.println("ret: " + ret);
}
public static void main(String[] args) {
JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
try(Jedis jedis = jedisPool.getResource()) {
test2(jedis);
}
}
}
、
5.3 blpop 和 brpop
public class RedisList {
public static void test3(Jedis jedis) {
System.out.println("blpop 和 brpop 的使用");
jedis.flushAll();
jedis.lpush("key1","a","b","c");
List<String> ret = jedis.blpop(0,"key1");
System.out.println("ret: " + ret);
ret = jedis.lrange("key1",0,-1);
System.out.println("ret: " + ret);
ret = jedis.brpop(0,"key1");
System.out.println("ret: " + ret);
ret = jedis.lrange("key1",0,-1);
System.out.println("ret: " + ret);
}
public static void main(String[] args) {
JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
try(Jedis jedis = jedisPool.getResource()) {
test3(jedis);
}
}
}
5.4 llen
public class RedisList {
public static void test4(Jedis jedis) {
System.out.println("llen 的使用");
jedis.flushAll();
jedis.lpush("key1","a","b","c");
long ret = jedis.llen("key1");
System.out.println("ret: " + ret);
}
public static void main(String[] args) {
JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
try(Jedis jedis = jedisPool.getResource()) {
test4(jedis);
}
}
}
6.哈希表操作
6.1 hset 和 hget
public class RedisHash {
public static void test1(Jedis jedis) {
System.out.println("hset 和 hget 的使用");
jedis.flushAll();
jedis.hset("key1","f1","111");
HashMap<String,String> map = new HashMap<>();
map.put("f2","222");
map.put("f3","333");
jedis.hset("key1",map);
List<String> ret = jedis.hmget("key1","f1","f2","f3");
System.out.println("ret: " + ret);
String value = jedis.hget("key1","f4");
System.out.println("value: " + value);
}
public static void main(String[] args) {
JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
try(Jedis jedis = jedisPool.getResource()) {
test1(jedis);
}
}
}
6.2 hexists 和 hdel
public class RedisHash {
public static void test2(Jedis jedis) {
System.out.println("hexists 和 hdel 的使用");
jedis.flushAll();
jedis.hset("key1","f1","111");
HashMap<String,String> map = new HashMap<>();
map.put("f2","222");
map.put("f3","333");
jedis.hset("key1",map);
boolean ret = jedis.hexists("key1","f1");
System.out.println("ret: " + ret);
jedis.hdel("key1","f1");
ret = jedis.hexists("key1","f1");
System.out.println("ret: " + ret);
}
public static void main(String[] args) {
JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
try(Jedis jedis = jedisPool.getResource()) {
test2(jedis);
}
}
}
6.3 hkeys 和 hvals
public class RedisHash {
public static void test3(Jedis jedis) {
System.out.println("hkeys 和 hvals 的使用");
jedis.flushAll();
jedis.hset("key1","f1","111");
HashMap<String,String> map = new HashMap<>();
map.put("f2","222");
map.put("f3","333");
jedis.hset("key1",map);
Set<String> ret = jedis.hkeys("key1");
System.out.println("ret: " + ret);
List<String> values = jedis.hvals("key1");
System.out.println("values: " + values);
}
public static void main(String[] args) {
JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
try(Jedis jedis = jedisPool.getResource()) {
test3(jedis);
}
}
}
7.集合操作
7.1 sadd 和 smembers
public class RedisSet {
public static void test1(Jedis jedis) {
System.out.println("sadd 和 smembers 的使用");
jedis.flushAll();
jedis.sadd("key1","aaa","bbb","ccc");
Set<String> ret = jedis.smembers("key1");
System.out.println("ret: " + ret);
}
public static void main(String[] args) {
JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis = jedisPool.getResource()) {
test1(jedis);
}
}
}
7.2 srem 和 sismember
public class RedisSet {
public static void test2(Jedis jedis) {
System.out.println("srem 和 sismember 的使用");
jedis.flushAll();
jedis.sadd("key1","aaa","bbb","ccc");
boolean ret = jedis.sismember("key1","aaa");
System.out.println("ret: " + ret);
jedis.srem("key1","aaa");
ret = jedis.sismember("key1","aaa");
System.out.println("ret: " + ret);
}
public static void main(String[] args) {
JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis = jedisPool.getResource()) {
test2(jedis);
}
}
}
7.3 scard 和 spop
public class RedisSet {
public static void test3(Jedis jedis) {
System.out.println("scard 和 spop 的使用");
jedis.flushAll();
jedis.sadd("key1","aaa","bbb","ccc");
long ret = jedis.scard("key1");
System.out.println("ret: " + ret);
String value = jedis.spop("key1");
System.out.println("value: " + value);
Set<String> tmp = jedis.smembers("key1");
System.out.println("tmp: " + tmp);
}
public static void main(String[] args) {
JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis = jedisPool.getResource()) {
test3(jedis);
}
}
}
7.4 sinter 和 sinterstore
public class RedisSet {
public static void test4(Jedis jedis) {
System.out.println("sinter 和 sinterstore 的使用");
jedis.flushAll();
jedis.sadd("key1","aaa","bbb","ccc");
jedis.sadd("key2","aaa","bbb","ddd");
Set<String> ret = jedis.sinter("key1","key2");
System.out.println("ret: " + ret);
jedis.sinterstore("key3","key1","key2");
ret = jedis.smembers("key3");
System.out.println("ret: " + ret);
}
public static void main(String[] args) {
JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis = jedisPool.getResource()) {
test4(jedis);
}
}
}
8.有序集合操作
8.1 zadd 和 zrange
public class RedisZset {
public static void test1(Jedis jedis) {
System.out.println("zadd 和 zrange 的使用");
jedis.flushAll();
jedis.zadd("key1",90,"zhangsan");
jedis.zadd("key1",60,"lisi");
jedis.zadd("key1",88,"wangwu");
List<String> ret = jedis.zrange("key1",0,-1);
System.out.println("ret: " + ret);
}
public static void main(String[] args) {
JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
try(Jedis jedis = jedisPool.getResource()) {
test1(jedis);
}
}
}
8.2 zrem 和 zcard
public class RedisZset {
public static void test2(Jedis jedis) {
System.out.println("zrem 和 zcard 的使用");
jedis.flushAll();
jedis.zadd("key1",90,"zhangsan");
jedis.zadd("key1",60,"lisi");
jedis.zadd("key1",88,"wangwu");
long ret = jedis.zcard("key1");
System.out.println("ret: " + ret);
ret = jedis.zrem("key1","zhangsan");
System.out.println("ret: " + ret);
List<Tuple> values = jedis.zrangeWithScores("key1",0,-1);
System.out.println("values: " + values);
}
public static void main(String[] args) {
JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
try(Jedis jedis = jedisPool.getResource()) {
test2(jedis);
}
}
}
8.3 zscore 和 zrank
public class RedisZset {
public static void test3(Jedis jedis) {
System.out.println("zscore 和 zrank 的使用");
jedis.flushAll();
jedis.zadd("key1",90,"zhangsan");
jedis.zadd("key1",60,"lisi");
jedis.zadd("key1",88,"wangwu");
double ret = jedis.zscore("key1","zhangsan");
System.out.println("ret: " + ret);
long values = jedis.zrank("key1","zhangsan");
System.out.println("values: " + values);
}
public static void main(String[] args) {
JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
try(Jedis jedis = jedisPool.getResource()) {
test3(jedis);
}
}
}