Purge expired cached tokens and oldest tokens if more than cache_size

def purge_cache(self):
        """
        Purge expired cached tokens and oldest tokens if more than cache_size
        """
        if len(self._cache) > self.max_cache_size:
            items = sorted(self._cache.items(), key=lambda (k, v): v['expiry'])
            self._cache = {k: v for k, v in items[self.max_cache_size:]
                           if not self._expired(v)}