Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 41 lines (32 sloc) 1.255 kB
d43c2b5 @whilefalse Initial commit
authored
1 from web.session import Store
2 import time
3
4 class MongoStore(Store):
5 def __init__(self, db, collection_name):
6 self.collection = db[collection_name]
7
8 def __contains__(self, key):
9 data = self.collection.find_one({'session_id':key})
10 return bool(data)
11
12 def __getitem__(self, key):
13 now = time.time()
14 s = self.collection.find_one({'session_id':key})
15 if not s:
16 raise KeyError
17 else:
18 s.update({'attime':now})
19 return s
20
21 def __setitem__(self, key, value):
22 now = time.time()
23
24 value['attime'] = now
25
26 s = self.collection.find_one({'session_id':key})
27 if s:
28 value = dict(map(lambda x: (str(x[0]), x[1]), [(k,v) for (k,v) in value.iteritems() if k not in ['_id']]))
29 s.update(**value)
30 self.collection.save(s)
31 else:
32 self.collection.insert(value)
33
34 def __delitem__(self, key):
35 self.collection.remove({'session_id':key})
36
37 def cleanup(self, timeout):
38 timeout = timeout/(24.0*60*60) #timedelta takes numdays as arg
39 last_allowed_time = time.time() - timeout
40 self.collection.remove({'attime' : { '$lt' : last_allowed_time}})
Something went wrong with that request. Please try again.