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

Changeset 566

Show
Ignore:
Timestamp:
08/28/05 11:22:18
Author:
mikerobi
Message:

sessionFilter: A lot more code cleanups, the file adaptor know uses mrow locking

Files:

Legend:

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

    r555 r566  
    5656        self.__data = shelve.open(dbFile, 'c') 
    5757     
    58     def newSession(self): 
    59         """ Return a new sessiondict instance """ 
    60         newData = self.getDefaultAttributes() 
    61         return SessionDict(sessionAttributes = newData) 
    62  
    63     def getSessionDict(self, sessionKey): 
     58    def _getSessionDict(self, sessionKey): 
    6459        try: 
    6560            return self.__data[sessionKey] 
     
    7065        self.__data[sessionData.key] = sessionData 
    7166 
    72     def deleteSession(self, sessionKey): 
    73         try: 
    74             del self.__data[sessionKey] 
    75         except KeyError: 
    76             raise SessionNotFoundError() 
    77      
    7867    def _cleanUpOldSessions(self): 
    79         #deleteList = [] 
    80         for sessionKey in self.__data: 
    81             session = self.__data[sessionKey] 
     68        deleteList = [] 
     69         
     70        for session in self.__data.itervalues(): 
    8271            if session.expired(): 
    83                 del self.__data[sessionKey] 
    84                 #deleteList.append(sessionKey) 
    85         #for key in deleteList: 
    86         #    self.deleteSession(sessionKey) 
     72                deleteList.append(session.key) 
     73         
     74        for key in deleteList: 
     75            del self.__data[key] 
    8776 
    88     def _debugDump(self): 
    89         if not cherrypy.config.get('testMode', False): 
    90             raise AttributeError() 
    91         else: 
    92             return dict(self.__data) 
     77    def _sessionCount(self): 
     78        return len(self.__data) 
  • trunk/cherrypy/lib/filter/sessionfilter/baseadaptor.py

    r541 r566  
    2828 
    2929import random, time, sha, string 
    30  
     30from sessiondict import SessionDict 
    3131import cherrypy 
    3232 
     
    4040    noCache = True 
    4141     
    42     # these are the  functions that need to rewritten  
    43     def deleteSession(self, sessionKey): 
    44         """ delete a session from storage """ 
    45         raise NotImplementedError('deleteSession has not been implemented') 
    46      
    47     def getSessionDict(self, sessionKey): 
     42    def _getSessionDict(self, sessionKey): 
    4843        """ function to lookup the session """ 
    49         raise NotImplementedError('getSessionDict has not been implemented') 
     44        raise NotImplementedError('_getSessionDict has not been implemented') 
    5045     
    5146    def saveSessionDict(self, sessionData): 
    5247        """ function to save sesion data """ 
    5348        raise NotImplementedError('saveSessionDict has not been implemented') 
     49 
     50    def _cleanUpOldSessions(self): 
     51        """ function to clean up old sesion data """ 
     52        raise NotImplementedError('cleanUpOldSession has not been implemented') 
    5453     
    55     def _cleanUpOldSessions(self): 
    56         """This function cleans up expired sessions""" 
    57         raise NotImplementedError('cleanUpOldSessions has not been implemented') 
    58      
     54    # it might be usefull to redefine these function 
     55    def generateSessionKey(self): 
     56        """ Function to return a new sessioId """ 
     57         
     58        return sha.new('%s' % random.random()).hexdigest() 
     59         
    5960    def newSession(self): 
    6061        """ Return a new sessiondict instance """ 
    61         raise NotImplementedError('newSession not been implemented') 
    62     
    63     # it might be usefull to redefine this function 
    64     def generateSessionKey(self): 
    65         """ Function to return a new sessioId """ 
    66         try: 
    67             sessionKeyFunc = self.getSetting('keyGenerator') 
    68         except AttributeError: 
    69             sessionKeyFunc = None 
     62        attributes = {  
     63               'timestamp'  : int(time.time()), 
     64               'timeout'    : self.getSetting('timeout'), 
     65               'lastAccess' : int(time.time()), 
     66               'key'        : self.generateSessionKey() 
     67               } 
     68        return SessionDict(sessionAttributes = attributes) 
     69     
     70    # there should never be a reason to modify the remaining functions, they are used  
     71    # internally by the sessionFilter 
    7072         
    71         if sessionKeyFunc: 
    72             newKey = sessionKeyFunc() 
    73         else: 
    74             newKey = sha.new('%s' % random.random()).hexdigest() 
    75          
    76         return newKey 
    77      
    7873    def __init__(self, sessionName, sessionPath): 
    7974        """ 
     
    10499            from cherrypy._cpthreadinglocal import local 
    105100 
    106     # there should never be a reason to modify the remaining functions, they used  
    107     # internally by the sessionFilter 
    108      
    109     def getDefaultAttributes(self): 
    110       return {  
    111                'timestamp'  : int(time.time()), 
    112                'timeout'    : self.getSetting('timeout'), 
    113                'lastAccess' : int(time.time()), 
    114                'key'        : self.generateSessionKey() 
    115              } 
    116               
    117101    def getSetting(self, settingName, default = None): 
    118102            missing = object() 
     
    122106 
    123107            return result 
    124  
    125              
     108     
    126109    def cleanUpOldSessions(self): 
    127110        now = time.time() 
    128111        if self.nextCleanUp < now: 
    129112            self._cleanUpOldSessions() 
    130          
    131     def loadSession(self, sessionKey, autoCreate = True): 
     113     
     114    def getSessionDict(self, sessionKey): 
    132115        try: 
    133116            # look for the session in the cache 
     
    136119        except KeyError: 
    137120            # look in the primary storage 
    138             session = self.getSessionDict(sessionKey) 
     121            session = self._getSessionDict(sessionKey) 
    139122            session.threadCount += 1 
    140123            self.__sessionCache[sessionKey] = session 
    141      
     124 
     125        return session 
     126 
     127    def loadSession(self, sessionKey): 
     128        session = self.getSessionDict(sessionKey) 
     129 
    142130        session.cookieName = self.cookieName 
    143131        session.lastAccess = time.time() 
     
    186174                self.commitCache(session.key) 
    187175                del self.__sessionCache[session.key] 
     176 
     177    def _cacheSize(self): 
     178        return len(self.__sessionCache) 
  • trunk/cherrypy/lib/filter/sessionfilter/fileadaptor.py

    r555 r566  
    3030import threading 
    3131import os.path 
    32  
     32import mrow 
    3333 
    3434from baseadaptor import BaseAdaptor 
    3535from sessionerrors import * 
    3636from sessiondict import SessionDict 
    37  
    3837 
    3938class FileAdaptor(BaseAdaptor): 
     
    4443    def __init__(self, sessionName, sessionPath): 
    4544        BaseAdaptor.__init__(self, sessionName, sessionPath) 
    46         self.__fileLock = threading.RLock() 
     45        self.__fileLock = mrow.MROWLock()  
    4746 
    48     def newSession(self): 
    49         """ Return a new sessiondict instance """ 
    50         newData = self.getDefaultAttributes() 
    51         return SessionDict(sessionAttributes = newData) 
    52     
    5347    # all session writes are blocked  
    54     def getSessionDict(self, sessionKey): 
     48    def _getSessionDict(self, sessionKey): 
    5549        if not sessionKey: 
    5650            raise SessionNotFoundError() 
     
    6357        if os.path.exists(filePath): 
    6458            f = open(filePath, "rb") 
    65             self.__fileLock.acquire() 
     59            self.__fileLock.lock_read() 
    6660            try: 
    6761                sessionData = pickle.load(f) 
    6862            finally: 
    69                 self.__fileLock.release() 
     63                self.__fileLock.unlock_read() 
    7064                f.close() 
    7165            return sessionData 
     
    8074        filePath = os.path.join(storagePath, fileName) 
    8175 
    82         f = open(filePath,"wb") 
    83         self.__fileLock.acquire() 
    84         pickle.dump(sessionData, f) 
    85         self.__fileLock.release() 
    86         f.close() 
     76        self.__fileLock.lock_write() 
     77        try: 
     78            f = open(filePath,"wb") 
     79            pickle.dump(sessionData, f) 
     80            f.close() 
     81        finally: 
     82            self.__fileLock.unlock_write() 
    8783 
    88     def deleteSession(self, sessionKey): 
    89         storagePath = self.getSetting('storagePath') 
    90         fileName = '%s-%s' % (self.name, sessionKey) 
    91         filePath = os.path.join(storagePath, fileName) 
     84    def _cleanUpOldSessions(self): 
     85        self.__fileLock.lock_read() 
     86        try: 
     87            storagePath = self.getSetting('storagePath') 
     88            sessionFileList = os.listdir(storagePath) 
     89             
     90            for fileName in sessionFileList: 
     91                try: 
     92                    prefix, sessionKey = fileName.split('-') 
     93                    if prefix == self.name: 
     94                        session = self._getSessionDict(sessionKey) 
     95                        if session.expired(): 
     96                            os.remove(os.path.join(storagePath, fileName)) 
     97                except ValueError: 
     98                    pass 
     99        finally: 
     100            self.__fileLock.unlock_read() 
     101     
     102    def _sessionCount(self): 
     103        self.__fileLock.lock_read() 
     104        try: 
     105            storagePath = self.getSetting('storagePath') 
     106            sessionFileList = os.listdir(storagePath) 
    92107         
    93         if os.path.exists(filePath): 
    94             self.__fileLock.acquire() 
    95             os.remove(filePath) 
    96             self.__fileLock.release() 
     108            count = 0 
     109            for fileName in sessionFileList: 
     110                if fileName.startswith(self.name + '-'): 
     111                    count += 1 
     112         
     113            return count 
     114        finally: 
     115            self.__fileLock.lock_read() 
     116         
    97117     
    98     def _cleanUpOldSessions(self): 
    99         storagePath = self.getSetting('storagePath') 
    100         sessionFileList = os.listdir(storagePath) 
    101          
    102         for fileName in sessionFileList: 
    103             try: 
    104                 prefix, sessionKey = fileName.split('-') 
    105                 if prefix == self.name: 
    106                     session = self.getSessionDict(sessionKey) 
    107                     if session.expired(): 
    108                         os.remove(os.path.join(storagePath, fileName)) 
    109             except ValueError: 
    110                 pass 
    111  
    112     def _debugDump(self): 
    113         storagePath = self.getSetting('storagePath') 
    114         sessionFileList = os.listdir(storagePath) 
    115          
    116         filePrefix = '%s-' % self.name 
    117         dump = {} 
    118         for fileName in sessionFileList: 
    119             try: 
    120                 prefix, sessionKey = fileName.split('-') 
    121                 if prefix == self.name: 
    122                     dump[sessionKey] = self.getSessionDict(sessionKey) 
    123             except ValueError: 
    124                 pass 
    125  
    126         return dump 
    127  
  • trunk/cherrypy/lib/filter/sessionfilter/ramadaptor.py

    r555 r566  
    3939        self.__data = {} 
    4040     
    41     def newSession(self): 
    42         """ Return a new sessiondict instance """ 
    43         newData = self.getDefaultAttributes() 
    44         return SessionDict(sessionAttributes = newData) 
    45          
    46     def getSessionDict(self, sessionKey): 
     41    def _getSessionDict(self, sessionKey): 
    4742        try: 
    4843            return self.__data[sessionKey] 
     
    5752            self.__data[sessionData.key] = sessionData 
    5853 
    59     def deleteSession(self, sessionKey): 
    60         try: 
    61             del self.__data[sessionKey] 
    62         except KeyError: 
    63             raise SessionNotFoundError() 
    64      
    6554    def _cleanUpOldSessions(self): 
    66         #deleteList = [] 
    67         for sessionKey in self.__data.keys(): 
    68             session = self.__data[sessionKey] 
     55        deleteList = [] 
     56         
     57        for session in self.__data.itervalues(): 
    6958            if session.expired(): 
    70                 del self.__data[sessionKey] 
    71                 #deleteList.append(sessionKey) 
    72         
    73         return  
     59                deleteList.append(session.key) 
     60         
    7461        for key in deleteList: 
    75             self.deleteSession(sessionKey) 
    76  
    77     def _debugDump(self): 
    78         if not cherrypy.config.get('testMode', False): 
    79             raise AttributeError() 
    80         else: 
    81             return self.__data 
     62            del self.__data[key] 
     63             
     64    def _sessionCount(self): 
     65        return len(self.__data) 

Hosted by WebFaction

Log in as guest/cpguest to create tickets