Redis常见用法
1. Python中redis模块的基本使用
redis_connect
# -*- coding:utf-8 -*- # Author:Wong Du import redis r = redis.Redis(host='localhost', port=6379, db=10) r.set('names', 'xiaohuan') print(r.get('names'))
redis_pool
# -*- coding:utf-8 -*- # Author:Wong Du import redis pool = redis.ConnectionPool(host='localhost', port=6379) r = redis.Redis(connection_pool=pool) r.set('age', '22') print(r.get('age'))
redis_管道
# -*- coding:utf-8 -*- # Author:Wong Du import redis, time pool = redis.ConnectionPool(host='111.230.64.164', port=6379, db=15) r = redis.Redis(connection_pool=pool) pipe = r.pipeline(transaction=True) pipe.set('name', 'xiaoxian') # r.set('names', 'xiaoxians') print("--------do other thing--------") pipe.set('age', 23) time.sleep(10) print("Start to exec...") pipe.execute()
2. redis介绍及命令行常见用法
redis常见用法 连接、连接池 常用操作 String Hash List Set Sort Set 管道 发布订阅 连接、连接池 通过连接池来管理对一个redis server的所有连接,可以避免每次建立、释放连接的开销。 默认,每个redis实例都会维护一个自己的连接池。 可以建立一个连接池,然后作为参数redis,这样可以实现多个redis实例共享一个连接池。 String操作 redis中的string在内存中按照一个key对应一个value来存储,类似于python中的变量 SET key value [EX seconds] [PX milliseconds] [NX|XX] 在redis中,set命令,若key存在则修改,不存在则创建 示例:set name china [ex 3] [nx|xx] ex 指定这个key存活多少秒(即过期时间) px 指定这个key存活多少毫秒 nx 只有当key不存在时,命令才执行 xx 只有当key存在时,命令才执行 SETNX key value == set key value nx summary: Set the value of a key, only if the key does not exist 示例:setnx name china SETEX key seconds value summary: Set the value and expiration of a key(设置key的过期时间秒) 示例:setex name 3 china PSETEX key milliseconds value summary: Set the value and expiration in milliseconds of a key(设置key的过期时间毫秒) 示例:psetex name 3 china MSET key value [key value ...] summary: Set multiple keys to multiple values(批量设置) 示例:mset name1 china name2 usa name3 japan GET key summary: Get the value of a key(获取key的value) 示例: get name MGET key [key ...] summary: Get the values of all the given keys(获取keys的values) 示例:mget key1 key2 GETSET key value summary: Set the string value of a key and return its old value(设置新的value并返回旧的value) 示例:getset name china2 SETRANGE key offset value summary: Overwrite part of a string at key starting at the specified offset(修改字符串内的字符) 示例:setrange name 0 C (效果:china -> China) SETBIT key offset value summary: Sets or clears the bit at offset in the string value stored at key(对value的二进制位进行操作) 示例:setbit name 8 1 **用途举例,用最省空间的方式,存储在线用户数及分别是哪些用户在线 GETBIT key offset summary: Returns the bit value at offset in the string value stored at key(获取对应二进制位的值0或1) 示例:getbit name 10 BITCOUNT key [start end] summary: Count set bits in a string(统计字符串中二进制位为1的个数) 示例:bitcount name [0 10] STRLEN key summary: Get the length of the value stored in a key(返回value的字节长度) 示例: strlen name INCR key summary: Increment the integer value of a key by one(自增key对应的值) 示例: incr names2 DECR key summary: Decrement the integer value of a key by one(自减key对应的值) 示例: decr names2 APPEND key value summary: Append a value to a key(在value后面追加内容) 示例: append name " is very strong" Hash操作 redis中的Hash在在内存中按照一个key对应一个dict来存储 hash表现形式上类似pyhton中的dict,可以存储一组关联性较强的数据 HSET key field value summary: Set the string value of a hash field(表现形式:key={field:value}) 示例:hset info name china 创建或修改 HMSET key field value [field value ...] summary: Set multiple hash fields to multiple values(批量创建或修改) 示例:hmset info name1 china name2 usa name3 japan HGET key field summary: Get the value of a hash field(查看) 示例:hget info name1 HMGET key field [field ...] summary: Get the values of all the given hash fields(查看多个) 示例: hmget info name1 name2 name3 HGETALL key summary: Get all the fields and values in a hash(查看hash内的所有信息) 示例: hgetall info HLEN key summary: Get the number of fields in a hash(查看hash内所有fields的个数) 示例:hlen info HKEYS key summary: Get all the fields in a hash(查看hash内所有的fields) 示例: hkeys info HVALS key summary: Get all the values in a hash(查看hash内所有的values) 示例:hvals info HEXISTS key field summary: Determine if a hash field exists(判断hash内是否存在指定的field,存在返回1) 示例:hexists info name HDEL key field [field ...] summary: Delete one or more hash fields(删除一个或多个field) 示例:hdel info name3 name2 HINCRBY key field increment summary: Increment the integer value of a hash field by the given number(value自增) 示例:hincrby info2 name 1 HSCAN key cursor [MATCH pattern] [COUNT count] summary: Incrementally iterate hash fields and associated values(可迭代对象获取fields和values) 示例:hscan info 0 List操作 redis中的List在在内存中按照一个key对应一个List来存储,类似于python中的list LPUSH key value [value ...] summary: Prepend one or multiple values to a list(创建或添加key里的value,从左) 示例:lpush list role1 role2 LPUSHX key value summary: Prepend a value to a list, only if the list exists(当list存在时添加) 示例:lpushx list role3 LLEN key summary: Get the length of a list(获取key中元素的个数) 示例:llen list LSET key index value summary: Set the value of an element in a list by its index(通过下标修改指定的value) 示例:lset list 1 role100 LREM key count value summary: Remove elements from a list(删除list中指定的value,若有多个,可用count指定删除多个) 示例:lrem list 2 role1 LPOP key summary: Remove and get the first element in a list(移除左边第一个value并返回) 示例:lpop list LINDEX key index summary: Get an element from a list by its index(通过下标获取指定的value) 示例:lindex list 1 LRANGE key start stop summary: Get a range of elements from a list(查看指定范围的value) 示例:lrange list 0 11 LTRIM key start stop summary: Trim a list to the specified range(修剪list到指定范围,相当于截取) 示例:ltrim list 0 5 RPOPLPUSH source destination summary: Remove the last element in a list, prepend it to another list and return it(删除原list的右边第一个value并返回,然后从目标list的左边添加进去) 示例:rpoplpush list list2 BLPOP key [key ...] timeout summary: Remove and get the first element in a list, or block until one is available(若list为空,则阻塞,否则删除左边第一个value,0表示永远阻塞) 示例:blpop list 30 BRPOPLPUSH source destination timeout summary: Pop a value from a list, push it to another list and return it; or block until one is available(删除原list的右边第一个value并返回,然后从目标list的左边添加进去,若原list为空,则阻塞) 示例:brpoplpush list list2 0 Set集合操作 SADD key member [member ...] summary: Add one or more members to a set(创建集合或添加members到集合中) 示例:sadd s role1 role1 role2 role3 SCARD key summary: Get the number of members in a set(统计集合中members个数) 示例: scard s SDIFF key [key ...] summary: Subtract multiple sets(key1有key2没有) 示例: sdiff s s2 SDIFFSTORE destination key [key ...] summary: Subtract multiple sets and store the resulting set in a key(key1有key2没有,放到指定的目的集合中) 示例:sdiffstore s3 s s2 SINTER key [key ...] summary: Intersect multiple sets(取交集) 示例: sinter s s2 SINTERSTORE destination key [key ...] summary: Intersect multiple sets and store the resulting set in a key(取交集并输出到另一个集合) 示例:sinterstore s4 s s2 SUNION key [key ...] summary: Add multiple sets(取并集) 示例:sunion s s2 SUNIONSTORE destination key [key ...] summary: Add multiple sets and store the resulting set in a key(取并集并输出到另一个集合) 示例:sunionstore s6 s s2 SMEMBERS key summary: Get all the members in a set(查看集合中所有members) 示例:smembers s SISMEMBER key member summary: Determine if a given value is a member of a set(判断指定member是否在集合当中) 示例:sismember s role1 SMOVE source destination member summary: Move a member from one set to another(把原集合的某个member移动到目标集合) 示例:smove s s5 role1 SPOP key [count] summary: Remove and return one or multiple random members from a set(删除集合中随机member并返回) 示例:spop s2 SRANDMEMBER key [count] summary: Get one or multiple random members from a set(从集合中随机获取1个或多个members) 示例:srandmember s3 2 SSCAN key cursor [MATCH pattern] [COUNT count] summary: Incrementally iterate Set elements(可迭代对象获取member) 示例:sscan s2 0 有序set集合操作 ZADD key [NX|XX] [CH] [INCR] score member [score member ...] summary: Add one or more members to a sorted set, or update its score if it already exists 示例:zadd z 1 age 5 name 3 role ZCARD key summary: Get the number of members in a sorted set(统计member个数) 示例:zcard z ZCOUNT key min max summary: Count the members in a sorted set with scores within the given values(统计优先级在min~max区间的members数量) 示例:zcount z 0 3 ZINCRBY key increment member summary: Increment the score of a member in a sorted set(自增指定member的优先级) 示例:zincrby z 1 role ZRANGE key start stop [WITHSCORES] summary: Return a range of members in a sorted set, by index(查看指定区间的members) 示例:zrange z 0 1 ZRANK key member summary: Determine the index of a member in a sorted set(获取指定member的index下标) 示例:zrank z role ZREM key member [member ...] summary: Remove one or more members from a sorted set(删除集合的指定成员) 示例:zrem z role name ZREMRANGEBYRANK key start stop summary: Remove all members in a sorted set within the given indexes(指定下标范围删除members) 示例:zremrangebyrank z 0 3 ZREMRANGEBYSCORE key min max summary: Remove all members in a sorted set within the given scores(指定优先级范围删除members) 示例:zremrangebyscore z 1 4 ZSCORE key member summary: Get the score associated with the given member in a sorted set(获取指定成员的优先级) 示例:zscore z role 其他常用操作 del exists keys expire rename move randomkey() type scan
静静的学习一阵子儿...