Python Operation Redis

Source: Internet
Author: User

All of the above is the direct operation of Redis in the database command line, now learning to use Python Redis package to operate Redis, I installed redis==2.10.6, not familiar with redis command line operation of Friends, please refer to:

Redis Foundation One

Redis Foundation II

Python's interaction with Redis
    • Installation:

Environment: ubuntu16.04;redis-4.0.6

$ sudo pip redis
    • Test
$ ipythonimport redis    # 出现版本信息,安装成功
Redis Instantiation Objects
import== redis.StrictRedis()# redis有上面两个类可以建立连接对象,用法几乎一样,推荐使用StrictRedis.= redis.StrictRedis(host=‘localhost‘,  # 默认ip为127.0.0.1                      port=6379,   # 默认端口6379                      db=0,        # 默认数据库0                      password=None# 默认没有密码
Redis Operation String Type
    • Increase
print(con.set(‘num‘‘3‘))  # string类型的插入键值对,参数key,value;返回true或Falseprint(con.setex(‘name‘,3,‘xiaobai‘# 设置一个有时间的键值对,返回true或falseprint(con.setnx(‘my‘,‘hh‘))  # 设置单个不存在的键值对print(con.mset({‘my‘:‘cai‘}))  # 设置多个键值对,返回true或falseprint(con.msetnx({‘you‘:‘tan‘,‘my‘:‘wan‘# 为不存在的键设置值,如果有一个已经存在,整个会失败,返回false
    • Check
print(con.get(‘num‘))       # 获取键的值,返回二进制的数据print(con.getrange(‘name‘, 0, 2))  # 获取子串,参数key、下标范围print(con.strlen(‘name‘))  # 获取值的长度print(con.exists(‘name‘)) # 是否存在这个键,针对所有的类型
    • Change
Print(CON.INCR (' num '))# Value self-add operation, return calculated result valuePrint(CON.INCR (' num ',' All '))# value plus an integer operation that returns the computed result value,Print(Con.incrby (' num ',' 5 '))# value plus an integer operation that returns the computed result valuePrint(Con.incrbyfloat (' num ',' 3.5 '))# value plus a floating-point number operation that returns the computed result valuePrint(Con.incrbyfloat (' num ',' -3.5 '))# value plus a floating-point number operation that returns the computed result valuePrint(CON.DECR (' num '))# Value-decrement operation, returns the computed result valuePrint(CON.DECR (' num ',' 5 '))# value minus an integer operation, returning the computed result valuePrint(Con.append (' Boy ',' Tom '))# string concatenation, parameter key value, returns the length of the current stringPrint(Con.setrange (' name ',' 5 ',' hh '))# string substitution character, parameter is key, offset, value, returns the length of the current string
    • By deleting
print(con.delete(‘my‘,‘name‘))  # 删除多个键值对,获取删除成功的数量
Redis Operations List Type
    • Increase
print(con.rpush(‘list‘‘v1‘‘v2‘))  # 从列表的首部插入,参数键、值,值可多个,返回列表的长度print(con.lpush(‘list‘‘vx‘))        # 从列表的尾部插入,参数键、值,值可多个,返回列表的长度con.lset(‘list‘,0,20)                 # 在列表的下标0位置插入20
    • Check
print(con.lrange(‘list‘,0,-1))  # 获取范围内的元素,返回一个列表,原来的列表不变
    • By deleting
print(con.lpop(‘list‘))   # 从列表首部弹出一个元素print(con.rpop(‘list‘))  # 从列表尾部弹出一个元素print(con.blpop(‘list‘,3))  # 从列表首部弹出一个元素,在3秒内阻塞等待,默认为0秒;没有获取值返回noneprint(con.brpop(‘list‘3))  # 从列表右端弹出一个元素,在3秒内阻塞等待,默认为0秒
    • Change
print(con.rpoplpush(‘list‘‘list1‘# 将list最右端的元素移到list1的最左端,返回移动的元素# 将list最右端的元素移到list1的最左端,返回移动的元素,等待时间3秒,默认0秒print(con.brpoplpush(‘list‘‘list1‘3))print(con.ltri(‘list‘2-1# 剪切列表,返回剪切后的列表,改变了原来的列表
Redis Operation Hash
    • Increase
print(con.hset(‘fruit‘,‘apple‘,‘red‘# 设置单个散列键值对,参数name,key,value,返回true或falseprint(con.hmset(‘color‘, {‘blue‘:‘good‘,‘green‘:‘bad‘}))  # 对个散列键值对,返回true或falseprint(con.hsetnx(‘goods‘‘color‘‘white‘))
    • By deleting
print(con.hdel(‘color‘,‘blue‘))  # 删除散列的某些键值对,至少成功一个返回true,其他返回False
    • Check
Print(Con.hget (' Color ',' Green '))# Get a single value for a hashPrint(Con.hmget (' Color ',' Green ',' Blue '))# Get multiple values for a hashPrint(Con.hkeys (' Color '))# gets all the keys of the hashPrint(Con.hvals (' Color '))# gets all the values of the hashPrint(Con.hgetall (' Color '))# Get hash of all key-value pairs, return a binary-coded dictionaryPrint(Con.hexists (' Color ',' Red '))# To determine if a hash exists for a key, return True or FalsePrint(Con.hstrlen (' Color ',' Blue '))# Gets the length of the value string, returns a numberPrint(Con.hlen (' Color '))# Gets the number of key-value pairs for the hash
    • Change
print(con.hincrby(‘color‘‘blue‘3# 将散列的值加一个整数,默认加1print(con.hincrbyfloat(‘color‘‘blue‘3.5))  # 加一个浮点数,默认为1.0
Redis Operations Collection
    • Increase
print(con.sadd(‘name‘‘a‘‘b‘))  # 添加元素,返回添加的个数
    • By deleting
print(con.srem(‘name‘,‘a‘))  # 移除元素,返回移除元素的数量print(con.spop(‘name‘))  # 随机的移除一个元素
    • Change
print(con.smove(‘name‘,‘name1‘,‘bb‘# 将bb从name移到name1,如果name1不存在,创建;返回true或false
    • Check
print(con.smembers(‘name‘))  # 返回集合所有的元素,以字典的形式print(con.scard(‘name‘))  # 返回集合元素的个数print(con.sismember(‘name‘,‘b‘)) # 查看元素是否在集合中,返回true或false
Other Redis functions
    • Clear the current database
    • Clear Current Redis
con.flushall()
    • Set expiration time for keys
con.expire(‘name‘,3)  # 单位为秒
    • View server time
con.time()  # 返回时间戳和当前已过去的微秒数
    • See how many lifetimes the key still has
con.ttl(key) # 时间单位为秒
    • See all the Keys
con.keys()  # 默认参数为‘*‘
    • Write Redis on a command-line basis
con.execute_command(‘SET‘,‘my‘, ‘hhhh‘)
Pipeline
    • Pipelines can cache commands, reducing the number of times clients interact with Redis-server
# 创建一个管道值= con.pipeline()pipe.get(‘name‘)pipe.set(‘name‘,‘xiao‘)# 提交pipe.execute()

Python Operation Redis

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.