"Sharedance is a high-performance server that centralize ephemeral key/data pairs on remote hosts, without the overhead and the complexity of an SQL database." Frank DENIS
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | from django.conf import settings
from sharedance.api import *
from django.contrib.sessions.backends.base import SessionBase
from django.core.exceptions import SuspiciousOperation, ImproperlyConfigured
import cjson
class SessionStore(SessionBase):
def __init__(self, session_key=None):
self.sharedance = shareDance('localhost',1042)
super(SessionStore, self).__init__(session_key)
def load(self):
session_data = {}
try:
session_data = self.decode(self.sharedance.get(self.session_key))
# session_data = cjson.decode(self.sharedance.get(self.session_key, encoding='utf-8'))
except:
self._session_key = self._get_new_session_key()
self._session_cache = {}
self.save()
# Ensure the user is notified via a new cookie.
self.modified = True
return session_data
def save(self):
try:
self.sharedance.set(self.session_key,self.encode(self._session))
# self.sharedance.set(self.session_key,cjson.encode(self._session, encoding='utf-8')))
except:
pass
def exists(self, session_key):
try:
return self.sharedance.get(session_key) != ''
except:
return False
def delete(self, session_key):
try:
self.sharedance.delete(self.session_key)
except:
pass
def clean(self):
pass
------------------------------------------------------------------------------------------------------
import socket
class shareDance(object):
def __init__(self, host, port):
self.host = host
self.port = port
def connect(self):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.connect((self.host, self.port))
def close(self):
self.socket.close()
self.socket = None
def set(self, key, data):
key_len = len(key)
data_len = len(data)
self.connect()
self.socket.send('S%c%c%c%c%c%c%c%c%s%s' % (
(key_len >> 24) & 0xff,
(key_len >> 16) & 0xff,
(key_len >> 8) & 0xff,
key_len & 0xff,
(data_len >> 24) & 0xff,
(data_len >> 16) & 0xff,
(data_len >> 8) & 0xff,
data_len & 0xff,
key,
data))
if self.socket.recv(3) == "OK\n":
self.close()
return True
else:
self.close()
return False
def get(self, key):
key_len = len(key)
self.connect()
self.socket.send('F%c%c%c%c%s' % (
(key_len >> 24) & 0xff,
(key_len >> 16) & 0xff,
(key_len >> 8) & 0xff,
key_len & 0xff,
key))
response = ''
while True:
data = self.socket.recv(4096)
if not data: break
response = response + data
self.close()
return response
def delete(self, key):
key_len = len(key)
self.connect()
self.socket.send('F%c%c%c%c%s' % (
(key_len >> 24) & 0xff,
(key_len >> 16) & 0xff,
(key_len >> 8) & 0xff,
key_len & 0xff,
key))
if self.socket.recv(3) == "OK\n":
self.close()
return True
else:
self.close()
return False
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 3 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
Now all we need is a cache backend as well
#
Please login first before commenting.