How to use the cachetools.LFUCache function in cachetools

To help you get started, we’ve selected a few cachetools examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github tkem / cachetools / tests / test_lfu.py View on Github external
def test_lfu_getsizeof(self):
        cache = LFUCache(maxsize=3, getsizeof=lambda x: x)

        cache[1] = 1
        cache[2] = 2

        self.assertEqual(len(cache), 2)
        self.assertEqual(cache[1], 1)
        self.assertEqual(cache[2], 2)

        cache[3] = 3

        self.assertEqual(len(cache), 1)
        self.assertEqual(cache[3], 3)
        self.assertNotIn(1, cache)
        self.assertNotIn(2, cache)

        with self.assertRaises(ValueError):
github tkem / cachetools / tests / test_lfu.py View on Github external
def test_lfu(self):
        cache = LFUCache(maxsize=2)

        cache[1] = 1
        cache[1]
        cache[2] = 2
        cache[3] = 3

        self.assertEqual(len(cache), 2)
        self.assertEqual(cache[1], 1)
        self.assertTrue(2 in cache or 3 in cache)
        self.assertTrue(2 not in cache or 3 not in cache)

        cache[4] = 4
        self.assertEqual(len(cache), 2)
        self.assertEqual(cache[4], 4)
        self.assertEqual(cache[1], 1)
github CAMeL-Lab / camel_tools / camel_tools / calima_star / analyzer.py View on Github external
if backoff in _BACKOFF_TYPES:
            if backoff == 'NONE':
                self._backoff_condition = None
                self._backoff_action = None
            else:
                backoff_toks = backoff.split('_')
                self._backoff_condition = backoff_toks[0]
                self._backoff_action = backoff_toks[1]
        else:
            raise AnalyzerError('Invalid backoff mode {}'.format(
                repr(backoff)))

        if isinstance(cache_size, int):
            if cache_size > 0:
                cache = LFUCache(cache_size)
                self.analyze = cached(cache, lock=RLock())(self.analyze)

        else:
            raise AnalyzerError('Invalid cache size {}'.format(
                                repr(cache_size)))
github thec0sm0s / Flask-Discord / flask_discord / _http.py View on Github external
def __init__(self, app, users_cache=None):
        self.client_id = app.config["DISCORD_CLIENT_ID"]
        self.client_secret = app.config["DISCORD_CLIENT_SECRET"]
        self.redirect_uri = app.config["DISCORD_REDIRECT_URI"]
        self.users_cache = cachetools.LFUCache(
            app.config.get("DISCORD_USERS_CACHE_MAX_LIMIT", configs.DISCORD_USERS_CACHE_DEFAULT_MAX_LIMIT)
        ) if users_cache is None else users_cache
        if not issubclass(self.users_cache.__class__, Mapping):
            raise ValueError("Instance users_cache must be a mapping like object.")
        if "http://" in self.redirect_uri:
            os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "true"
        app.discord = self
github gkunter / coquery / coquery / cache.py View on Github external
def clear(self):
        self._backup = self._cache
        self._cache = cachetools.LFUCache(
            maxsize=self._backup.maxsize,
            getsizeof=sys.getsizeof)
github gkunter / coquery / coquery / cache.py View on Github external
def resize(self, newsize):
        new_cache = cachetools.LFUCache(
            maxsize=newsize,
            getsizeof=sys.getsizeof)
        cached = [self._cache.popitem() for x in range(len(self._cache))]
        for key, val in cached:
            if sys.getsizeof(val) + new_cache.currsize <= new_cache.maxsize:
                new_cache[key] = val
        self._cache = new_cache
github gkunter / coquery / coquery / cache.py View on Github external
path = os.path.join(options.cfg.cache_path, "coq_cache.db")
        if os.path.exists(path):
            try:
                self._cache = pickle.load(open(path, "rb"))
                if options.cfg.verbose:
                    S = "Using query cache (current size: {}, max size: {})."
                    S = S.format(self._cache.currsize, self._cache.maxsize)
                    logger.info(S)
                    print(S)
            except (IOError, ValueError, EOFError):
                S = "Cannot read query cache, creating a new one (size: {})."
                S = S.format(options.cfg.query_cache_size)
                logger.warning(S)

        if self._cache is None:
            self._cache = cachetools.LFUCache(
                maxsize=options.cfg.query_cache_size,
                getsizeof=sys.getsizeof)
github DHI-GRAS / terracotta / terracotta / tile.py View on Github external
def __init__(self, cfg_path):
        cfg = configparser.ConfigParser()
        cfg.read(cfg_path)
        options = config.parse_options(cfg)

        self._datasets = self._make_datasets(cfg)
        self._cache = LFUCache(options['tile_cache_size'])
github Suyash458 / Wordbot / DictionaryAPI.py View on Github external
def __init__(self):
        self.word_api = WordApi.WordApi(swagger.ApiClient(wordnik_api_key, wordnik_api))
        self.wordoftheday_api = WordsApi.WordsApi(swagger.ApiClient(wordnik_api_key, wordnik_api))
        self.urbandictionary_api = urbandictionary_api
        self.dictionaryCache = LFUCache(maxsize = 1000)
        self.urbandictionaryCache = LFUCache(maxsize = 1000)
        self.wordOfTheDayCache = {}
        self.session = requests.Session()
        self.session.mount('http://', requests.adapters.HTTPAdapter(max_retries=5))
        self.session.mount('https://', requests.adapters.HTTPAdapter(max_retries=5))
github scikit-hep / uproot / uproot / cache.py View on Github external
def __init__(self, limitbytes, method="LRU"):
        from uproot.rootio import _memsize
        m = _memsize(limitbytes)
        if m is not None:
            limitbytes = int(math.ceil(m))
        if method == "LRU":
            self._cache = cachetools.LRUCache(limitbytes, getsizeof=self.getsizeof)
        elif method == "LFU":
            self._cache = cachetools.LFUCache(limitbytes, getsizeof=self.getsizeof)
        else:
            raise ValueError("unrecognized method: {0}".format(method))