程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Pickle in Python to store data in redis

編輯:Python

pickle  and json contrast

pickle.loads() This is for converting to binary

json.loads()

In fact, the usage is similar , But with pickle After putting data into the cache , For the data in the subsequent read cache , Easy to operate

Use pickle, First

First step , First import , because pickle yes python Self contained . So there's no need to install

direct .import pickle

This is before me Write the login interface , You can use it directly  

pickle.dumps(user.first())

To convert data types , And convert it into binary form

Through

res.set(token, user_dict, expire_time)

set Method to add data to the cache

Then, when requesting an interface , Walking Middleware , You need to assign an object in the middleware

 

In the end, you can do it in view View into the inside through request.uer object , To get redis The data in the cache , Again , You can also modify the data in the cache

  In this way, you can get the data in the cache

Then use request.user Modify the data in the cache

def update_nick(self,request):

        nick=request.GET.get('nick)

        request.user.nick=nick

        request.user.save()

This completes the modification , After the modification , stay set Just put it in the cache


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved