Edgewall Software

Ticket #6436: cache-get_ticket_fields-trac-0.10.2.diff

File cache-get_ticket_fields-trac-0.10.2.diff, 1.8 kB (added by cboos, 6 months ago)

A safer version, with a copy of the fields

  • trac/ticket/api.py

     
    1515# Author: Jonas Borgström <jonas@edgewall.com> 
    1616 
    1717import re 
     18try: 
     19    import threading 
     20except ImportError: 
     21    import dummy_threading as threading 
    1822 
    1923from trac.config import * 
    2024from trac.core import * 
     
    6872        [TracTickets#Assign-toasDrop-DownList Assign-to as Drop-Down List] 
    6973        (''since 0.9'').""") 
    7074 
     75    def __init__(self): 
     76         self._fields_lock = threading.RLock() 
     77   
    7178    # Public API 
    7279 
    7380    def get_available_actions(self, ticket, perm_): 
     
    8592 
    8693    def get_ticket_fields(self): 
    8794        """Returns the list of fields available for tickets.""" 
     95        # This is now cached - as it makes quite a number of things faster, 
     96        # e.g. #6436 
     97        if self._fields is None: 
     98            self._fields_lock.acquire() 
     99            try: 
     100                self._fields = self._get_ticket_fields() 
     101            finally: 
     102                self._fields_lock.release() 
     103        return [f.copy() for f in self._fields] 
     104 
     105    _fields = None 
     106    def _get_ticket_fields(self): 
    88107        from trac.ticket import model 
    89108 
    90109        db = self.env.get_db_cnx() 
     
    155174        return fields 
    156175 
    157176    def get_custom_fields(self): 
     177        if self._custom_fields is None: 
     178            self._fields_lock.acquire() 
     179            try: 
     180                self._custom_fields = self._get_custom_fields() 
     181            finally: 
     182                self._fields_lock.release() 
     183        return [f.copy() for f in self._custom_fields] 
     184 
     185    _custom_fields = None 
     186    def _get_custom_fields(self): 
    158187        fields = [] 
    159188        config = self.config['ticket-custom'] 
    160189        for name in [option for option, value in config.options()