def _base_set(self, mode, key, value, timeout=None):
timeout = timeout or self.default_timeout
# Only UTC here for Mongo auto-purge
now = datetime.utcnow()
expires = now + timedelta(seconds=timeout)
#
pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
encoded = base64.encodebytes(pickled).strip()
if mode == 'set':
# Set sets new data. And there is no matter, that key exists
self._coll.update({'key':key} ,{'key':key, 'data': encoded, 'expires': expires}, upsert = True)
elif mode == 'add':
if self._coll.find_one({'key': key}):
return False
self._coll.insert({'key': key, 'data': encoded, 'expires': expires})
return True
评论列表
文章目录