Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 298

Show
Ignore:
Timestamp:
06/12/05 10:30:36
Author:
mikerobi
Message:

faster sessionKey function, sessionFilter cleanups

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/lib/filter/sessionfilter/basesession.py

    r292 r298  
    3939    """ 
    4040    This is the class from which all session storage types are derived. 
     41    The functions which need to be redefined are at the end of the file 
    4142    """ 
    4243 
    43     # configname is the string storageType value used by the  
    44     # configuration file 
    45     configName = 'BaseSession' 
     44    # these are the  functions that need to rewritten  
     45    def delSession(self, sessionKey): 
     46        """ delete a session from storage """ 
     47 
     48    def getSession(self, sessionKey): 
     49        """ function to lookup the session """ 
    4650     
    47     """ 
    48     autoKeys is used to tell the server if the session storage class 
    49     can automaticly determine all of the key names.  This would be  
    50     true if you are using a relational database and false if you are  
    51     using python dictionaries. 
     51    def setSession(self, sessionData): 
     52        """ function to save sesion data """ 
    5253 
    53     If autoKeys is false the key names must be provided at runtime. 
    54     If it is true any key names provied at runtime are ignored. 
    55     """ 
    56     autoKeys   = True 
    57      
     54    def cleanUpOldSessions(self): 
     55        """This function cleans up expired sessions""" 
     56 
     57    def newSession(self): 
     58        """ Return a new sessiondict instance """ 
     59 
     60    # it might be usefull to redefine this function 
     61    def generateSessionKey(self): 
     62        """ Function to return a new sessioId """ 
     63        sessionKeyFunc = sessionconfig.retrieve('keyGenerator', self.sessionName, None) 
     64         
     65        if sessionKeyFunc: 
     66            newKey = cherrypy._cputil.getSpecialAttribute(sessionKeyFunc)() 
     67        else: 
     68            newKey = sha.new('%s%s' % (time.time(), random.random())).hexdigest() 
     69         
     70        return newKey 
    5871 
    5972    def __init__(self, sessionName): 
     73        """ 
     74        Create the session caceh and set the session name.  Make if you write 
     75        a custom __init__ function make sure you make a call to  
     76        BaseSession.__init__(sessioName) 
     77        """ 
     78         
    6079        self.__sessionCache = {} 
    61         self.defaultValues = {} 
    6280        self.sessionName = sessionName 
    63         """ 
    64         This is where the you initialize your session storage class. 
    65         sessionOptions is a direct mapping of the configuration 
    66         options. 
    67          
    68         The keys 'host', 'user', 'password', and 'database', 
    69         must be used by classes that need to connect to remote 
    70         databases.  'tableName' will contain the table name (duh!). 
    71  
    72         The keyw 'dataKeys' will map to a list of the variable names you wish 
    73         to store in your session object.  If autoKeys is true you will use it 
    74         to set to create sessionMap instances. 
    75         """ 
     81   
     82    
     83    # there should never be a reason to modify the remaining functions, they used  
     84    # internally by the sessionFilter 
    7685     
    7786    def getDefaultAttributes(self): 
     
    8392             } 
    8493        
    85     def newSession(self): 
    86         """ Return a new sessionMap instance """ 
    87         # this needs to check the config file for default values 
    88         newData = self.getDefaultAttributes() 
    89         newData.update(self.defaultValues) 
    90         return SimpleSessionDict(newData) 
    91  
    92     def generateSessionKey(self): 
    93         """ Function to return a new sessioId """ 
    94         sessionKeyFunc = sessionconfig.retrieve('keyGenerator', self.sessionName, None) 
    95          
    96         if sessionKeyFunc: 
    97             newKey = cherrypy._cputil.getSpecialAttribute(sessionKeyFunc)() 
    98         else: 
    99             s = [random.choice(string.letters+string.digits) for i in xrange(50)] 
    100             s.append('%s'%time.time()) 
    101             newKey = sha.sha(''.join(s)).hexdigest() 
    102          
    103         return newKey 
    104  
    10594    def loadSession(self, sessionKey, autoCreate = True): 
    10695        cpg = cherrypy.cpg 
     
    154143                    del self.__sessionCache[session.key] 
    155144     
    156     def dropSession(self, sessionKey): 
    157         self.delSession() 
    158         """ delete a session from storage """ 
    159  
    160     def cleanUpOldSessions(self): 
    161         """This function cleans up expired sessions""" 
    162  
  • trunk/cherrypy/lib/filter/sessionfilter/basesessiondict.py

    r283 r298  
    3939        return result 
    4040    return _inner 
    41  
    42 import threading  
    4341 
    4442# this is a dictionary like class that will be exposed to the application 
  • trunk/cherrypy/lib/filter/sessionfilter/dbmsession.py

    r280 r298  
    3333 
    3434from sessionerrors import * 
    35  
    36 import cPickle as pickle 
     35from simplesessiondict import SimpleSessionDict 
    3736 
    3837class DBMSession(BaseSession): 
     
    4544        sessionFile=cpg.config.get('%s.dbFile' % sessionName, '%s.db' % sessionName) 
    4645        self.__data = shelve.open(sessionFile, 'c') 
     46     
     47    def newSession(self): 
     48        """ Return a new sessiondict instance """ 
     49        newData = self.getDefaultAttributes() 
     50        return SimpleSessionDict(newData) 
    4751 
    4852    def getSession(self, sessionKey): 
  • trunk/cherrypy/lib/filter/sessionfilter/filesession.py

    r282 r298  
    3737 
    3838import threading 
     39from simplesessiondict import SimpleSessionDict 
    3940 
    4041class FileSession(BaseSession): 
     
    4950        return storageDir 
    5051     
     52    def newSession(self): 
     53        """ Return a new sessiondict instance """ 
     54        newData = self.getDefaultAttributes() 
     55        return SimpleSessionDict(newData) 
     56    
    5157    # all session writes are blocked  
    5258    def getSession(self, sessionKey): 
  • trunk/cherrypy/lib/filter/sessionfilter/ramsession.py

    r275 r298  
    3131 
    3232from sessionerrors import * 
     33from simplesessiondict import SimpleSessionDict 
    3334 
    3435class RamSession(BaseSession): 
     
    3637        BaseSession.__init__(self, sessionName) 
    3738        self.__data = {} 
    38  
     39     
     40    def newSession(self): 
     41        """ Return a new sessiondict instance """ 
     42        newData = self.getDefaultAttributes() 
     43        return SimpleSessionDict(newData) 
     44         
    3945    def getSession(self, sessionKey): 
    4046        try: 
     
    4450     
    4551    def setSession(self, sessionData): 
    46         # since everything in in rame the  
     52        # since everything in in ram the  
    4753        # session we don't need to update the data 
    4854        # unless int is a new session 
  • trunk/cherrypy/tutorial/tut10_sessionfilter.conf

    r275 r298  
    44server.environment = "production" 
    55 
     6# by default we get 
     7# sessionFilter.on=True 
     8# sessionFilter.new='sessionMap' 
     9# sessionMap.storageType='ram' 
     10 
    611[/admin] 
     12# this tell CherryPy to create a session, 
     13# named 'adminSession' and to store the data in ram 
    714sessionFilter.new='adminSession' 
    815adminSession.storageType='ram' 
  • trunk/cherrypy/tutorial/tut10_sessionfilter.py

    r279 r298  
    66 
    77class HitCounter: 
     8   
     9    # this is just a primative template function 
    810    def __examplePage(self, poweredBy, count, links, sessionKey): 
    911        yield '<html><head><title>sessionFilter exampe</title><body>\n' 
     
    1719        yield '\n</body></html>' 
    1820 
     21    # a list of the pages used in the example so we add pages 
     22    # without changing any code 
    1923    samplePages = ['admin', 'index', 'forum'] 
    20  
    21     def admin(self): 
    22         adminCount = cpg.sessions.adminSession.get('adminCount', 0) + 1 
    23         cpg.sessions.adminSession['adminCount'] = adminCount 
     24     
     25    def index(self): 
     26        # this function uses the sessionMap which is turned on by defualt 
     27        # it may not be the defualt in future versions 
    2428         
    25         key = cpg.sessions.adminSession.key 
    26         return self.__examplePage('ram', adminCount, self.samplePages, key) 
    27  
    28     admin.exposed = True 
    29      
    30     def forum(self): 
    31         forumCount = cpg.sessions.forumSession.get('forumCount', 0) + 1 
    32         cpg.sessions.forumSession['forumCount'] = forumCount 
    33          
    34         key = cpg.sessions.forumSession.key 
    35         return self.__examplePage('ram', forumCount, self.samplePages, key) 
    36     forum.exposed=True 
    37  
    38     def index(self): 
    3929        # Increase the silly hit counter 
    4030        count = cpg.sessions.sessionMap.get('count', 0) + 1 
     
    5040    index.exposed = True 
    5141 
    52     # these functions do the same as the index but with a different session dictionary 
    5342    def admin(self): 
     43        # this function uses the adminSession which is defined in 
     44        # the config file "tut10_sessionFilter.conf", otherwise 
     45        # it mirrors the session function 
     46 
    5447        adminCount = cpg.sessions.adminSession.get('adminCount', 0) + 1 
    5548        cpg.sessions.adminSession['adminCount'] = adminCount 
     
    5750        key = cpg.sessions.adminSession.key 
    5851        return self.__examplePage('ram', adminCount, self.samplePages, key) 
    59  
    6052    admin.exposed = True 
    6153     
    6254    def forum(self): 
     55        # this function uses the forumSession which is defined in 
     56        # the config file "tut10_sessionFilter.conf", otherwise 
     57        # it mirrors the session function 
     58         
    6359        forumCount = cpg.sessions.forumSession.get('forumCount', 0) + 1 
    6460        cpg.sessions.forumSession['forumCount'] = forumCount 

Hosted by WebFaction

Log in as guest/cpguest to create tickets