Exemple #1
0
class NameParserCache(object):
    def __init__(self):
        super(NameParserCache, self).__init__()
        self._previous_parsed = OrderedDefaultdict()
        self._cache_size = 1000
        self.lock = threading.Lock()

    def add(self, name, parse_result):
        with self.lock:
            self._previous_parsed[name] = parse_result
            _current_cache_size = len(self._previous_parsed)
            if _current_cache_size > self._cache_size:
                key = None
                for i in range(_current_cache_size - self._cache_size):
                    try:
                        key = self._previous_parsed.first_key()
                        del self._previous_parsed[key]
                    except KeyError:
                        logger.log('Could not remove old NameParserCache entry: %s' % key, logger.DEBUG)

    def get(self, name):
        with self.lock:
            if name in self._previous_parsed:
                logger.log('Using cached parse result for: ' + name, logger.DEBUG)
                self._previous_parsed.move_to_end(name)
                return self._previous_parsed[name]
Exemple #2
0
class NameParserCache(object):
    def __init__(self):
        super(NameParserCache, self).__init__()
        self._previous_parsed = OrderedDefaultdict()
        self._cache_size = 1000
        self.lock = threading.Lock()

    def add(self, name, parse_result):
        with self.lock:
            self._previous_parsed[name] = parse_result
            _current_cache_size = len(self._previous_parsed)
            if _current_cache_size > self._cache_size:
                key = None
                for i in range(_current_cache_size - self._cache_size):
                    try:
                        key = self._previous_parsed.first_key()
                        del self._previous_parsed[key]
                    except KeyError:
                        logger.log(
                            'Could not remove old NameParserCache entry: %s' %
                            key, logger.DEBUG)

    def get(self, name):
        with self.lock:
            if name in self._previous_parsed:
                logger.log('Using cached parse result for: ' + name,
                           logger.DEBUG)
                self._previous_parsed.move_to_end(name)
                return self._previous_parsed[name]
    def test_ordereddefaultdict(self):

        d = OrderedDefaultdict()
        d['key1'] = 'test_item1'
        d['key2'] = 'test_item2'
        d['key3'] = 'test_item3'
        self.assertEqual('key1', d.first_key())
        del d['key1']
        d['key4'] = 'test_item4'
        d.move_to_end('key2')
        self.assertEqual('test_item2', d['key2'])
        self.assertEqual('key2', d.last_key())
        _ = 'end'