1. Alen Mujezinovic
  2. django-redis-sessions

Commits

David Paccoud  committed 6b6231c

Created the redis_sessions application

The application declares a backend to store sessions in a Redis database

  • Participants
  • Branches default

Comments (0)

Files changed (6)

File .hgignore

View file
+.sw(p|o)$
+.py(c|o)$
+.orig$

File redis_sessions/__init__.py

Empty file added.

File redis_sessions/backends/__init__.py

Empty file added.

File redis_sessions/backends/redis.py

View file
+from django.contrib.sessions.backends.base import SessionBase, CreateError
+from redis_models import database as db
+
+
+class SessionStore(SessionBase):
+    """
+    Implements Redis session store.
+    """
+
+    def exists(self, session_key):
+        if db.exists("session:%s" % session_key):
+            return True
+        return False
+
+    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
+            return
+
+    def save(self, must_create=False):
+        if must_create:
+            func = db.setnx
+        else:
+            func = db.set
+        key = "session:%s" % self.session_key
+        result = func(
+                key,
+                self.encode(self._get_session(no_load=must_create)))
+        if must_create and result is None:
+            raise CreateError
+        # The key has been set, set its expiration
+        db.expire(key, self.get_expiry_age())
+        self.modified = False
+
+    def delete(self, session_key=None):
+        if session_key is None:
+            session_key = self.session_key
+        db.delete("session:%s" % session_key)
+
+    def load(self):
+        session_data = db.get("session:%s" % self.session_key)
+        if session_data is None:
+            self.create()
+            return {}
+        return self.decode(session_data)

File redis_sessions/models.py

View file
+from django.db import models
+
+# Create your models here.

File redis_sessions/tests.py

View file
+from django.test import TestCase
+from redis_sessions.backends.redis import SessionStore
+
+
+class RedisSessionTest(TestCase):
+
+    def test_session_store(self):
+        """
+        Tests session store creation.
+        """
+        st = SessionStore()
+        st.load()
+        session_key = st.session_key
+
+        self.assertEqual(st.exists(session_key), True)
+        self.assertEqual(st.exists("whatever"), False)
+
+        st["data1"] = "Hello world!"
+        st["data2"] = "my data"
+        self.assertEqual(st.modified, True)
+        st.save()
+        self.assertEqual(st.modified, False)
+
+        st2 = SessionStore(session_key)
+        self.assertEqual(st.session_key, session_key)
+        self.assertEqual(st2["data1"], "Hello world!")
+        self.assertEqual(st2["data2"], "my data")
+
+        st2.delete()
+        st2.load()
+        self.assertNotEqual(st2.session_key, session_key)