In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article will explain in detail how to use MongoDB to achieve session storage in web.py. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Web.py is a python web development framework, since it involves web development, then it is inevitable to use session, while the traditional session storage is basically the default on disk, of course, considering the expansion of web front-end computers, we often need to rewrite sessionhandler to customize session storage.
Fromweb.sessionimportStore
Importtime
ClassMongoStore (Store):
Def__init__ (self,db,collection_name):
Self.collection= db[collection _ name]
Def__contains__ (self,key):
Data=self.collection.find_one ({'session_id':key})
Returnbool (data)
Def__getitem__ (self,key):
Now=time.time ()
S=self.collection.find_one ({'session_id':key})
Ifnots:
RaiseKeyError
Else:
S.update ({'attime':now})
Returns
Def__setitem__ (self,key,value):
Now=time.time ()
Value ['attime'] = now
S=self.collection.find_one ({'session_id':key})
How to use MongoDB to implement session Storage of web.py
Ifs:
Value=dict (map (lambdax: (str (x [0]), x [1]), [(kMagnev) for (kmagin v) invalue.iteritems () ifknotin ['_ id']])
S.update (* * value)
Self.collection.save (s)
Else:
Self.collection.insert (value)
Def__delitem__ (self,key):
Self.collection.remove ({'session_id':key})
Defcleanup (self,timeout):
Timeout=timeout/ (24.0 / 60 / 60) # timedeltatakesnumdaysasarg
Last_allowed_time=time.time ()-timeout
Self.collection.remove ({'attime': {' $lt':last_allowed_time}})
Then replace the declaration of using disk for storage in app, using the
Session=web.session.Session (app,MongoStore (db,'sessions'))
Replace:
Session=web.session.Session (app,web.session.DiskStore ('sessions')).
This is the end of this article on "how to use MongoDB to achieve session storage in web.py". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.