Jedis客户端相关

java客户端相关

1.0 StringRedisTemplate与RedisTemplate

spring 封装了 RedisTemplate 对象来进行对redis的各种操作,它支持所有的 redis 原生的 api。在RedisTemplate中提供了几个常用的接口方法的使用,分别是:

private ValueOperations<K, V> valueOps;
private HashOperations<K, V> hashOps;
private ListOperations<K, V> listOps;
private SetOperations<K, V> setOps;
private ZSetOperations<K, V> zSetOps;

RedisTemplate中定义了对5种数据结构操作

redisTemplate.opsForValue();//操作字符串
redisTemplate.opsForHash();//操作hash
redisTemplate.opsForList();//操作list
redisTemplate.opsForSet();//操作set
redisTemplate.opsForZSet();//操作有序set

StringRedisTemplate继承自RedisTemplate,也一样拥有上面这些操作。

StringRedisTemplate默认采用的是String的序列化策略,保存的key和value都是采用此策略序列化保存的。

RedisTemplate默认采用的是JDK的序列化策略,保存的key和value都是采用此策略序列化保存的。

2.0 RedisTemplate中的方法列表

String类型结构
Redis RedisTemplate rt
set key value rt.opsForValue().set("key","value")
get key rt.opsForValue().get("key")
del key rt.delete("key")
strlen key rt.opsForValue().size("key")
getset key value rt.opsForValue().getAndSet("key","value")
getrange key start end rt.opsForValue().get("key",start,end)
append key value rt.opsForValue().append("key","value")
Hash结构
hmset key field1 value1 field2 value2... rt.opsForHash().putAll("key",map) //map是一个集合对象
hset key field value rt.opsForHash().put("key","field","value")
hexists key field rt.opsForHash().hasKey("key","field")
hgetall key rt.opsForHash().entries("key") //返回Map对象
hvals key rt.opsForHash().values("key") //返回List对象
hkeys key rt.opsForHash().keys("key") //返回List对象
hmget key field1 field2... rt.opsForHash().multiGet("key",keyList)
hsetnx key field value rt.opsForHash().putIfAbsent("key","field","value"
hdel key field1 field2 rt.opsForHash().delete("key","field1","field2")
hget key field rt.opsForHash().get("key","field")
List结构
lpush list node1 node2 node3... rt.opsForList().leftPush("list","node")
rt.opsForList().leftPushAll("list",list) //list是集合对象
rpush list node1 node2 node3... rt.opsForList().rightPush("list","node")
rt.opsForList().rightPushAll("list",list) //list是集合对象
lindex key index rt.opsForList().index("list", index)
llen key rt.opsForList().size("key")
lpop key rt.opsForList().leftPop("key")
rpop key rt.opsForList().rightPop("key")
lpushx list node rt.opsForList().leftPushIfPresent("list","node")
rpushx list node rt.opsForList().rightPushIfPresent("list","node")
lrange list start end rt.opsForList().range("list",start,end)
lrem list count value rt.opsForList().remove("list",count,"value")
lset key index value rt.opsForList().set("list",index,"value")
Set结构
sadd key member1 member2... rt.boundSetOps("key").add("member1","member2",...)
rt.opsForSet().add("key", set) //set是一个集合对象
scard key rt.opsForSet().size("key")
sidff key1 key2 rt.opsForSet().difference("key1","key2") //返回一个集合对象
sinter key1 key2 rt.opsForSet().intersect("key1","key2")//同上
sunion key1 key2 rt.opsForSet().union("key1","key2")//同上
sdiffstore des key1 key2 rt.opsForSet().differenceAndStore("key1","key2","des")
sinter des key1 key2 rt.opsForSet().intersectAndStore("key1","key2","des")
sunionstore des key1 key2 rt.opsForSet().unionAndStore("key1","key2","des")
sismember key member rt.opsForSet().isMember("key","member")
smembers key rt.opsForSet().members("key")
spop key rt.opsForSet().pop("key")
srandmember key count rt.opsForSet().randomMember("key",count)
srem key member1 member2... rt.opsForSet().remove("key","member1","member2",...)

55

posted @ 2022-12-16 17:17  浮沉丶随心  阅读(6)  评论(0编辑  收藏  举报