This snippet uses a Riak (http://www.basho.com) bucket as a Django session store. I give no guarantees that it works 100% or even 50%.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | # Do with this what you will but don't blame me if anything bad happens.
import time
from django.conf import settings
from django.contrib.sessions.backends.base import SessionBase, CreateError
import riak
class SessionStore(SessionBase):
def __init__(self, session_key=None):
self._client = riak.RiakClient()
self._bucket = self._client.bucket('sessions')
super(SessionStore, self).__init__(session_key)
def load(self):
# Get the session from Riak.
session = self._bucket.get(self.session_key)
if session.exists():
session_data = session.get_data()
return self.decode(session_data['data'])
else:
self.create()
return {}
def create(self):
while True:
self.session_key = self._get_new_session_key()
try:
self.save(must_create=True)
except CreateError:
# The key wasn't unique, try again
continue
self.modified = True
return
def save(self,must_create=True):
session_key = self.session_key
session_data = self.encode(self._get_session(no_load=must_create))
expire_date = time.mktime(self.get_expiry_date().timetuple())
session = self._bucket.new(session_key, data={
'data': session_data,
'expire': expire_date,
})
# Save the object to Riak.
session.store()
def exists(self, session_key):
session = self._bucket.get(session_key)
return session.exists()
def delete(self, session_key=None):
sk = session_key
# Get the object and delete it if found.
if session_key is None:
sk = self.session_key
session = self._bucket.get(sk)
if session.exists():
session.delete()
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 8 months ago
Comments
Various people in the world receive the credit loans from different creditors, just because that's easy and fast.
#
Please login first before commenting.