Operate Redis --day7

Redis is also a database, and its storage is stored in the form of key-value , such as:
 

a. Relational database

For example: 
   mysql, oracle, sql server, db2, sqlite databases are relational databases. Data
is queried and manipulated through sql statements. The
   data is stored in the form of tables. The
data is stored on the disk
. , do not need sql query to obtain data directly use: get ('k') add data use: set ('xx') redis data is all stored in memory. Therefore, its operation speed is faster than that of relational databases. The performance of redis itself is very good, and it supports 30w reads and writes per second.







First define a database:
r = redis.Redis(host='118.24.3.XX',password='abc*&js',db=1,port=6379)
1. There are two ways to add a key to the database: 
  r.set('fancy','today is friday')
  or directly right-click on the database name and select "add new key"
2. Modify the key and add it, use set
3. Delete the key, r.delete('fancy')
4. Set the expiration time of the key, the last parameter is seconds, r.setex('fancy','hahaha',20)
add:
s='Haha'
s .encode() #Convert string to binary
hwt = b'sdsdfdjkj'
hwt.decode() #Convert bytes type to string
5. Get all keys:
hwt = r.get('hwt')
# print( hwt.decode())
 print(r.keys()) #Get all the keys
print(r.keys(nn**)) #Get the keys starting with nn
6. Add a folder under the key:
 r.set ('Pisces: fancy','hello') #The name of the folder before the colon, if there are multiple folders, use a colon to connect, for example: r.set('Pisces: fancy1: fancy2: fancy3','hello ') as shown in
the figure:

7. Get the value under the folder: print(r.get('Pisces: fancy')) Result: b'hello'

8. Delete all keys:

  for k in r.keys():

    r.delete (k)

All the above operations are for the string type in redis.

The following is an introduction: hash type hash
1.r.hset('stu_info','fancy','200, we all need to be good')

   2. Check what type of Key is: print(r.type(stu_info))

   3. Take the key: print(r.hget('stu_info','Zhang flow').decode()) #Specify the big key and the small key to get the corresponding data, the return value is byte type

  print(r.hgetall('stu_info')) #Get all k and -v inside, return a dictionary, the value is byte type

    As shown in the figure:

 Convert the above binary dictionary to decimal,

 #stu_info  = r.hgetall('stu_info')
# new_stu_info = {}
# for k,v in stu_info.items():
#  new_stu_info[k.decode()] = v.decode()
# print(new_stu_info)

   4. Delete the specified key:

  stu_info = r.hgetall('stu_info')

  r.hdel('stu_info','gyx') #Delete the specified key

  r.delete('stu_info') #delete the entire key

 5. Set the expiration time of the key:

  r.expire('aaa',100) #The first key sets the expiration time

Small practice in class:
#Analysis : 
#1. Connect to the database, find all the data in the database, use pymysql.curosrs.DictCour for the cursor type
#2. Find all the data [ {"id":1,"passwd":"49487dd4f94008a6110275e48ad09448"," username":"niuhayang","is_admin":1}]
#3. Loop this list, get the usernamer, use the username as the key
#4, then convert this small dictionary into json, and save it in it is ok.
import pymysql,json,redis
r = redis.Redis(host='118.24.3.xx',password='xxyybbc&*',db=1,port=6379)
conn = pymysql.connect(host='118.24.3.xx',user='jxzs',passwd='123456',db='jxzs',charset='utf8')
cur = conn.cursor(cursor=pymysql.cursors.DictCursor)
cur.execute('select * from my_user;')
all_data = cur.fetchall()
for data in all_data:
    k = data.get('username')
    r.hset('stu_info_nhy',k,json.dumps(data))
cur.close()
conn.close()
View Code

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325854513&siteId=291194637