Commits

Anonymous committed b170b38

faster sessionKey function, sessionFilter cleanups

Comments (0)

Files changed (7)

cherrypy/lib/filter/sessionfilter/basesession.py

 class BaseSession(object):
     """
     This is the class from which all session storage types are derived.
+    The functions which need to be redefined are at the end of the file
     """
 
-    # configname is the string storageType value used by the 
-    # configuration file
-    configName = 'BaseSession'
+    # these are the  functions that need to rewritten 
+    def delSession(self, sessionKey):
+        """ delete a session from storage """
+
+    def getSession(self, sessionKey):
+        """ function to lookup the session """
     
-    """
-    autoKeys is used to tell the server if the session storage class
-    can automaticly determine all of the key names.  This would be 
-    true if you are using a relational database and false if you are 
-    using python dictionaries.
+    def setSession(self, sessionData):
+        """ function to save sesion data """
 
-    If autoKeys is false the key names must be provided at runtime.
-    If it is true any key names provied at runtime are ignored.
-    """
-    autoKeys   = True
-    
+    def cleanUpOldSessions(self):
+        """This function cleans up expired sessions"""
+
+    def newSession(self):
+        """ Return a new sessiondict instance """
+
+    # it might be usefull to redefine this function
+    def generateSessionKey(self):
+        """ Function to return a new sessioId """
+        sessionKeyFunc = sessionconfig.retrieve('keyGenerator', self.sessionName, None)
+        
+        if sessionKeyFunc:
+            newKey = cherrypy._cputil.getSpecialAttribute(sessionKeyFunc)()
+        else:
+            newKey = sha.new('%s%s' % (time.time(), random.random())).hexdigest()
+        
+        return newKey
 
     def __init__(self, sessionName):
+        """
+        Create the session caceh and set the session name.  Make if you write
+        a custom __init__ function make sure you make a call to 
+        BaseSession.__init__(sessioName)
+        """
+        
         self.__sessionCache = {}
-        self.defaultValues = {}
         self.sessionName = sessionName
-        """
-        This is where the you initialize your session storage class.
-        sessionOptions is a direct mapping of the configuration
-        options.
-        
-        The keys 'host', 'user', 'password', and 'database',
-        must be used by classes that need to connect to remote
-        databases.  'tableName' will contain the table name (duh!).
-
-        The keyw 'dataKeys' will map to a list of the variable names you wish
-        to store in your session object.  If autoKeys is true you will use it
-        to set to create sessionMap instances.
-        """
+  
+   
+    # there should never be a reason to modify the remaining functions, they used 
+    # internally by the sessionFilter
     
     def getDefaultAttributes(self):
       return { 
                'key' : self.generateSessionKey()
              }
        
-    def newSession(self):
-        """ Return a new sessionMap instance """
-        # this needs to check the config file for default values
-        newData = self.getDefaultAttributes()
-        newData.update(self.defaultValues)
-        return SimpleSessionDict(newData)
-
-    def generateSessionKey(self):
-        """ Function to return a new sessioId """
-        sessionKeyFunc = sessionconfig.retrieve('keyGenerator', self.sessionName, None)
-        
-        if sessionKeyFunc:
-            newKey = cherrypy._cputil.getSpecialAttribute(sessionKeyFunc)()
-        else:
-            s = [random.choice(string.letters+string.digits) for i in xrange(50)]
-            s.append('%s'%time.time())
-            newKey = sha.sha(''.join(s)).hexdigest()
-        
-        return newKey
-
     def loadSession(self, sessionKey, autoCreate = True):
         cpg = cherrypy.cpg
         
                 if session.threadCount == 0 and expired:
                     del self.__sessionCache[session.key]
     
-    def dropSession(self, sessionKey):
-        self.delSession()
-        """ delete a session from storage """
-
-    def cleanUpOldSessions(self):
-        """This function cleans up expired sessions"""
-

cherrypy/lib/filter/sessionfilter/basesessiondict.py

         return result
     return _inner
 
-import threading 
-
 # this is a dictionary like class that will be exposed to the application
 # this class is used by 
 class BaseSessionDict(object):

cherrypy/lib/filter/sessionfilter/dbmsession.py

 import shelve
 
 from sessionerrors import *
-
-import cPickle as pickle
+from simplesessiondict import SimpleSessionDict
 
 class DBMSession(BaseSession):
     def __init__(self, sessionName):
         sessionName=cpg.config.get('sessionFilter.new', None)
         sessionFile=cpg.config.get('%s.dbFile' % sessionName, '%s.db' % sessionName)
         self.__data = shelve.open(sessionFile, 'c')
