Edgewall Software

Ticket #8510: t8510-Configuration-cache-r8720.diff

File t8510-Configuration-cache-r8720.diff, 2.5 KB (added by cboos, 3 years ago)

Add a cache in Configuration objects.

  • trac/config.py

     
    2828 
    2929_TRUE_VALUES = ('yes', 'true', 'enabled', 'on', 'aye', '1', 1, True) 
    3030 
     31_use_default = object() 
     32 
    3133def _to_utf8(basestr): 
    3234    return to_unicode(basestr).encode('utf-8') 
    3335 
     
    5355        self._lastmtime = 0 
    5456        self._sections = {} 
    5557        self.parse_if_needed() 
     58        self._cache = {} 
    5659 
    5760    def __contains__(self, name): 
    5861        """Return whether the configuration contains a section of the given 
     
    237240        changed = False 
    238241        modtime = os.path.getmtime(self.filename) 
    239242        if modtime > self._lastmtime: 
     243            self._cache = {} 
    240244            self.parser._sections = {} 
    241245            self.parser.read(self.filename) 
    242246            self._lastmtime = modtime 
     
    311315         
    312316        Valid default input is a string. Returns a string. 
    313317        """ 
     318        ckey = (self.name, key) 
     319        cached = self.config._cache.get(ckey, _use_default) 
     320        if cached is not _use_default: 
     321            return cached 
    314322        name_str = _to_utf8(self.name) 
    315323        key_str = _to_utf8(key) 
    316324        if self.config.parser.has_option(name_str, key_str): 
     
    318326        elif self.config.parent: 
    319327            value = self.config.parent[self.name].get(key, default) 
    320328        else: 
    321             option = Option.registry.get((self.name, key)) 
     329            option = Option.registry.get(ckey) 
    322330            if option: 
    323                 value = option.default or default 
     331                value = option.default or _use_default 
    324332            else: 
    325                 value = default 
     333                value = _use_default 
     334        if value is _use_default: 
     335            return default 
    326336        if not value: 
    327             return u'' 
     337            value = u'' 
    328338        elif isinstance(value, basestring): 
    329             return to_unicode(value) 
    330         else: 
    331             return value 
     339            value = to_unicode(value) 
     340        self.config._cache[ckey] = value 
     341        return value 
    332342 
    333343    def getbool(self, key, default=''): 
    334344        """Return the value of the specified option as boolean. 
     
    418428         
    419429        These changes are not persistent unless saved with `save()`. 
    420430        """ 
     431        self.config._cache.pop((self.name, key), None) 
    421432        name_str = _to_utf8(self.name) 
    422433        key_str = _to_utf8(key) 
    423434        if not self.config.parser.has_section(name_str):