Redis常用的数据结构
- string
最简单的结构,key-value形式
- list
一个key对应一个链表
- hash:
记录一个对象
- set
无重复的数据集合
- sorted set (zset)
有序无重复的数据集合
在python中的应用
redis会默认生成0-15的库,推荐使用StrictRedis连接方式
import redis
pool = redis.ConnectionPool(host='localhost', port= 6379, db= 0)
client = redis.StrictRedis(connection_pool=pool)
统一操作
client.exists(key) #是否存在key
client.delete(key) #删除key
client.type(key) #返回key类型
client.keys(pattern) #返回匹配pattern的key
client.randomkey() #随机返回一个key
client.rename(key1, key2) #更改key1名字为key2
client.dbsize() #当前库中key数量
client.expire(key, time) #设置key存在时间
client.ttl(key) #获取key的存在时间
client.flushdb() #删除当前库中所有key
client.flushall() #删除所有库中的所有key
string类型操作
client.set(key, val)
client.get(key)
client.getset(key, val) #返回旧value
client.mget(key1, key2, ...) #返回多个key的val
client.setnx(key, value) #设置不存在的key
client.setex(key, time, value)
client.mset(key1, value1, key2, value2,...)#批量设置key,value
client.msetnx(key, value, key1,value1)
client.incr(key) # value + 1
client.incrby(key,integer) # value + integer
client.decr(key) # value - 1
client.decrby(key, integer) #value - integer
client.appent(key, value) #附加value
client.substr(key, start, end) #返回子串
list类型操作
client.rpush(key, value) #尾部添加
client.lpush(key, value) #头部添加
client.llen(key) #长度
client.lrange(key, start, end) #
client.ltrim(key, start, end)
client.lindex(key, index)
client.lset(key, index, value)
client.lrem(key, count, value)
client.lpop(key)
client.rpop(key)
client.blpop(key1, key2, ...)
client.brpop(key1, key2, ...)
client.rpoplpush(srckey, dstkey)
set类型操作
client.sadd(key, value)
client.srem(key, value)
client.spop(key)
client.smove(skey, dkey, value)
client.scard(key)
client.sismember(key, member)
client.sinter(key1, key2,...)
client.sinterstore(dstkey, key1, key2, ...)
client.sunion(key1, key2, ...)
client.sunionstore(dstkey, key1, key2, ...)
client.sdiff(key1, key2, ...)
client.sdiffstore(dstkey, key1, key2, ...)
client.smembers(key)
client.srandmember(key)
zset类型操作
client.zadd(key, score, value)
client.zrem(key,value)
client.zincrby(key, increment, member)
client.zrank(key, value)
client.zrevrank(key, value)
client.zrange(key, start, end)
client.zrevrange(key, start, end)
client.zrangebyscore(key, min, max)
client.zcard(key)
client.zscore(key, element)
client.zremrangebyrank(key, min, max)
client.zremrangebyscore(key, min, max)
client.zunionstore(dstkeyN, key1,…,keyN, WEIGHTS w1,…wN, AGGREGATE SUM|MIN|MAX)
hash类型操作
client.hset(key, field, value)
client.hget(key, field)
client.hmget(key, field1, …,field N)
client.hmset(key, field1, value1,…,field N, value N)
client.hincrby(key, field, integer)
client.hexists(key, field)
client.hdel(key, field)
client.hlen(key)
client.hkeys(key)
client.hvals(key)
client.hgetall(key)
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。