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

Changeset 380

Show
Ignore:
Timestamp:
06/25/05 10:07:48
Author:
mikerobi
Message:

session filter fixes: session cache no cleans up, session files are are now prefixed

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/_cpconfig.py

    r377 r380  
    3535 
    3636    'sessionFilter.on' : False, 
    37     'sessionFilter.sessionsList' : ['default'], 
     37    'sessionFilter.sessionList' : ['default'], 
    3838    'sessionFilter.default.on': True, 
    3939    'sessionFilter.default.timeout': 60, 
  • trunk/cherrypy/lib/filter/sessionfilter/basesession.py

    r375 r380  
    4141    The functions which need to be redefined are at the end of the file 
    4242    """ 
     43 
     44    # by default don't use session caching 
     45    noCache = True 
    4346 
    4447    # these are the  functions that need to rewritten  
     
    114117 
    115118    def commitCache(self, sessionKey):  
     119        """ commit a session to persistand storage """ 
    116120         
    117121        session = self.__sessionCache[sessionKey] 
     
    121125        cacheTimeout = sessionconfig.retrieve('cacheTimeout',  self.sessionName, None) 
    122126         
    123         if session.threadCount == 0 and not cacheTimeout
     127        if session.threadCount == 0 and (self.noCache or not cacheTimeout)
    124128            del self.__sessionCache[sessionKey] 
    125         """ commit a session to persistand storage """ 
    126129     
    127130    def cleanUpCache(self): 
     
    131134         
    132135        # don't waste cycles if we aren't caching inactive sessions 
    133         if cacheTimeout
     136        if cacheTimeout and not self.noCache
    134137            for session in self.__sessionCache.itervalues(): 
    135138                # make sure the session doesn't have any active threads 
    136                 expired = time.time() < (session.lastAccess + cacheTimeout
     139                expired = time.time() < (session.lastAccess + cacheTimeout * 60
    137140                if session.threadCount == 0 and expired: 
    138141                    del self.__sessionCache[session.key] 
    139      
  • trunk/cherrypy/lib/filter/sessionfilter/dbmsession.py

    r375 r380  
    3838 
    3939class DBMSession(BaseSession): 
     40    
     41    # it is ok to cache the sessoin data 
     42    noCache = False 
     43     
    4044    def __init__(self, sessionName): 
    4145        cpg = cherrypy.cpg 
  • trunk/cherrypy/lib/filter/sessionfilter/filesession.py

    r375 r380  
    4040 
    4141class FileSession(BaseSession): 
    42    
     42     
     43    # is ok to cache filesession data 
     44    noCache = False 
     45     
    4346    def __init__(self, sessionName): 
    4447        BaseSession.__init__(self, sessionName) 
    4548        self.__fileLock = threading.RLock() 
    4649 
    47     def __storageDir(self): 
    48         cpg = cherrypy.cpg 
    49          
    50         storageDir = sessionconfig.retrieve('storageFileDir', self.sessionName)  
    51         return storageDir 
    52      
    5350    def newSession(self): 
    5451        """ Return a new sessiondict instance """ 
     
    5855    # all session writes are blocked  
    5956    def getSession(self, sessionKey): 
    60         sessionStorageFileDir = self.__storageDir() 
    6157        if not sessionKey: 
    6258            raise SessionNotFoundError 
    63         fname = os.path.join(sessionStorageFileDir, sessionKey) 
    64         if os.path.exists(fname): 
    65             f = open(fname, "rb") 
     59         
     60        storageDir = sessionconfig.retrieve('storageFileDir', self.sessionName)  
     61        fileName = '%s_%i-%s' % (self.sessionName, hash(self), sessionKey) 
     62        filePath = os.path.join(storageDir, fileName) 
     63         
     64        if os.path.exists(filePath): 
     65            f = open(filePath, "rb") 
    6666            self.__fileLock.acquire() 
    6767            sessionData = pickle.load(f) 
     
    7373     
    7474    def setSession(self, sessionData): 
    75         sessionStorageFileDir = self.__storageDir() 
    7675     
    77         fname=os.path.join(sessionStorageFileDir, sessionData.key) 
    78         f = open(fname,"wb") 
     76        storageDir = sessionconfig.retrieve('storageFileDir', self.sessionName)  
     77        fileName = '%s_%i-%s' % (self.sessionName, hash(self), sessionData.key) 
     78        filePath = os.path.join(storageDir, fileName) 
     79 
     80        f = open(filePath,"wb") 
    7981        self.__fileLock.acquire() 
    8082        pickle.dump(sessionData, f) 
     
    8385 
    8486    def delSession(self, sessionKey): 
    85         sessionStorageFileDir = self.__storageDir() 
     87        storageDir = sessionconfig.retrieve('storageFileDir', self.sessionName)  
     88        fileName = '%s_%i-%s' % (self.sessionName, hash(self), sessionKey) 
     89        filePath = os.path.join(storageDir, fileName) 
     90         
     91        if os.path.exists(filePath): 
     92            self.__fileLock.acquire() 
     93            os.remove(filePath) 
     94            self.__fileLock.release() 
    8695     
    8796    def cleanUpOldSessions(self): 
    8897        sessionStorageFileDir = self.__storageDir() 
    8998        sessionFileList = os.listdir(sessionStorageFileDir) 
    90  
     99         
     100        filePrefix = '%s_%i' % (self.sessionName, hash(self)) 
     101         
    91102        for sessionKey in sessionFileList: 
    92             session = self.getSession(sessionKey) 
    93             if session.expired(): 
    94                 try: 
    95                     os.remove(os.path.join(sessionStorageFileDir, sessionKey)) 
    96                 except: 
    97                     """ the session was probably removed already """ 
     103            prefix, key = sessionFileList.split('-') 
     104            if filePrefix == prefix: 
     105                session = self.getSession(sessionKey) 
     106                if session.expired(): 
     107                    try: 
     108                        os.remove(os.path.join(sessionStorageFileDir, sessionKey)) 
     109                    except: 
     110                        """ the session was probably removed already """ 
  • trunk/cherrypy/lib/filter/sessionfilter/ramsession.py

    r298 r380  
    3434 
    3535class RamSession(BaseSession): 
     36 
    3637    def __init__(self, sessionName): 
    3738        BaseSession.__init__(self, sessionName) 
  • trunk/cherrypy/lib/filter/sessionfilter/sessionconfig.py

    r377 r380  
    3737                } 
    3838 
    39 _sessionDefaults = { 
    40     'sessionFilter.on' : False, 
    41     'sessionFilter.SessionsList' : ['default'], 
    42     'sessionFilter.default.on': True, 
    43     'sessionFilter.default.timeout': 60, 
    44     'sessionFilter.default.cleanUpDelay': 60, 
    45     'sessionFilter.default.storageType' : 'ram', 
    46     'sessionFilter.default.cookiePrefix': 'CherryPySession', 
    47     'sessionFilter.default.storageFileDir': '.sessionFiles' 
    48     } 
    49  
    5039try: 
    5140    # the user might not have sqlobject instaled 
  • trunk/cherrypy/lib/filter/sessionfilter/sessionfilter.py

    r377 r380  
    1212    sessions = {} 
    1313     
    14     sessionLists = cpg.config.getAll('sessionFilter.sessionsList') 
     14    sessionLists = cpg.config.getAll('sessionFilter.sessionList') 
    1515 
    1616    for sessionPath, sessionList in sessionLists.iteritems(): 
     
    3434                sessionList[index] = sessionManager 
    3535                 
    36                 cpg.config.update({sessionPath: {'sessionFilter.sessionsList' : sessionList} }) 
     36                cpg.config.update({sessionPath: {'sessionFilter.sessionList' : sessionList} }) 
    3737            else: 
    38                 if not cpg.config.get('sessionFilter.%s.on' % session, True): 
    39                     continue 
    40                  
    4138                sessionManager = session 
    42                 sessionManager.lastCleanUp = time.time() 
    43  
    44                 cleanUpDelay = sessionconfig.retrieve('cleanUpDelay', sessionManager.sessionName) 
    45                 now = time.time() 
    46                 lastCleanUp = sessionManager.lastCleanUp 
    47                 if lastCleanUp + cleanUpDelay * 60 <= now: 
    48                     sessionManager.cleanUpOldSessions() 
    4939 
    5040            sessions[sessionManager.sessionName] = sessionManager 
     
    129119            sessionData = getattr(cpg.sessions, sessionName) 
    130120            sessionManager.commitCache(sessionData.key) 
     121            sessionManager.cleanUpCache() 
     122 
     123            sessionManager.lastCleanUp = time.time() 
     124 
     125            cleanUpDelay = sessionconfig.retrieve('cleanUpDelay', sessionManager.sessionName) 
     126            now = time.time() 
     127            lastCleanUp = sessionManager.lastCleanUp 
     128            if lastCleanUp + cleanUpDelay * 60 <= now: 
     129                sessionManager.cleanUpOldSessions() 
     130 
    131131     
    132132    def beforeMain(self): 
  • trunk/cherrypy/lib/filter/sessionfilter/sqlobjectsession.py

    r288 r380  
    3535 
    3636class SQLObjectSessionDict(BaseSessionDict): 
     37     
     38    # it is ok to cache the session data 
     39     
     40    noCache = False 
    3741    def __init__(self, sqlObject): 
    3842        self.__sqlObject = sqlObject 
     
    108112    def delSession(self, sessionKey): 
    109113        # figure out what to catch when this doesn't work 
    110         Session.delete(Session.q.session_key=='abcd'
     114        Session.delete(Session.q.session_key==sessionKey
    111115         
    112116        #raise SessionNotFoundError 
  • trunk/cherrypy/tutorial/tut09_sessionfilter.py

    r377 r380  
    1818                { 
    1919                'global' : {'sessionFilter.on': True}, 
    20                 '/admin' : {'sessionFilter.sessionsList' : ['admin'] }, 
    21                 '/forum' : {'sessionFilter.sessionsList' : ['forum'] } 
     20                '/admin' : {'sessionFilter.sessionList' : ['admin'] }, 
     21                '/forum' : {'sessionFilter.sessionList' : ['forum'] } 
    2222                }) 
    2323 

Hosted by WebFaction

Log in as guest/cpguest to create tickets