def add_chain(self, chain):
        """
        Add block in a chain in the correct order. Also add all of the blocks
        to the cache before doing a purge.
        """
        with self._lock:
            chain.sort(key=lambda x: x.block_num)
            for block in chain:
                block_id = block.header_signature
                if block_id not in self._cache:
                    self._cache[block_id] = self.CachedValue(block)
                    if block.previous_block_id in self._cache:
                        self._cache[block.previous_block_id].inc_count()

            if time.time() > self._next_purge_time:
                self._purge_expired()
                self._next_purge_time = time.time() + self._purge_frequency
Add block in a chain in the correct order. Also add all of the blocks
        to the cache before doing a purge.