The hash type of the Python Redis operation other common operations

Source: Internet
Author: User

1.hash type Operation

Import Redispool = Redis. ConnectionPool (host= "192.168.48.131", port=6379, db=0) R = Redis. Redis (Connection_pool=pool)

#hash类型操作: is a name corresponding to a dictionary

#语法 Hset (name, key, value)

A key-value pair is set #name the corresponding hash (does not exist, the key-value pair is created; otherwise, the key-value pair is modified)

# syntax parameter explanation:

Name:redis's hash name

Key:key1 corresponding to the key in the hash

Value:value1 value in the corresponding hash

#注意: Hsetx (name, key, value), created (equivalent to add) if the current key does not exist in the hash of name.


Hget (Name,key)

#在name对应的hash中获取根据key获取value

Hmset (name,mapping)

#在name对应的hash中批量设置键值对

#参数:

#mapping: Dictionary, as {' K1 ': ' v1 ', ' K2 ', ' v2 '}

#例:

R.hmset (' Test ', {' K1 ': ' v1 ', ' K2 ': ' v2 '}) print (R.hmget (' Test ', ' K1 ', ' K2 ')) #结果 [b ' v1 ', B ' v2 ']


Hmget (Name,keys,*args)

#在name对应的hash中获取多个key的值

#参数:

#keys: To get a collection of keys, such as: [' K1 ', ' K2 ', ' K3 ']

#*args: The key to get, such as: K1,K2,K3

#例:

R.hmset (' Test ', {' K1 ': ' v1 ', ' K2 ': ' v2 '}) print (R.hmget (' Test ', ' K1 ', ' K2 ')) #获取多个值


Hgetall (name)

Gets the name of all the key values corresponding to the hash

R.hmset (' Test ', {' K1 ': ' v1 ', ' K2 ': ' v2 '}) print (R.hgetall (' Test ')) #结果: {b ' k2 ': B ' v2 ', B ' K1 ': B ' v1 '}


Hlen (name)

Gets the number of key values in the hash for name

R.hmset (' Test ', {' K1 ': ' v1 ', ' K2 ': ' v2 '}) print (R.hlen (' Test '))


Hkeys (name): Gets the value of all keys in the hash corresponding to name

hvals (name): Gets the value of all the values in the hash corresponding to name

#举例: R.hmset (' Test ', {' K1 ': ' v1 ', ' K2 ': ' v2 ') ' Print (R.hkeys (' Test ')) ', ' (r.hvals (' Test ') ') #结果: [B ' K2 ', b ' K1 '][b ' v2 ', B ' V1 ']


hexists (Name,key): checks if the hash name corresponds to the current key being passed in

Hdel (Name,*keys): deletes the key value pair of the specified key in the hash name corresponding to

Example:

R.hmset (' Test ', {' K1 ': ' v1 ', ' K2 ': ' v2 '}) print (r.exists (' Test ')) print (R.hdel (' Test ', ' K1 ') "Print (R.hmget (' Test ') ', ' K1 ', ' K2 '))


2. Other common operations

#根据name删除redis中的任意数据类型

Delete (*names)


#检测redis的name是否存在

Exists (name)


#根据 *? Wait for wildcard matches to get the name of a Redis

Keys (pattern= ' * ')


# Set a time-out for a name

Expire (name, time)


# rename

Rename (src, DST)


# move one of the Redis values to the specified db

Move (name, DB))


# Gets the type of the name corresponding value

Type (name)


The hash type of the Python Redis operation other common operations

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.