def __init__(self, filePath, maxAvatarSizeValue):
     self.storageSystem = SQLiteAvatarStorageSystem(filePath)
     self.maxAvatarSizeValue = maxAvatarSizeValue
     self.avatarFetcher = ConnectionCachingFetcher(TIMEOUT_TIME)
class PersistentAvatarCache(object):
    def __init__(self, filePath, maxAvatarSizeValue):
        self.storageSystem = SQLiteAvatarStorageSystem(filePath)
        self.maxAvatarSizeValue = maxAvatarSizeValue
        self.avatarFetcher = ConnectionCachingFetcher(TIMEOUT_TIME)

    def open(self):
        self.storageSystem.open()

    def close(self):
        self.storageSystem.close()

    def __convertHTTPObjectToAvatar(self, url, httpObj):
        data = filterImageDataToVersionWithinSizeRestriction(httpObj.data, self.maxAvatarSizeValue)
        lastModified = httpObj.lastModified
        return Avatar(url, data, lastModified)

    def __downloadAvatar(self, avatarURL):
        httpObj = self.avatarFetcher.downloadHTTPObject(avatarURL, MAX_DOWNLOAD_SIZE)
        return self.__convertHTTPObjectToAvatar(avatarURL, httpObj)

    def __updateRecordedAvatarURL(self, user, oldURL, newURL):
        self.storageSystem.updateUserAvatarURL(user, newURL)
        self.storageSystem.cleanCachedAvatarIfUnused(oldURL)

    def __getAvatarURL(self, user, avatarURL):
        recordedAvatarURL = self.storageSystem.getUserAvatarURL(user)
        if avatarURL == None:
            return recordedAvatarURL
        
        if recordedAvatarURL == None:
            self.storageSystem.storeUserAvatarURL(user, avatarURL)
        elif recordedAvatarURL != avatarURL:
            self.__updateRecordedAvatarURL(user, recordedAvatarURL, avatarURL)

        return avatarURL

    def __downloadAvatarIfUnmodified(self, avatarURL, lastModified):
        httpObj = self.avatarFetcher.downloadHTTPObjectIfModified(avatarURL, lastModified, MAX_DOWNLOAD_SIZE)
        if httpObj == None:
            return None
        return self.__convertHTTPObjectToAvatar(avatarURL, httpObj)

    def __refreshAvatar(self, cachedAvatar):
        try:
            newAvatar = self.__downloadAvatarIfUnmodified(cachedAvatar.url, cachedAvatar.lastModified)
        except:
            return cachedAvatar
        if newAvatar == None:
            return cachedAvatar
        
        self.storageSystem.updateCachedAvatar(newAvatar)
        return newAvatar

    def getAvatar(self, user, avatarURL):
        try:
            avatarURL = self.__getAvatarURL(user, avatarURL)
            if avatarURL == None:
                return None
            cachedAvatar = self.storageSystem.getAvatar(avatarURL)
            if cachedAvatar == None:
                avatar = self.__downloadAvatar(avatarURL)
                self.storageSystem.storeCachedAvatar(avatar)
            else:
                avatar = self.__refreshAvatar(cachedAvatar)

            self.storageSystem.commitUncommittedChanges()

            return avatar.data
        except Exception, e:
            self.storageSystem.rollbackUncommittedChanges()
            raise e