Java 使用Jedis操作redis

时间:2022-02-19 17:27:04

目录


redis相关命令总结

导入Jar包

创建Redis连接

Jedis API整理

普通类型(String)

散列类型(Hash)

列表类型(List)

无序集合类型(Set)

有序集合类型(Zset)

 

 


 

 

 

 

 

 

 

redis相关命令总结

  redis常用数据结构的操作命令可以参考:Redis命令总结

 

导入Jar包

  Java官方推荐使用Jedis来管理redis,所以这里本文将会使用Jedis。

  如果创建Maven项目,那么在pom.xml中添加下面的依赖:

<dependency>
	<groupId>redis.clients</groupId>
	<artifactId>jedis</artifactId>
	<version>2.10.2</version>
	<type>jar</type>
	<scope>compile</scope>
</dependency>

  如果是手动导入Jar包,则需要下面三个Jar包(已经注明了groupId、artifactId、version):

redis/clients/jedis/2.10.2/jedis-2.10.2.jar
org/slf4j/slf4j-api/1.7.22/slf4j-api-1.7.22.jar
org/apache/commons/commons-pool2/2.4.3/commons-pool2-2.4.3.jar

  

 

创建Redis连接

  

  在Jedis中,创建Redis,只需要创建Jedis对象即可,下面是创建Jedis常用的构造方法:

Jedis() 
Jedis(String host) 
Jedis(String host, int port) 

  上面的三个构造方法是最简单的创建Jedis对象的方式,如果使用redis连接池,可以参考文章:

 

Jedis API整理

  下面将会列举出常用的Jedis API,这些API和redis原始的命令几乎没有区别,只需要大概注意一下返回值即可。

  另外还需要注意的是,下面的所有API都是通过Jedis对象进行调用的,也就是说,下面的所有API都是Jedis的实例方法。 

 

普通类型(String)

Redis命令 Jedis对应的API
set key value String set(String key, String value)
get key String get(String key) 
exists key Long exists(String... keys)
del key Long del(String key) 
type key String type(String key)
incr key Long incr(String key)
incrby key increment Long incrBy(String key, long increment)
incrbyfloat key incrment Double incrByFloat(String key, double increment)
decr key Long decr(String key)
decrby key decrment  Long decrBy(String key, long decrement)
append key value Long append(String key, String value)
strlen key Long strlen(String key)
mset key1 value1 key2  value2 String mset(String k1, String v1, String k2, String v2...)
mget key1 key2... List<String> mget(String k1, String k2)

 

散列类型(hash)

Redis命令 Jedis对应的API
hset key field value

Long hset(String key, String field, String value)

Long hset(String key, Map<String,String> map)

hsetnx key field value

Long hsetnx(String key, String field, String value)

String hmset(String key, Map<String,String> hash)

hmset key field1 value1 field2 value2 String hmset(String key, Map<String,String> hash)
hget key field String hget(String key, String field)
hmget key field1 field2 List<String> hmget(String key, String k1, String k2...)
hgetall key Map<String,String> hgetAll(String key) 
hexists key field Boolean hexists(String key, String field)
hincrby key field increment Long hincrBy(String key, String field, long value)
hincrbyfloat key field increment Double hincrByFloat(String key, String field, double value)
hdel key field Long hdel(String key, String... fields) 
hkeys key Set<String> hkeys(String key)
hvals key List<String> hvals(String key)
hlen key Long hlen(String key)

 

列表类型(list)

Redis命令 Jedis对应的API
lpush key value1 value2 Long lpush(String key, String... strings)
rpush key value1 value2 Long rpush(String key, String... strings) 
lpop key String lpop(String key)
rpop key String rpop(String key)
llen Long llen(String key)
lrange key start end List<String> lrange(String key, long start, long stop)
lrange key 0 -1  
lindex key index String lindex(String key, long index)
lset key index value String lset(String key, long index, String value) 
ltrim key start end String ltrim(String key, long start, long stop)
linsert key before/after oneValue value Long linsert(String key, ListPosition where, String pivot, String value)
rpoplpush key1 key2 String rpoplpush(String srckey, String dstkey) 
lrem key count value Long lrem(String key, long count, String value)

 

无序集合类型(set)

Redis命令  Jedis对应的API
 sadd key member1 member2 Long sadd(String key, String... members)
 srem key member1 member2 Long srem(String key, String... members)
 smembers key Set<String> smembers(String key)
 sismember key member Boolean sismember(String key, String member)
 sdiff key1 key2 ... Set<String> sdiff(String... keys)
 sinter key1 key2... Set<String> sinter(String... keys)
 sunion key1 key2... Set<String> sunion(String... keys)
 scard key Long scard(String key)
srandmember key [count]

String srandmember(String key)

List<String> srandmember(String key, int count)

spop key [count]

String spop(String key)

Set<String> spop(String key, long count)

 

有序集合类型(zset)

Redis命令 Jedis对应的API
zadd key score member

Long zadd(String key, double score, String member)

Long zadd(String key, Map<String,Double> scoreMembers)

zscore key member Double zscore(String key, String member)
zrange key start end Set<String> zrange(String key, long start, long stop)
zrevrange key start end Set<String> zrevrange(String key, long start, long stop)

zrangebyscore key min max[withscore][limit offset count]

Set<String> zrangeByScore(String key, double min, double max)

Set<String> zrangeByScore(String key, String min, String max)

Set<String> zrangeByScore(String key, String min, String max, int offset, int count)

zincrby key increment member Double zincrby(String key, double increment, String member)
zcard key Long zcard(String key)
zcount key min max

Long zcount(String key, String min, String max)

Long zcount(String key, double min, double max)

zrem key member1 member2... Long zrem(String key, String... members)
zremrangebyrank key start end Long zremrangeByRank(String key, long start, long stop)
zremrangebyscore key min max

Long zremrangeByScore(String key, double min, double max)

Long zremrangeByScore(String key, String min, String max)

zrank key member  Long zrank(String key, String member)