# 1, a single field HSET Key Field value HSETNX value Key Field # 2, a plurality of fields HMSET value Key Field Field value # . 3, returns the number of fields HLEN Key # . 4, determines whether there is a field (absence returns 0) HEXISTS field Key # . 5, return to the field value HGET Key field # . 6, the plurality of field values returned HMGET Filed Key field # . 7, all the value pairs returned HGETALL Key # . 8, to return all the field names HKEYS Key # . 9, returns all values HVALS Key # 10, to delete the specified field the HDEL Key field # . 11, for operation on the integer incremental values of the corresponding field HINCRBY Filed iNCREMENT Key # 12 is, perform floating-point arithmetic on the field corresponding to the incremental value HINCRBYFLOAT key field increment
# 1, an update of the attribute data, not the new HSET (name, Key, value) # 2, this attribute specifies the read data, returns the string type hget (name, Key) # . 3, batch update data (not the New) attribute parameter dictionary hmset (name, Mapping) # . 4, the batch read data (not the new) property hmget (name, Keys) # . 5, all the attributes and obtaining a value corresponding to this data, a Dictionary type hgetall (name) # 6, get all the attribute names of this data, returns a list of types hkeys (name) # 7, delete the specified attribute this data hdel (name, * keys)
python code hash hash
Import Redis R & lt = redis.Redis (Host = " 192.168.153.136 " , Port = 6379, DB = 0) # Create a key named "user1" data comprising attribute name r.hset ( " user1 " , " name " , ' zhanshen001 ' ) # change data key named "userinfo", changing the value of the property username r.hset ( " user1 " , " name " , ' zhanshen002 ' ) # extracted attribute value username username = r.hget ( "user1", "name") # 输出看一下 print('name',username) # 属性集合 user_dict = { "password": "123456", "name": "Wang Success", "sex": "male", "height": '178', "Tel": '13838383888 ' , } # add bulk properties r.hmset ( " user1 " , user_dict) # remove all data (return value Dictionary) all_data = r.hgetall ( " UserInfo " ) Print ( ' all_data: ' , all_data) # Delete Attribute (you can bulk delete) r.hdel ( " user1 " , " Tel " ) # remove all attribute names: list h_keys = r.hkeys ( " user1 " ) Print ( ' all_key_name: ', h_keys) # remove all attribute values: List h_values = r.hvals ( ' user1 ' ) Print ( ' all_values: ' , h_values)
Scenarios: redis + mysql + hash combination
Import redis Import pymysql # . 1, redis to query the personal information # 2, not redis query, the query to mysql, and cached to redis # . 3, access to personal information again R & lt = Host redis.Redis (= ' 192.168.153.136 ' , Port = 6379, DB = 0) username = iNPUT ( ' enter username: ' ) # If no cache redis, empty dictionary is returned} { Result = r.hgetall (username) Print ( ' redis found: ' , the Result) # MySQL table fields: username, password, Gender, Age IF not the Result: db = pymysql.connect ('172.0.0.1','root','123456','spider',charset='utf8') cursor = db.cursor() cursor.execute('select gender,age from user where username=%s',[username]) # (('m',30),) userinfo = cursor.fetchall() if not userinfo: print('MySQL中用户信息不存在') else: dict = { ' Gender ' : UserInfo [0] [0], ' Age ' : UserInfo [0] [. 1 ] } # hmset second parameter dictionary r.hmset (username, dict) # set the expiration time of 5 min r .expire (username, 60 *. 5 ) Print ( ' Redis of cache hits ' )
Mysql database data updated cache information synchronized to redis
Import redis Import pymysql # after update data (mysql), synchronized to the buffer redis DEF update_mysql (Age, username): DB = pymysql.connect ( ' 127.0.0.1 ' , ' the root ' , ' 123456 ' , ' the userdb ' , charset = ' UTF8 ' ) Cursor = db.cursor () UPD = ' Update User SET WHERE Age =% S% S = username ' the try : # code: 0 or. 1 code = cursor.execute(upd, [age, username]) db.commit() if code == 1: return True except Exception as e: db.rollback() print(e) cursor.close() db.close() def update_redis(age): r = redis.Redis(host="127.0.0.1", port=6379, db=0) r.hset('user', 'age', age) print('已同步至redis') # 设置过期时间 r.expire('User ' , 30 ) # Test Print (r.hget ( ' User ' , ' Age ' )) IF the __name__ == ' __main__ ' : username = INPUT ( ' Enter Username: ' ) Age = INPUT ( ' Enter more the age-: ' ) IF update_mysql (Age, username): update_redis (Age) the else : Print ( " user name is incorrect " )