[Redis] 渐进式遍历+使用jedis操作Redis+使用Spring操作Redis

简介: [Redis] 渐进式遍历+使用jedis操作Redis+使用Spring操作Redis

3.3 在Java中操作Redis
3.3.1 准备工作
引入jedis依赖


redis.clients
jedis
4.3.2

1
2
3
4
5
创建Redis连接,并测试连接
public class Main {
public static void main(String[] args) {
JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");//redis连接池
try (Jedis jedis = jedisPool.getResource()){//获取数据源
String ping = jedis.ping();
System.out.println(ping);
}
}
}
1
2
3
4
5
6
7
8
9
返回pong,说明连接成功.

3.3.2 关于String的操作
get,set.
public class Main {
public static void main(String[] args) {
JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");//redis连接池
try (Jedis jedis = jedisPool.getResource()){//获取数据源
stringTest(jedis);
}
}
private static void stringTest(Jedis jedis){
jedis.set("key1","value1");//set一次只可以设置一个键值对
jedis.set("key2","value2");
jedis.set("key3","value3");
String key1 = jedis.get("key1");
String key2 = jedis.get("key2");
String key3 = jedis.get("key100");
System.out.println(key1);
System.out.println(key2);
System.out.println(key3);
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

mset,mget
private static void stringTest2(Jedis jedis){
jedis.flushAll();
jedis.mset("key1","value1","key2","value2","key3","value3");//key和value交替设置
List list = jedis.mget("key1","key2","key3");//由于value的值可以重复,所以使用的是list来接收
System.out.println(list);
}
1
2
3
4
5
6

在mset的时候,key和value是交替设置的.在mget接收value的时候,由于value的值是可以重复的.所以我们使用List来接收.

exists,del
private static void stringTest3(Jedis jedis){
jedis.flushAll();
jedis.set("key1","value1");
Boolean ret = jedis.exists("key1");//返回Boolean,即该key是否存在
System.out.println(ret);
Long ret1 = jedis.del("key1");//返回的是Long类型,即删除成功的个数
System.out.println(ret1);
}
1
2
3
4
5
6
7
8
exists返回的是Boolean,即该key是否存在,del返回的是Long类型,即删除成功的个数.

keys
private static void stringTest4(Jedis jedis){
jedis.flushAll();
jedis.set("key1","value1");
jedis.set("key2","value2");
jedis.set("key3","value3");
jedis.set("key4","value4");
jedis.set("key100","value100");
Set keys = jedis.keys("");//由于key不可以重复,所以我们使用set来接收
System.out.println(keys);
Set keys1 = jedis.keys("key?");//?只匹配一个字符
System.out.println(keys1);
Set keys2 = jedis.keys("key
");//*可以匹配多个字符
System.out.println(keys2);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

expire,ttl
private static void stringTest5(Jedis jedis) throws InterruptedException {
jedis.flushAll();
jedis.set("key1","value1");
jedis.set("key2","value2");
jedis.set("key3","value3");
jedis.set("key4","value4");
jedis.expire("key1",5);
Long key1 = jedis.ttl("key1");
System.out.println(key1);
Thread.sleep(6000);//阻塞6秒
boolean ret = jedis.exists("key1");
System.out.println(ret);
}
1
2
3
4
5
6
7
8
9
10
11
12
13

append
private static void stringTest6(Jedis jedis){
jedis.flushAll();
jedis.set("key1","value1");
jedis.append("key1", "aaa");
System.out.println(jedis.get("key1"));
}
1
2
3
4
5
6

getrange,setrange
getrange获取指定字符串区间的字符,setrange设置指定字符串区间的字符.
private static void stringTest7(Jedis jedis){
jedis.flushAll();
jedis.set("key1","value1aaaa");
System.out.println(jedis.getrange("key1", 0, 4));
jedis.setrange("key1",0,"bbbbb");
System.out.println(jedis.getrange("key1",0,4));
}
1
2
3
4
5
6
7

setnx
如果存在key,则不设置,返回0,不存在,则设置
private static void stringTest8(Jedis jedis){
jedis.flushAll();
jedis.set("key1","value1");
long setnx = jedis.setnx("key1", "value1");
System.out.println(setnx);
}
1
2
3
4
5
6

setex
设置键值对,并设置过期时间
private static void stringTest9(Jedis jedis) throws InterruptedException {
jedis.flushAll();
jedis.setex("key1",2,"value1");
Thread.sleep(3000);
String s = jedis.get("key1");
System.out.println(s);
}
1
2
3
4
5
6
7

incr,decr
给指定的key对应的value+1或-1
private static void stringTest10(Jedis jedis){
jedis.flushAll();
jedis.set("key1","1");
jedis.incr("key1");
System.out.println(jedis.get("key1"));
jedis.decr("key1");
System.out.println(jedis.get("key1"));
}
1
2
3
4
5
6
7
8

incrby,decrby
private static void stringTest11(Jedis jedis){
jedis.flushAll();
jedis.set("key1","1");
jedis.incrBy("key1",2);
System.out.println(jedis.get("key1"));
jedis.decrBy("key1",2);
System.out.println(jedis.get("key1"));
}
1
2
3
4
5
6
7
8

3.3.3 关于List的操作
lpush,lpop
private static void listTest1(Jedis jedis){
jedis.flushAll();
jedis.lpush("list1","v1","v2","v3");
System.out.println(jedis.lpop("list1"));
System.out.println(jedis.lpop("list1"));
System.out.println(jedis.lpop("list1"));
}
1
2
3
4
5
6
7

rpush,rpop
private static void listTest2(Jedis jedis){
jedis.flushAll();
jedis.rpush("key1","v1","v2","v3");
System.out.println(jedis.rpop("key1"));
System.out.println(jedis.rpop("key1"));
System.out.println(jedis.rpop("key1"));
}
1
2
3
4
5
6
7

lrange
查看指定下标区间的元素
private static void listTest3(Jedis jedis){
jedis.flushAll();
jedis.rpush("key1","v1","v2","v3");
System.out.println(jedis.lrange("key1",0,1));//左闭右闭
}
1
2
3
4
5

blpop
private static void listTest4(Jedis jedis){
jedis.flushAll();
jedis.lpush("key1","v1");
System.out.println(jedis.blpop(0, "key1"));
System.out.println(jedis.blpop(5, "key1"));
}
1
2
3
4
5
6

brpop和blpop同理,这里不再赘述.

lindex
获取指定下标的元素
private static void listTest5(Jedis jedis){
jedis.flushAll();
jedis.lpush("key1","v1","v2","v3");
System.out.println(jedis.lindex("key1", 2));
}
1
2
3
4
5

linsert
在指定的元素之前或者之后插入指定元素.
private static void listTest6(Jedis jedis){
jedis.flushAll();
jedis.lpush("key1","v1","v2");
jedis.linsert("key1", ListPosition.BEFORE,"v2","100");
System.out.println(jedis.lrange("key1",0,-1));
}
1
2
3
4
5
6

llen
private static void listTest7(Jedis jedis){
jedis.flushAll();
jedis.rpush("key1","v1","v2","v3");
System.out.println(jedis.llen("key1"));
}
1
2
3
4
5

3.3.4 关于Hash的操作
hset,hget
private static void hashTest1(Jedis jedis){
jedis.flushAll();
jedis.hset("key1","field1","value1");
jedis.hset("key1","field2","value2");
jedis.hset("key1","field3","value3");
System.out.println(jedis.hget("key1", "field1"));
System.out.println(jedis.hget("key1", "field2"));
System.out.println(jedis.hget("key1", "field3"));
}
1
2
3
4
5
6
7
8
9

hexists,hdel
private static void hashTest2(Jedis jedis){
jedis.flushAll();
jedis.hset("key1","field1","value1");
jedis.hset("key1","field2","value2");
jedis.hset("key1","field3","value3");
System.out.println(jedis.hexists("key1", "field1"));
System.out.println(jedis.hexists("key1", "field100"));
jedis.hdel("key1","field1");
System.out.println(jedis.hgetAll("key1"));
}
1
2
3
4
5
6
7
8
9
10

hkeys,hvals
private static void hashTest3(Jedis jedis){
jedis.flushAll();
jedis.hset("key1","field1","value1");
jedis.hset("key1","field2","value2");
jedis.hset("key1","field3","value3");
System.out.println(jedis.hkeys("key1"));//获取所有的key
System.out.println(jedis.hvals("key1"));//获取所有的val
}
1
2
3
4
5
6
7
8

hmget
private static void hashTest4(Jedis jedis){
jedis.flushAll();
jedis.hset("key1","field1","value1");
jedis.hset("key1","field2","value2");
jedis.hset("key1","field3","value3");
System.out.println(jedis.hmget("key1","field1","field2"));
}
1
2
3
4
5
6
7

hlen
private static void hashTest5(Jedis jedis){
jedis.flushAll();
jedis.hset("key1","field1","value1");
jedis.hset("key1","field2","value2");
jedis.hset("key1","field3","value3");
System.out.println(jedis.hlen("key1"));
}
1
2
3
4
5
6
7

hincrby,hincrbyfloat
private static void hashTest6(Jedis jedis){
jedis.flushAll();
jedis.hset("key1","field1","2");
jedis.hincrBy("key1","field1",1);
System.out.println(jedis.hget("key1","field1"));
jedis.hincrByFloat("key1","field1",1.5);
System.out.println(jedis.hget("key1","field1"));
}
1
2
3
4
5
6
7
8

3.3.5 关于set的操作
sadd,smember
private static void setTest1(Jedis jedis){
jedis.flushAll();
jedis.sadd("key1","m1","m2","m3");
jedis.sadd("key1","m1");
System.out.println(jedis.smembers("key1"));
}
1
2
3
4
5
6

srem,sismember
private static void setTest2(Jedis jedis){
jedis.flushAll();
jedis.sadd("key1","m1","m2","m3");
System.out.println(jedis.smembers("key1"));
jedis.srem("key1","m1");
System.out.println(jedis.smembers("key1"));
System.out.println(jedis.sismember("key1", "m1"));
System.out.println(jedis.sismember("key1", "m2"));
}
1
2
3
4
5
6
7
8
9

scard
private static void setTest3(Jedis jedis){
jedis.flushAll();
jedis.sadd("key1","m1","m2","m3");
System.out.println(jedis.scard("key1"));
}
1
2
3
4
5

sinter
private static void setTest4(Jedis jedis){
jedis.flushAll();
jedis.sadd("key1","m1","m2","m3");
jedis.sadd("key2","m2","m3","m4");
System.out.println(jedis.sinter("key1","key2"));
}
1
2
3
4
5
6

sunion和sdiff是同样的道理,这里不再赘述.

3.3.6 关于zset的操作
zadd,zrange,zrangeWithScores
private static void zsetTest1(Jedis jedis){
jedis.flushAll();
jedis.zadd("key1",90,"zhangfei");
jedis.zadd("key1",91,"zhaoyun");
jedis.zadd("key1",92,"guanyu");
System.out.println(jedis.zrange("key1",0,-1));
System.out.println(jedis.zrangeWithScores("key1",0,-1));//返回的是带有分数的二元组
}
1
2
3
4
5
6
7
8

zrem,zcard
private static void zsetTest2(Jedis jedis){
jedis.flushAll();
jedis.zadd("key1",90,"zhangfei");
jedis.zadd("key1",91,"zhaoyun");
jedis.zadd("key1",92,"guanyu");
jedis.zrem("key1","guanyu");
System.out.println(jedis.zcard("key1"));
System.out.println(jedis.zrangeWithScores("key1",0,-1));
}
1
2
3
4
5
6
7
8
9

zcount
返回指定分数区间的所有key
private static void zsetTest3(Jedis jedis){
jedis.flushAll();
jedis.zadd("key1",90,"zhangfei");
jedis.zadd("key1",91,"zhaoyun");
jedis.zadd("key1",92,"guanyu");
System.out.println(jedis.zcount("key1",90,91));
}
1
2
3
4
5
6
7

zpopmax,zpopmin
private static void zsetTest4(Jedis jedis){
jedis.flushAll();
jedis.zadd("key1",90,"zhangfei");
jedis.zadd("key1",91,"zhaoyun");
jedis.zadd("key1",92,"guanyu");
System.out.println(jedis.zpopmax("key1"));
System.out.println(jedis.zpopmin("key1"));
}
1
2
3
4
5
6
7
8

zrank
按升序的顺序返回指定元素的排名
private static void zsetTest5(Jedis jedis){
jedis.flushAll();
jedis.zadd("key1",90,"zhangfei");
jedis.zadd("key1",91,"zhaoyun");
jedis.zadd("key1",92,"guanyu");
System.out.println(jedis.zrank("key1","guanyu"));
}
1
2
3
4
5
6
7

zscore
获取指定元素的分数.
private static void zsetTest6(Jedis jedis){
jedis.flushAll();
jedis.zadd("key1",90,"zhangfei");
jedis.zadd("key1",91,"zhaoyun");
jedis.zadd("key1",92,"guanyu");
System.out.println(jedis.zscore("key1","zhaoyun"));
}
1
2
3
4
5
6
7

zincrby
给指定的元素加上指定的分数
private static void zsetTest7(Jedis jedis){
jedis.flushAll();
jedis.zadd("key1",90,"zhangfei");
jedis.zadd("key1",91,"zhaoyun");
jedis.zadd("key1",92,"guanyu");
jedis.zincrby("key1",10,"zhaoyun");
System.out.println(jedis.zscore("key1","zhaoyun"));
}
1
2
3
4
5
6
7
8

zinterstore
private static void zsetTest8(Jedis jedis){
jedis.flushAll();
jedis.zadd("key1",90,"zhangfei");
jedis.zadd("key1",91,"zhaoyun");
jedis.zadd("key1",92,"guanyu");
jedis.zadd("key2",90,"zhangfei");
jedis.zadd("key2",91,"zhaoyun");
jedis.zadd("key2",93,"zhugeliang");
jedis.zinterstore("key3","key1","key2");
System.out.println(jedis.zrangeWithScores("key3",0, -1));
}
1
2
3
4
5
6
7
8
9
10
11

union和diff是同样的道理,这里不再赘述.

3.4 在Spring中操作Redis
3.4.1 准备工作

相关文章
|
11天前
|
消息中间件 NoSQL Java
Spring Boot整合Redis
通过Spring Boot整合Redis,可以显著提升应用的性能和响应速度。在本文中,我们详细介绍了如何配置和使用Redis,包括基本的CRUD操作和具有过期时间的值设置方法。希望本文能帮助你在实际项目中高效地整合和使用Redis。
27 1
|
1月前
|
NoSQL Java Redis
redis的基本命令,并用netty操作redis(不使用springboot或者spring框架)就单纯的用netty搞。
这篇文章介绍了Redis的基本命令,并展示了如何使用Netty框架直接与Redis服务器进行通信,包括设置Netty客户端、编写处理程序以及初始化Channel的完整示例代码。
44 1
redis的基本命令,并用netty操作redis(不使用springboot或者spring框架)就单纯的用netty搞。
|
30天前
|
缓存 NoSQL Java
Spring Boot与Redis:整合与实战
【10月更文挑战第15天】本文介绍了如何在Spring Boot项目中整合Redis,通过一个电商商品推荐系统的案例,详细展示了从添加依赖、配置连接信息到创建配置类的具体步骤。实战部分演示了如何利用Redis缓存提高系统响应速度,减少数据库访问压力,从而提升用户体验。
70 2
|
16天前
|
JavaScript NoSQL Java
CC-ADMIN后台简介一个基于 Spring Boot 2.1.3 、SpringBootMybatis plus、JWT、Shiro、Redis、Vue quasar 的前后端分离的后台管理系统
CC-ADMIN后台简介一个基于 Spring Boot 2.1.3 、SpringBootMybatis plus、JWT、Shiro、Redis、Vue quasar 的前后端分离的后台管理系统
31 0
|
1月前
|
NoSQL Java Redis
在 Spring 中操作 Redis
本文详细介绍了在Spring框架中如何通过引入依赖、配置文件、使用StringRedisTemplate类以及执行原生命令等方式来操作Redis数据库,并提供了对String、List、Set、Hash和ZSet数据类型的操作示例。
69 0
在 Spring 中操作 Redis
|
2月前
|
JSON NoSQL Java
redis的java客户端的使用(Jedis、SpringDataRedis、SpringBoot整合redis、redisTemplate序列化及stringRedisTemplate序列化)
这篇文章介绍了在Java中使用Redis客户端的几种方法,包括Jedis、SpringDataRedis和SpringBoot整合Redis的操作。文章详细解释了Jedis的基本使用步骤,Jedis连接池的创建和使用,以及在SpringBoot项目中如何配置和使用RedisTemplate和StringRedisTemplate。此外,还探讨了RedisTemplate序列化的两种实践方案,包括默认的JDK序列化和自定义的JSON序列化,以及StringRedisTemplate的使用,它要求键和值都必须是String类型。
redis的java客户端的使用(Jedis、SpringDataRedis、SpringBoot整合redis、redisTemplate序列化及stringRedisTemplate序列化)
|
1月前
|
存储 NoSQL Java
Spring Boot项目中使用Redis实现接口幂等性的方案
通过上述方法,可以有效地在Spring Boot项目中利用Redis实现接口幂等性,既保证了接口操作的安全性,又提高了系统的可靠性。
37 0
|
NoSQL Java 数据库
|
Java Spring 数据格式
spring 整合redis
用的是最新的jedis-2.6.2.jar这个包,这个和以前的有点不同。还需要添加spring-data-redis-1.2.1.RELEASE.jar和commons-pool2-2.3.jar。 在类路径下创建spring-redis-config.
935 0
|
NoSQL Java Redis
redis和spring整合
pom构建: [html] view plain copy  print? <modelVersion>4.0.0</modelVersion>   <groupId>com.x.redis</groupId>   <artifactId>springredis&l
1035 0