+    
+    def newSession(self):
+        """ Return a new sessiondict instance """
+        newData = self.getDefaultAttributes()
+        return SimpleSessionDict(newData)
 
     def getSession(self, sessionKey):
         try:

cherrypy/lib/filter/sessionfilter/filesession.py

 import sessionconfig
 
 import threading
+from simplesessiondict import SimpleSessionDict
 
 class FileSession(BaseSession):
   
         storageDir = sessionconfig.retrieve('storageFileDir', self.sessionName, '.sessionFiles')
         return storageDir
     
+    def newSession(self):
+        """ Return a new sessiondict instance """
+        newData = self.getDefaultAttributes()
+        return SimpleSessionDict(newData)
+   
     # all session writes are blocked 
     def getSession(self, sessionKey):
         sessionStorageFileDir = self.__storageDir()

cherrypy/lib/filter/sessionfilter/ramsession.py

 import cherrypy.cpg
 
 from sessionerrors import *
+from simplesessiondict import SimpleSessionDict
 
 class RamSession(BaseSession):
     def __init__(self, sessionName):
         BaseSession.__init__(self, sessionName)
         self.__data = {}
-
+    
+    def newSession(self):
+        """ Return a new sessiondict instance """
+        newData = self.getDefaultAttributes()
+        return SimpleSessionDict(newData)
+        
     def getSession(self, sessionKey):
         try:
             return self.__data[sessionKey]
             raise SessionNotFoundError
     
     def setSession(self, sessionData):
-        # since everything in in rame the 
+        # since everything in in ram the 
         # session we don't need to update the data
         # unless int is a new session
         if not self.__data.has_key(sessionData.key):

cherrypy/tutorial/tut10_sessionfilter.conf

 server.threadPool = 10
 server.environment = "production"
 
+# by default we get
+# sessionFilter.on=True
+# sessionFilter.new='sessionMap'
+# sessionMap.storageType='ram'
+
 [/admin]
+# this tell CherryPy to create a session,
+# named 'adminSession' and to store the data in ram
 sessionFilter.new='adminSession'
 adminSession.storageType='ram'
 

cherrypy/tutorial/tut10_sessionfilter.py

 from cherrypy import cpg
 
 class HitCounter:
+  
+    # this is just a primative template function
     def __examplePage(self, poweredBy, count, links, sessionKey):
         yield '<html><head><title>sessionFilter exampe</title><body>\n'
         yield 'This page uses %s based session storage.<br/>\n' % poweredBy
         yield sessionKey
         yield '\n</body></html>'
 
+    # a list of the pages used in the example so we add pages
+    # without changing any code
     samplePages = ['admin', 'index', 'forum']
-
-    def admin(self):
-        adminCount = cpg.sessions.adminSession.get('adminCount', 0) + 1
-        cpg.sessions.adminSession['adminCount'] = adminCount
+    
+    def index(self):
+        # this function uses the sessionMap which is turned on by defualt
+        # it may not be the defualt in future versions
         
-        key = cpg.sessions.adminSession.key
-        return self.__examplePage('ram', adminCount, self.samplePages, key)
-
-    admin.exposed = True
-    
-    def forum(self):
-        forumCount = cpg.sessions.forumSession.get('forumCount', 0) + 1
-        cpg.sessions.forumSession['forumCount'] = forumCount
-        
-        key = cpg.sessions.forumSession.key
-        return self.__examplePage('ram', forumCount, self.samplePages, key)
-    forum.exposed=True
-
-    def index(self):
         # Increase the silly hit counter
         count = cpg.sessions.sessionMap.get('count', 0) + 1
 
 
     index.exposed = True
 
-    # these functions do the same as the index but with a different session dictionary
     def admin(self):
+        # this function uses the adminSession which is defined in
+        # the config file "tut10_sessionFilter.conf", otherwise
+        # it mirrors the session function
+
         adminCount = cpg.sessions.adminSession.get('adminCount', 0) + 1
         cpg.sessions.adminSession['adminCount'] = adminCount
         
         key = cpg.sessions.adminSession.key
         return self.__examplePage('ram', adminCount, self.samplePages, key)
-
     admin.exposed = True
     
     def forum(self):
+        # this function uses the forumSession which is defined in
+        # the config file "tut10_sessionFilter.conf", otherwise
+        # it mirrors the session function
+        
         forumCount = cpg.sessions.forumSession.get('forumCount', 0) + 1
         cpg.sessions.forumSession['forumCount'] = forumCount