|
r = redis.Redis(ip, port, index)
如此实例化一个redis对象,index取值0-15,一个redis对象有16个库。
Keys
函数
| 功能
| 返回值
| 备注
| keys(self, pattern='*')
| 返回匹配pattern的keys列表,不存在则返回空列表
| 返回匹配pattern的keys列表,不存在则返回空列表
| http://www.redisdoc.com/en/latest/key/keys.html
| randomkey(self)
| 随机返回一个键,如果数据库为空则返回None
| 随机返回一个键,如果数据库为空则返回None
| http://www.redisdoc.com/en/latest/key/randomkey.html
| delete(self, *names)
| 删除一个或几个键,忽略不存在的键
| 被删除 key 的数量
| http://www.redisdoc.com/en/latest/key/del.html
| exists(self, name)
| 判断某个键是否存在
| 存在返回True,否则返回Fasle
| http://www.redisdoc.com/en/latest/key/exists.html
| ttl(self, name)
| 返回过期时间,以秒为单位
| 返回生存时间或者None
| http://www.redisdoc.com/en/latest/key/ttl.html
| expire(self, name, time)
| 设置生存时间,接受timedelta对象或者整型(代表秒数)
| 返回True or False
| http://www.redisdoc.com/en/latest/key/expire.html
| expireat(self, name, when)
| 设置过期时间点,接受datetime对象或者整型(代表秒数)
| 返回True or False
| http://www.redisdoc.com/en/latest/key/expireat.html
| persist(self, name)
| 移除过期时间
| 返回True or False
| http://www.redisdoc.com/en/latest/key/persist.html
| rename(self, src, dst)
| 将src改名为dst,若src不存在引发一个异常, 若dst存在则覆盖dst
| 返回True or False
| http://www.redisdoc.com/en/latest/key/rename.html
| renamenx(self, src, dst)
| 同rename,若dst存在则返回False
| 返回True or False
| http://www.redisdoc.com/en/latest/key/renamenx.html
| move(self, name, db)
| 将当前数据库的 key 移动到给定的数据库 db 当中
| 返回True or False
如果当前数据库(源数据库)和给定数据库(目标数据库)有相同名字的给定key ,或者 key 不存在于当前数据库,那么 MOVE 没有任何效果
| http://www.redisdoc.com/en/latest/key/move.html
| sort(self, name, start=None, num=None, by=None, get=None,
desc=False, alpha=False, store=None, groups=False)
| 对list,set,sorted-set进行排序,设置很多
| 没有使用 STORE 参数,返回列表形式的排序结果。
使用 STORE 参数,返回排序结果的元素数量
| http://www.redisdoc.com/en/latest/key/sort.html
| type(self, name)
| 返回key关联值的类型
| 返回值的类型
| http://www.redisdoc.com/en/latest/key/type.html
|
defdump(self, name): # 序列化一个键的值
"""
Return a serialized version of the value stored at the specified key.
If key does not exist a nil bulk reply is returned.
"""
returnself.execute_command('DUMP', name)
序列化生成的值有以下几个特点:
- 它带有 64 位的校验和,用于检测错误, RESTORE 在进行反序列化之前会先检查校验和。
- 值的编码格式和 RDB 文件保持一致。
- RDB 版本会被编码在序列化值当中,如果因为 Redis 的版本不同造成 RDB 格式不兼容,那么 Redis 会拒绝对这个值进行反序列化操作。
defrestore(self, name, ttl, value): # 反序列化一个键值
"""
Create a key using the provided serialized value, previously obtained
using DUMP.
"""
returnself.execute_command('RESTORE', name, ttl, value)
- 反序列化给定的序列化值,并将它和给定的 key 关联。
- 参数 ttl 以毫秒为单位为 key 设置生存时间;如果 ttl 为 0 ,那么不设置生存时间
- 如果序列化值不对,会报错
defttl(self, name): # 返回过期时间,以秒为单位
"Returns the number of seconds until the key ``name`` will expire"
returnself.execute_command('TTL', name)
- 当 key 不存在时,返回 -2 。
- 当 key 存在但没有设置剩余生存时间时,返回 -1 。
- 否则,以秒为单位,返回 key 的剩余生存时间。
defpttl(self, name): # 同ttl,以毫秒为单位
"Returns the number of milliseconds until the key ``name`` will expire"
returnself.execute_command('PTTL', name)
defpexpire(self, name, time): # 同expire,以毫秒为时间
"""
Set an expire flag on key ``name`` for ``time`` milliseconds.
``time`` can be represented by an integer or a Python timedelta
object.
"""
ifisinstance(time, datetime.timedelta):
ms =int(time.microseconds /1000)
time = (time.seconds + time.days *24*3600) *1000+ ms
returnself.execute_command('PEXPIRE', name, time)
defpexpireat(self, name, when): # 同expireat,以毫秒为时间
"""
Set an expire flag on key ``name``. ``when`` can be represented
as an integer representing unix time in milliseconds (unix time * 1000)
or a Python datetime object.
"""
ifisinstance(when, datetime.datetime):
ms =int(when.microsecond /1000)
when =int(mod_time.mktime(when.timetuple())) *1000+ ms
returnself.execute_command('PEXPIREAT', name, when)
string
redis-2.4.9
函数
| 功能
| 返回值
| 备注
| append(key, value)
| 追加字符串,不存在就创建
| 追加后key值的长度
| http://www.redisdoc.com/en/latest/string/append.html
| incr(self, name, amount=1)
| 将key中储存的数字值增一
| 执行 INCR 命令之后 key 的值(如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 INCR 操作。)
| 如果值的类型不对,那么返回一个错误
http://www.redisdoc.com/en/latest/string/incr.html
| incrby(self, name, amount=1)
| 将key中存储的数字增加amount
| 执行incrby之后的key值,其他情况同incr
| http://www.redisdoc.com/en/latest/string/incrby.html
| decr(self, name, amount=1)
| 把key值中的数字减一
| 返回key的值,若key不存在,则初始化key值为0,再执行decr操作,最后返回 0-amount
| 如果值的类型不对,那么返回一个错误
http://www.redisdoc.com/en/latest/string/decr.html
| set(self, name, value)
| 将字符串值 value 关联到 key
| 总是返回True
| http://www.redisdoc.com/en/latest/string/set.html
| setnx(self, name, value)
| 将 key 的值设为 value ,当且仅当 key 不存在
| 设置成功返回True
设置失败返回False
| http://www.redisdoc.com/en/latest/string/setnx.html
| get(self, name)
| 读取key的值
| 返回key关联的值,若key不存在返回None
| http://www.redisdoc.com/en/latest/string/get.html
| getset(self, name, value
| 将给定 key 的值设为 value ,并返回 key 的旧值(old value)。
当 key 存在但不是字符串类型时,返回一个错误。
| 返回给定 key 的旧值。
当 key 没有旧值时,也即是, key 不存在时,返回 none
| http://www.redisdoc.com/en/latest/string/getset.html
| mset(self, *args, **kwargs)
| 同时设置多个key值
| 总是返回True
| 接受字典这样的映射
http://www.redisdoc.com/en/latest/string/mset.html
| msetnx(self, *args, **kwargs)
| 当且仅当原key都不存在的进行批量设置
| 成功设置返回True,若有一个以上的key存在,返回False
| http://www.redisdoc.com/en/latest/string/msetnx.html
| mget(self, keys, *args
| 返回所有(一个或多个)给定 key 的值
| 返回所有key值的列表,如果某个key不存在,它的值用None代替
| http://www.redisdoc.com/en/latest/string/mget.html
| setbit(self, name, offset, value)
| 对 key 所储存的字符串值,设置指定偏移量上的位
| 返回原来位上的值(True or False)
| http://www.redisdoc.com/en/latest/string/setbit.html
| strlen(self, name)
| 返回 key 所储存的字符串值的长度
当 key 储存的不是字符串值时,返回一个错误
| 字符串值的长度。
当 key 不存在时,返回 0
| http://www.redisdoc.com/en/latest/string/strlen.html
| setex(self, name, time, value)
| 将值 value 关联到 key ,并将 key 的生存时间设为 seconds (以秒为单位)。
如果 key 已经存在, SETEX 命令将覆写旧值。
| 设置成功时返回 OK 。
当 seconds 参数不合法时,返回一个错误。
| http://www.redisdoc.com/en/latest/string/setex.html
| setrange(self, name, offset, value)
| 用 value 参数覆写(overwrite)给定 key 所储存的字符串值
| 被 SETRANGE 修改之后,字符串的长度
| http://www.redisdoc.com/en/latest/string/setrange.html
|
redis>2.4.9
[table][tr][td]函数
[/td][td]功能
[/td][td]返回值
[/td][td]备注
[/td][/tr][tr][td]bitcount(self, key, start=None, end=None)
[/td][td]计算给定字符串中,被设置为 1 的比特位的数量
[/td][td]返回1的数量
[/td][td]2.6及以上支持
http://www.redisdoc.com/en/latest/string/bitcount.html
[/td][/tr][tr][td]bitop(self, operation, dest, *keys)
[/td][td]对一个或多个保存二进制位的字符串 key 进行位元操作,并将结果保存到 dest 上
[/td][td]保存到 dest 的字符串的长度,和输入 key 中最长的字符串长度相等
[/td][td]2.6及以上支持
http://www.redisdoc.com/en/latest/string/bitop.html
[/td][/tr][tr][td]getbit(self, name, offset
[/td][td]对 key 所储存的字符串值,获取指定偏移量上的位(bit)
[/td][td]字符串值指定偏移量上的位(bit)
[/td][td]2.6及以上支持
http://www.redisdoc.com/en/latest/string/getbit.html
[/td][/tr][tr][td]getrange(self, key, start, end)
[/td][td]返回 key 中字符串值的子字符串,字符串的截取范围由 start 和 end 两个偏移量决定(包括 start 和 end 在内)
[/td][td]截取得出的子字符串
[/td][td]在 |
|