예제 #1
0
파일: base.py 프로젝트: mrmaple/open_qon
 def __init__(self, get_function=None):
     self.data = None
     self.get_function = get_function
     
     from qon.observe import Dependencies
     self.__depends = Dependencies()
     self.__disable_cache = False           
예제 #2
0
파일: base.py 프로젝트: mrmaple/open_qon
 def upgradeToVersion1(self):
     from qon.observe import Dependencies
     self.__depends = Dependencies()
     self.version_upgrade_done()
예제 #3
0
파일: base.py 프로젝트: mrmaple/open_qon
class PersistentCache(QonPersistent):
    """Generic Persistent caching mechanism."""
    
    persistenceVersion = 2
    
    def __init__(self, get_function=None):
        self.data = None
        self.get_function = get_function
        
        from qon.observe import Dependencies
        self.__depends = Dependencies()
        self.__disable_cache = False           
    
    def upgradeToVersion1(self):
        from qon.observe import Dependencies
        self.__depends = Dependencies()
        self.version_upgrade_done()

    # alex added __disable_cache ability so that directives like on-user-link can
    #  call disable_cache to signify that the cache is not to be used
    def upgradeToVersion2(self):
        self.__disable_cache = False    
        self.version_upgrade_done()
        
    def set(self, data):
        self.data = data
    
    def get(self):
        from quixote.html import htmltext
        
        # check dependencies and cache setting
        if (not self.__depends.up_to_date()) or self.__disable_cache or self.__depends.any_caches_disabled():
            self.data = None
        
        if self.data:
            return self.data
        
        if self.get_function:

            # assume that we'll be able to use the cache
            self.__disable_cache = False

            self.__depends.remove_all_dependencies()

            self.data = self.get_function()
            
            # can't pickle htmltext objects
            if type(self.data) is htmltext:
                self.data = str(self.data)
        
        return self.data
    
    def flush(self):
        """Flush cache. E.g. when something it depends on has changed."""
        self.data = None
        self.__depends.remove_all_dependencies()

    def add_dependency(self, target):
        self.__depends.add_dependency(target)

    def disable_cache(self):
        self.__disable_cache = True

    def cache_disabled(self):
        return self.__disable_cache