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