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都是采用此策略序列化保存的。打开的话看到的是 \xac\xed\x00\x之类
Redis客户端命令对应的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
Redis | RedisTemplate rt |
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对象d |
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
Redis | RedisTemplate rt |
lpush list node1 node2 node3... | rt.opsForList().leftPush("list","node") |
lpush list node1 node2 node3... | rt.opsForList().leftPushAll("list",list) //list是集合对象 |
rpush list node1 node2 node3... | rt.opsForList().rightPush("list","node") |
rpush list node1 node2 node3... | 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
Redis | RedisTemplate rt |
sadd key member1 member2... | rt.boundSetOps("key").add("member1","member2",...) |
sadd key 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",...) |
评论 (0)