1、Jedis所需要的jar包
< dependency>
< groupId> redis.clients</ groupId>
< artifactId> jedis</ artifactId>
< version> 3.7.0</ version>
</ dependency>
2、Jedis常用操作
创建maven的工程
创建测试程序
public class Demo1 {
public static void main ( String [ ] args) {
Jedis jedis = new Jedis ( "127.0.0.1" , 6379 ) ;
String pong = jedis. ping ( ) ;
System . out. println ( "连接成功:" + pong) ;
jedis. close ( ) ;
}
}
3、测试相关数据类型
Jedis-API: Key
public static void main ( String [ ] args) {
Jedis jedis = new Jedis ( "127.0.0.1" , 6379 ) ;
String pong = jedis. ping ( ) ;
System . out. println ( "连接成功:" + pong) ;
jedis. set ( "k1" , "v1" ) ;
jedis. set ( "k2" , "v2" ) ;
jedis. set ( "k3" , "v3" ) ;
Set < String > keys = jedis. keys ( "*" ) ;
System . out. println ( keys. size ( ) ) ;
keys. forEach ( System . out:: println ) ;
System . out. println ( jedis. exists ( "k1" ) ) ;
System . out. println ( jedis. ttl ( "k1" ) ) ;
jedis. close ( ) ;
}
Jedis-API: String
public static void main ( String [ ] args) {
Jedis jedis = new Jedis ( "127.0.0.1" , 6379 ) ;
jedis. mset ( "str1" , "v1" , "str2" , "v2" , "str3" , "v3" ) ;
System . out. println ( jedis. mget ( "str1" , "str2" , "str3" ) ) ;
}
Jedis-API: List
public static void main ( String [ ] args) {
Jedis jedis = new Jedis ( "127.0.0.1" , 6379 ) ;
List < String > list = jedis. lrange ( "mylist" , 0 , - 1 ) ;
for ( String element : list) {
System . out. println ( element) ;
}
}
Jedis-API: Set
public static void main ( String [ ] args) {
Jedis jedis = new Jedis ( "127.0.0.1" , 6379 ) ;
jedis. sadd ( "orders" , "order01" ) ;
jedis. sadd ( "orders" , "order02" ) ;
jedis. sadd ( "orders" , "order03" ) ;
jedis. sadd ( "orders" , "order04" ) ;
Set < String > smembers = jedis. smembers ( "orders" ) ;
smembers. forEach ( System . out:: println ) ;
jedis. srem ( "orders" , "order02" ) ;
}
Jedis-API: Hash
public static void main ( String [ ] args) {
Jedis jedis = new Jedis ( "127.0.0.1" , 6379 ) ;
jedis. hset ( "hash1" , "userName" , "lisi" ) ;
System . out. println ( jedis. hget ( "hash1" , "userName" ) ) ;
Map < String , String > map = new HashMap < String , String > ( ) ;
map. put ( "telphone" , "13810169999" ) ;
map. put ( "address" , "atguigu" ) ;
map. put ( "email" , "abc@163.com" ) ;
jedis. hmset ( "hash2" , map) ;
List < String > result = jedis. hmget ( "hash2" , "telphone" , "email" ) ;
result. forEach ( System . out:: println ) ;
jedis. close ( ) ;
}
Jedis-API: ZSet
public static void main ( String [ ] args) {
Jedis jedis = new Jedis ( "127.0.0.1" , 6379 ) ;
jedis. zadd ( "zset01" , 100d , "z3" ) ;
jedis. zadd ( "zset01" , 90d , "l4" ) ;
jedis. zadd ( "zset01" , 80d , "w5" ) ;
jedis. zadd ( "zset01" , 70d , "z6" ) ;
Set < String > zrange = jedis. zrange ( "zset01" , 0 , - 1 ) ;
zrange. forEach ( System . out:: println ) ;
jedis. close ( ) ;
}