Changeset 566
- Timestamp:
- 08/28/05 11:22:18
- Files:
-
- trunk/cherrypy/lib/filter/sessionfilter/anydbadaptor.py (modified) (2 diffs)
- trunk/cherrypy/lib/filter/sessionfilter/baseadaptor.py (modified) (6 diffs)
- trunk/cherrypy/lib/filter/sessionfilter/fileadaptor.py (modified) (4 diffs)
- trunk/cherrypy/lib/filter/sessionfilter/mrow.py (added)
- trunk/cherrypy/lib/filter/sessionfilter/ramadaptor.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cherrypy/lib/filter/sessionfilter/anydbadaptor.py
r555 r566 56 56 self.__data = shelve.open(dbFile, 'c') 57 57 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): 64 59 try: 65 60 return self.__data[sessionKey] … … 70 65 self.__data[sessionData.key] = sessionData 71 66 72 def deleteSession(self, sessionKey):73 try:74 del self.__data[sessionKey]75 except KeyError:76 raise SessionNotFoundError()77 78 67 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(): 82 71 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] 87 76 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 28 28 29 29 import random, time, sha, string 30 30 from sessiondict import SessionDict 31 31 import cherrypy 32 32 … … 40 40 noCache = True 41 41 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): 48 43 """ function to lookup the session """ 49 raise NotImplementedError(' getSessionDict has not been implemented')44 raise NotImplementedError('_getSessionDict has not been implemented') 50 45 51 46 def saveSessionDict(self, sessionData): 52 47 """ function to save sesion data """ 53 48 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') 54 53 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 59 60 def newSession(self): 60 61 """ 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 70 72 71 if sessionKeyFunc:72 newKey = sessionKeyFunc()73 else:74 newKey = sha.new('%s' % random.random()).hexdigest()75 76 return newKey77 78 73 def __init__(self, sessionName, sessionPath): 79 74 """ … … 104 99 from cherrypy._cpthreadinglocal import local 105 100 106 # there should never be a reason to modify the remaining functions, they used107 # internally by the sessionFilter108 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 117 101 def getSetting(self, settingName, default = None): 118 102 missing = object() … … 122 106 123 107 return result 124 125 108 126 109 def cleanUpOldSessions(self): 127 110 now = time.time() 128 111 if self.nextCleanUp < now: 129 112 self._cleanUpOldSessions() 130 131 def loadSession(self, sessionKey, autoCreate = True):113 114 def getSessionDict(self, sessionKey): 132 115 try: 133 116 # look for the session in the cache … … 136 119 except KeyError: 137 120 # look in the primary storage 138 session = self. getSessionDict(sessionKey)121 session = self._getSessionDict(sessionKey) 139 122 session.threadCount += 1 140 123 self.__sessionCache[sessionKey] = session 141 124 125 return session 126 127 def loadSession(self, sessionKey): 128 session = self.getSessionDict(sessionKey) 129 142 130 session.cookieName = self.cookieName 143 131 session.lastAccess = time.time() … … 186 174 self.commitCache(session.key) 187 175 del self.__sessionCache[session.key] 176 177 def _cacheSize(self): 178 return len(self.__sessionCache) trunk/cherrypy/lib/filter/sessionfilter/fileadaptor.py
r555 r566 30 30 import threading 31 31 import os.path 32 32 import mrow 33 33 34 34 from baseadaptor import BaseAdaptor 35 35 from sessionerrors import * 36 36 from sessiondict import SessionDict 37 38 37 39 38 class FileAdaptor(BaseAdaptor): … … 44 43 def __init__(self, sessionName, sessionPath): 45 44 BaseAdaptor.__init__(self, sessionName, sessionPath) 46 self.__fileLock = threading.RLock()45 self.__fileLock = mrow.MROWLock() 47 46 48 def newSession(self):49 """ Return a new sessiondict instance """50 newData = self.getDefaultAttributes()51 return SessionDict(sessionAttributes = newData)52 53 47 # all session writes are blocked 54 def getSessionDict(self, sessionKey):48 def _getSessionDict(self, sessionKey): 55 49 if not sessionKey: 56 50 raise SessionNotFoundError() … … 63 57 if os.path.exists(filePath): 64 58 f = open(filePath, "rb") 65 self.__fileLock. acquire()59 self.__fileLock.lock_read() 66 60 try: 67 61 sessionData = pickle.load(f) 68 62 finally: 69 self.__fileLock. release()63 self.__fileLock.unlock_read() 70 64 f.close() 71 65 return sessionData … … 80 74 filePath = os.path.join(storagePath, fileName) 81 75 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() 87 83 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) 92 107 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 97 117 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 pass111 112 def _debugDump(self):113 storagePath = self.getSetting('storagePath')114 sessionFileList = os.listdir(storagePath)115 116 filePrefix = '%s-' % self.name117 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 pass125 126 return dump127 trunk/cherrypy/lib/filter/sessionfilter/ramadaptor.py
r555 r566 39 39 self.__data = {} 40 40 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): 47 42 try: 48 43 return self.__data[sessionKey] … … 57 52 self.__data[sessionData.key] = sessionData 58 53 59 def deleteSession(self, sessionKey):60 try:61 del self.__data[sessionKey]62 except KeyError:63 raise SessionNotFoundError()64 65 54 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(): 69 58 if session.expired(): 70 del self.__data[sessionKey] 71 #deleteList.append(sessionKey) 72 73 return 59 deleteList.append(session.key) 60 74 61 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)

