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

函数

功能

返回值

备注

bitcount(self, key, start=None, end=None)

计算给定字符串中,被设置为 1 的比特位的数量

返回1的数量

2.6及以上支持

http://www.redisdoc.com/en/latest/string/bitcount.html

bitop(self, operation, dest, *keys)

对一个或多个保存二进制位的字符串 key 进行位元操作,并将结果保存到 dest 上

保存到 dest 的字符串的长度,和输入 key 中最长的字符串长度相等

2.6及以上支持

http://www.redisdoc.com/en/latest/string/bitop.html

getbit(self, name, offset

对 key 所储存的字符串值,获取指定偏移量上的位(bit)

字符串值指定偏移量上的位(bit)

2.6及以上支持

http://www.redisdoc.com/en/latest/string/getbit.html

getrange(self, key, start, end)

返回 key 中字符串值的子字符串,字符串的截取范围由 start 和 end 两个偏移量决定(包括 start 和 end 在内)

截取得出的子字符串

在 <= 2.4 的版本里,GETRANGE 被叫作 SUBSTR

http://www.redisdoc.com/en/latest/string/getrange.html

       

Redis命令参考(Keys & String)的更多相关文章

  1. redis命令参考和redis文档中文翻译版

    找到了一份redis的中文翻译文档,觉得适合学习和查阅.这份文档翻译的真的很良心啊,他是<Redis 设计与实现>一书的作者黄健宏翻译的. 地址:http://redisdoc.com/i ...

  2. Redis 命令参考

    Redis 命令参考 http://redis.readthedocs.org/en/latest/index.html

  3. Redis命令参考之复制(Replication)

    Redis 支持简单且易用的主从复制(master-slave replication)功能, 该功能可以让从服务器(slave server)成为主服务器(master server)的精确复制品. ...

  4. redis命令参考

    http://doc.redisfans.com/ 进入redis命令行模式方式: 1.进入redis安装目录 2.运行redis-cli

  5. Redis命令行之String

    一.Redis之String简介 1. String是redis最基本的数据类型,一个key对应一个value. 2. String是二进制安全的,可以包含任何数据,例如图片或序列化的对象. 3. S ...

  6. Redis命令参考【EXPIRE】

    EXPIRE EXPIRE key seconds 为给定 key 设置生存时间,当 key 过期时(生存时间为 0 ),它会被自动删除. 在 Redis 中,带有生存时间的 key 被称为『易失的』 ...

  7. 常用 redis 命令(for php)

    Redis 主要能存储 5 种数据结构,分别是 strings,hashes,lists,sets 以及 sorted sets. 新建一个 redis 数据库 $redis = new Redis( ...

  8. Redis命令大全&中文解释&在线测试命令工具&在线中文文档

    在线测试命令地址:http://try.redis.io/ 官方文档:http://redis.io/commands http://redis.io/documentation Redis 命令参考 ...

  9. redis命令Keys(九)

    常用命令 1>keys 返回满足给定pattern 的所有key redis 127.0.0.1:6379> keys mylist* 1) "mylist" 2) & ...

随机推荐

  1. sqlserver 2008 R2 分区表测试

    有一张表期中有100多w条数据 程序执行起来比较慢,想用分区表的办法,使查询变快一些. 方案如下 --查看分区信息SELECT * FROM sys.partition_range_values -- ...

  2. jquery优化28个建议

    我一直在寻找有关jQuery性能优化方面的小窍门,能让我那臃肿的动态网页应用变得轻便些.找了很多文章后,我决定将最好最常用的一些优化性能的建议列出来.我也做了一个jQuery性能优化的简明样式表,你可 ...

  3. STM32 串口DMA方式接收(转)

    STM32 是一款基于ARM Cortex-M3内核的32位MCU,主频最高可达72M.最近因为要在车机上集成TPMS功能, 便开始着手STM32的开发工作,STM32F10x系列共有5个串口(USA ...

  4. python 调试

    python 调试基本和gdb调试一样,举例: debug .py #!/usr/bin/python   print "hello" i=0 for j in range(10) ...

  5. 在Visual Studio中利用NTVS创建Pomelo项目

    刚看新闻,才知道微软发布了Node.js Tools for Visual Studio(NTVS),受够了WebStorm输入法Bug的困扰,这下终于可以解脱了.以Pomelo为例,运行命令:pom ...

  6. HDU-4699 Editor 数据结构维护

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4699 题意:开始有一个光标,每次有5中操作:1,光标当前位置插入一个数,2,光标当前位置删除一个,3, ...

  7. Java管道流PipedStream

    管道读取流和管道写入流可以像管道一样对接上,管道读取流就可以读取管道写入流写入的数据.需要注意的是需要加入多线程技术,因为单线程,先执行read,会发生死锁,因为read方法是阻塞式的,没有数据的re ...

  8. SQL2008-查询库中是否存在某表

    select   *   from   sysobjects   where   name= 'N201111B'   and   xtype= 'U'

  9. SQLServer2005数据导入Mysql到详细教程

    如果转载请注明转载地址,谢谢. SQL SERVER数据导入MYSQL目录 1.Navicat for MySQL 版本10.0.9 2.创建目标数据库 3.创建正确的SQL SERVER数据库ODB ...

  10. 微软.NET各子技术领域的应用前景

    从2002年微软发布.NET 1.0,其间历经了8年的发展,再到.NET 4.0,其已经成为一个庞大而复杂的软件开发与运行平台,架构日益复杂,应用领域也在不断地扩展,包容了“一堆”的子技术领域. 在. ...