Python Operation Redis

Source: Internet
Author: User
Tags set set

Yesterday, we discussed how Python connects to Redis. Today we are going to learn how to manipulate Redis data in Python.

Here, please add. Redis is a database in the form of Key-value. You can store data in multiple formats.

Today, we are trying to do these common APIs.

1. Set the data:

R = Redis. Redis (host= ' 127.0.0.1 ', port=6379)

#若是key存在, just modify, do not exist, add
R.set (' zcx ', ' 123999 ')

#time: Refers to the cache expiration time, in units of seconds

R.setex (Key,value, Time)

#time: Refers to the cache expiration time, in units of seconds

R.setex (Key,value, Time)

#time: Refers to the cache expiration time, in milliseconds

R.psetex (Key,value, Time)

All two of them are in batch setup.

R.mset (zcx= ' 123 ', zcx1= ' 456 ')
R.mget ({"ZCX": ' 888 ', "zcx1": ' 999 '})

2. Get Data

Get (name)

Bulk acquisition:

R.mget ("Zcx", "zcx1")
Li = ["Zcx", "zcx1"]
R.mget (LI)

3. Get the original value and set a new value: Notice that get and set are spelled together

Getset (name, value)

4. Value by length of data content

GetRange (key, start, end)

Print (R.getrange (' Zcx ', 0, 1))

5. Insert a value at the specified position of the corresponding value according to key

SETRANGE (key, offset, value)

6. Bit operation on 2 binary

Setbit (key, offset, value)

7. Get the value of a 2 binary bit

Getbit (name, offset)

8. Get the value of a paragraph in the 2 binary

Bitcount (Key, Start=none, End=none)

9. Return the length of the corresponding data

Strlen (Key)

10. Adding operations to a number

INCR (self, key, amount=1)

R.INCR (' zcx ', 1)
Print (R.get (' zcx '))
R.INCR (' Zcx ', 5)
Print (R.get (' zcx '))

Output:

B ' 124 '

B ' 129 '

Grow by following the decimals

Incrbyfloat (self, name, amount=1.0)

Contrary to the function of INCR

DECR (self, key, amount=1)

11. Append content to name

Append (key, value)

12. Accessing the Dictionary

R.hset ("Zcx", "zcx1", "8888")
Print (R.hget ("Zcx", "zcx1"))

#取所有的字典

Print (R.hgetall ("ZCX"))
Output:

B ' 8888 '

{b ' zcx1 ': B ' 8888 '}

13. Bulk Access Dictionary

DIC = {"Zcx": "111111", "zcx1": "2222222"}
R.hmset ("My", dic)
Print (R.hget ("My", "zcx"))
Print (R.hgetall ("my"))

test=["Zcx", "zcx1"]
Print (R.hmget ("My", test))
Print (R.hmget ("My", "zcx", "zcx1"))

14. Get the dictionary's properties and corresponding values: length, number

DIC = {"ZCX": "1111", "zcx1": "2222"}
R.hmset ("My", dic)
#获取长度
Print (R.hlen ("my"))
#获取所有key
Print (R.hkeys ("my"))
#获取所有值
Print (R.hvals ("my"))

15. Determine if there is

Print (R.hexists (' My ', ' zcx '))

Output:

True

16. Remove the key from the dictionary

Hdel (' My ', ' zcx ')

17. List Operations

Lpush (Key,val)

#每个新的元素都添加到列表的最右边

Rpush (Key,val)

#在key对应的list中添加元素, the value is added to the leftmost/right of the list only if the key already exists

Lpushx (Key,value)

Rpushx (Key,value)

Llen (Key) #求长度

R.linsert ("My", "before", "55555", "66666") #在前后插入值

R.lset (key, index, value) #对某个位置的数据重新复制

R.lrem (Key, Value,position) #position删除指定位置的值. Position>0 from left to right, otherwise right-to-left

Lpop (Key) #移除元素

Lindex (key, index) #根据索引取元素

Lrange (key, start, end) #根据索引取范围数据

LTrim (key, start, end) #移除列表内没有在该索引之内的值

Rpoplpush (SRC, target) #合并列表. Remove the rightmost element from SRC and add it to the far left of target

Blpop (key, timeout) #移除数据

18.Set Collection Operations

Set set is a list of elements that are not allowed to be duplicated

Sadd (Key,value) #设置值

Smembers (Key) #获取值

SCard (Key) #获取长度

Sdiff (keys, *args) #取差

Sdiffstore (target, Keys, *args) #把sdiff获取的值加入到target中

Sinter (Key, *args) #取并

Sinterstore (target, Keys, *args) #获取多个key对应集合的并集 and joined to target

Sismember (key, value) #检查元素

Smove (SRC, target, value) #把某个元素从一个集合中移动到另外一个集合

Spop (Key) #移除

Srandmember (key, num) #随机取值, num is length

Srem (key, values) #删除key中的某些值

Sunion (keys, *args) #获取多个keys对应的集合的并集

Sunionstore (Target,keys, *args) #sunion的结果存入target

Zadd (Key, *args, **kwargs) #有序集合

Zcard (Key) #有序集合的元素

Zcount (Key, Min, max) #有序集合的元素在min和max范围中的数

Zincrby (key, value, amount)

# Gets the elements of an ordered set of keys corresponding to the index range
Result=r.zrange ("My", 0,1,desc=false,withscores=true,score_cast_func=int)
Print (Result
"' Parameter:
Key
Start ordinal set Index start position
End ordered collection index ending position
Desc collation, by default sorted by fractions from small to large
Withscores whether to get the fraction of an element, by default only gets the value of the element
Score_cast_func function for data conversion of fractions

Zrevrange (key, start, end, Withscores=false, Score_cast_func=float) #同zrange, the collection is sorted from large to small

Zrank (key, value), Zrevrank (key, value) #获取元素所在位置

Zscore (key, value)

Zrem (Key, Val) #删除key对应的有序集合中值是val成员

Zremrangebyrank (Key,min, max) #根据范围删除

Zremrangebyscore (Key, Min, Max)

Zinterstore (target, Keys, Aggregate=none) #合并集合. Perform Cartesian product operations

Zunionstore (target, Keys, Aggregate=none) #合并集合放入target

Delete (*names) #删除

Exists (name)

Keys (pattern= ' * ') #模式匹配

Expire (Key,time) #设置超期时间

Rename (A, b)

Move (key, db)) #将可以移动到另一个db下

Randomkey () #生成随机key

Type (key) #获取可以对应的类型

These are some of the APIs that Python operates with Redis. In fact, in the actual application, can use One-third is good. However, it is important to understand which APIs are available and what to think about when you use them in the future.

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.