Python cachetools.LRUCache() Examples

The following are 30 code examples of cachetools.LRUCache(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module cachetools , or try the search function .
Example #1
Source File: kgx_utils.py    From kgx with BSD 3-Clause "New" or "Revised" License 7 votes vote down vote up
def get_cache(maxsize=10000):
    """
    Get an instance of cachetools.cache

    Parameters
    ----------
    maxsize: int
        The max size for the cache (``10000``, by default)

    Returns
    -------
    cachetools.cache
        An instance of cachetools.cache

    """
    global cache
    if cache is None:
        cache = LRUCache(maxsize)
    return cache 
Example #2
Source File: store.py    From pantalaimon with Apache License 2.0 6 votes vote down vote up
def load_media(self, server, mxc_server=None, mxc_path=None):
        server, _ = Servers.get_or_create(name=server)

        if not mxc_path:
            media_cache = LRUCache(maxsize=MAX_LOADED_MEDIA)

            for i, m in enumerate(server.media):
                if i > MAX_LOADED_MEDIA:
                    break

                media = MediaInfo(m.mxc_server, m.mxc_path, m.key, m.iv, m.hashes)
                media_cache[(m.mxc_server, m.mxc_path)] = media

            return media_cache
        else:
            m = PanMediaInfo.get_or_none(
                PanMediaInfo.server == server,
                PanMediaInfo.mxc_server == mxc_server,
                PanMediaInfo.mxc_path == mxc_path,
            )

            if not m:
                return None

            return MediaInfo(m.mxc_server, m.mxc_path, m.key, m.iv, m.hashes) 
Example #3
Source File: eager_tf_executor.py    From federated with Apache License 2.0 6 votes vote down vote up
def __init__(self, device=None):
    """Creates a new instance of an eager executor.

    Args:
      device: An optional `tf.config.LogicalDevice` that this executor will
        schedule all of its operations to run on. For example, the list of
        logical devices can be obtained using
        `tf.config.list_logical_devices()`.

    Raises:
      RuntimeError: If not executing eagerly.
      TypeError: If the device is not a `tf.config.LogicalDevice`.
      ValueError: If there is no device `device`.
    """
    if not tf.executing_eagerly():
      raise RuntimeError('The eager executor may only be used in eager mode.')
    if device is not None:
      py_typecheck.check_type(device, tf.config.LogicalDevice)
      self._device = device
    else:
      self._device = None
    self._tf_function_cache = cachetools.LRUCache(_TF_FUNCTION_CACHE_SIZE) 
Example #4
Source File: memory.py    From bplustree with MIT License 6 votes vote down vote up
def __init__(self, filename: str, tree_conf: TreeConf,
                 cache_size: int=512):
        self._filename = filename
        self._tree_conf = tree_conf
        self._lock = rwlock.RWLock()

        if cache_size == 0:
            self._cache = FakeCache()
        else:
            self._cache = cachetools.LRUCache(maxsize=cache_size)

        self._fd, self._dir_fd = open_file_in_dir(filename)

        self._wal = WAL(filename, tree_conf.page_size)
        if self._wal.needs_recovery:
            self.perform_checkpoint(reopen_wal=True)

        # Get the next available page
        self._fd.seek(0, io.SEEK_END)
        last_byte = self._fd.tell()
        self.last_page = int(last_byte / self._tree_conf.page_size)
        self._freelist_start_page = 0

        # Todo: Remove this, it should only be in Tree
        self._root_node_page = 0 
Example #5
Source File: _core.py    From mapchete with MIT License 6 votes vote down vote up
def __init__(self, config, with_cache=False):
        """
        Initialize Mapchete processing endpoint.

        Parameters
        ----------
        config : MapcheteConfig
            Mapchete process configuration
        with_cache : bool
            cache processed output data in memory (default: False)
        """
        logger.info("initialize process")
        if not isinstance(config, MapcheteConfig):
            raise TypeError("config must be MapcheteConfig object")
        self.config = config
        self.process_name = self.config.process_name
        self.with_cache = True if self.config.mode == "memory" else with_cache
        if self.with_cache:
            self.process_tile_cache = LRUCache(maxsize=512)
            self.current_processes = {}
            self.process_lock = threading.Lock()
        self._count_tiles_cache = {} 
Example #6
Source File: impl_dir.py    From taskflow with Apache License 2.0 6 votes vote down vote up
def __init__(self, conf):
        super(DirBackend, self).__init__(conf)
        max_cache_size = self._conf.get('max_cache_size')
        if max_cache_size is not None:
            max_cache_size = int(max_cache_size)
            if max_cache_size < 1:
                raise ValueError("Maximum cache size must be greater than"
                                 " or equal to one")
            self.file_cache = cachetools.LRUCache(max_cache_size)
        else:
            self.file_cache = {}
        self.encoding = self._conf.get('encoding', self.DEFAULT_FILE_ENCODING)
        if not self._path:
            raise ValueError("Empty path is disallowed")
        self._path = os.path.abspath(self._path)
        self.lock = fasteners.ReaderWriterLock() 
Example #7
Source File: caches.py    From endpoints-management-python with Apache License 2.0 6 votes vote down vote up
def __init__(self, maxsize, out_deque=None, **kw):
        """Constructor.

        Args:
          maxsize (int): the maximum number of entries in the queue
          out_deque :class:`collections.deque`: a `deque` in which to add items
            that expire from the cache
          **kw: the other keyword args supported by constructor to
            :class:`cachetools.LRUCache`

        Raises:
          ValueError: if out_deque is not a collections.deque

        """
        super(DequeOutLRUCache, self).__init__(maxsize, **kw)
        if out_deque is None:
            out_deque = collections.deque()
        elif not isinstance(out_deque, collections.deque):
            raise ValueError(u'out_deque should be collections.deque')
        self._out_deque = out_deque
        self._tracking = {} 
Example #8
Source File: caching_executor.py    From federated with Apache License 2.0 6 votes vote down vote up
def __init__(self, target_executor, cache=None):
    """Creates a new instance of this executor.

    Args:
      target_executor: An instance of `executor_base.Executor`.
      cache: The cache to use (must be an instance of `cachetools.Cache`). If
        unspecified, by default we construct a 1000-element LRU cache.
    """
    py_typecheck.check_type(target_executor, executor_base.Executor)
    if cache is not None:
      py_typecheck.check_type(cache, cachetools.Cache)
    else:
      cache = cachetools.LRUCache(_DEFAULT_CACHE_SIZE)
    self._target_executor = target_executor
    self._cache = cache
    self._num_values_created = 0 
Example #9
Source File: test_lru.py    From cachetools with MIT License 6 votes vote down vote up
def test_lru_getsizeof(self):
        cache = LRUCache(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):
            cache[4] = 4
        self.assertEqual(len(cache), 1)
        self.assertEqual(cache[3], 3) 
Example #10
Source File: githubhandler.py    From bioconda-utils with MIT License 6 votes vote down vote up
def __init__(self, session: aiohttp.ClientSession,
                 app_name: str, app_key: str, app_id: str,
                 client_id: str, client_secret: str) -> None:
        #: Name of app
        self.name = app_name
        #: ID of app
        self.app_id = app_id
        #: Authorization key
        self.app_key = app_key
        #: OAUTH client ID
        self.client_id = client_id
        #: OAUTH client secret
        self.client_secret = client_secret
        #: Cache for API queries
        self._cache = cachetools.LRUCache(maxsize=500)
        #: Our client session
        self._session = session
        #: JWT and its expiry
        self._jwt: Tuple[int, str] = (0, "")
        #: OAUTH tokens for installations
        self._tokens: Dict[str, Tuple[int, str]] = {}
        #: GitHubHandlers for each installation
        self._handlers: Dict[Tuple[str, str], GitHubHandler] = {}
        #: GitHubHandlers for each user token->time,handler
        self._user_handlers: Dict[str, Tuple[int, GitHubHandler]] = {} 
Example #11
Source File: test_lru.py    From cachetools with MIT License 6 votes vote down vote up
def test_lru(self):
        cache = LRUCache(maxsize=2)

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

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

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

        cache[5] = 5
        self.assertEqual(len(cache), 2)
        self.assertEqual(cache[4], 4)
        self.assertEqual(cache[5], 5)
        self.assertNotIn(2, cache) 
Example #12
Source File: tree.py    From uproot with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _init(self):
        self.trees = cachetools.LRUCache(5)                                 # last 5 TTrees
        if self.basketcache is None:
            self.basketcache = uproot.cache.ThreadSafeArrayCache(1024**2)   # 1 MB
        if self.keycache is None:
            self.keycache = cachetools.LRUCache(10000)                      # last 10000 TKeys 
Example #13
Source File: cache.py    From uproot with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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)) 
Example #14
Source File: jwt.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def __init__(self, signer, issuer, subject,
                 additional_claims=None,
                 token_lifetime=_DEFAULT_TOKEN_LIFETIME_SECS,
                 max_cache_size=_DEFAULT_MAX_CACHE_SIZE):
        """
        Args:
            signer (google.auth.crypt.Signer): The signer used to sign JWTs.
            issuer (str): The `iss` claim.
            subject (str): The `sub` claim.
            additional_claims (Mapping[str, str]): Any additional claims for
                the JWT payload.
            token_lifetime (int): The amount of time in seconds for
                which the token is valid. Defaults to 1 hour.
            max_cache_size (int): The maximum number of JWT tokens to keep in
                cache. Tokens are cached using :class:`cachetools.LRUCache`.
        """
        super(OnDemandCredentials, self).__init__()
        self._signer = signer
        self._issuer = issuer
        self._subject = subject
        self._token_lifetime = token_lifetime

        if additional_claims is None:
            additional_claims = {}

        self._additional_claims = additional_claims
        self._cache = cachetools.LRUCache(maxsize=max_cache_size) 
Example #15
Source File: cache.py    From uproot with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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)) 
Example #16
Source File: tree.py    From uproot with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _init(self):
        self.trees = cachetools.LRUCache(5)                                 # last 5 TTrees
        if self.basketcache is None:
            self.basketcache = uproot.cache.ThreadSafeArrayCache(1024**2)   # 1 MB
        if self.keycache is None:
            self.keycache = cachetools.LRUCache(10000)                      # last 10000 TKeys 
Example #17
Source File: iter.py    From zerodb with GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self, f, cache_size=1000, length=None):
        """
        Makes a sliceable, cached list-like interface to an iterator
        :param callable f: Function which inits the iterator
        """
        self.f = f
        self.cache = LRUCache(cache_size)
        self.stop = 0
        self.length = length
        self.iterator = iter(f()) 
Example #18
Source File: caching.py    From apex-sigma-core with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, cfg):
        """
        :type cfg: sigma.core.mechanics.config.CacheConfig
        :param cfg: The CacheConfig object for getting the configuration parameters.
        """
        super().__init__(cfg)
        self.cache = cachetools.LRUCache(self.cfg.size) 
Example #19
Source File: jwt.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def __init__(self, signer, issuer, subject,
                 additional_claims=None,
                 token_lifetime=_DEFAULT_TOKEN_LIFETIME_SECS,
                 max_cache_size=_DEFAULT_MAX_CACHE_SIZE):
        """
        Args:
            signer (google.auth.crypt.Signer): The signer used to sign JWTs.
            issuer (str): The `iss` claim.
            subject (str): The `sub` claim.
            additional_claims (Mapping[str, str]): Any additional claims for
                the JWT payload.
            token_lifetime (int): The amount of time in seconds for
                which the token is valid. Defaults to 1 hour.
            max_cache_size (int): The maximum number of JWT tokens to keep in
                cache. Tokens are cached using :class:`cachetools.LRUCache`.
        """
        super(OnDemandCredentials, self).__init__()
        self._signer = signer
        self._issuer = issuer
        self._subject = subject
        self._token_lifetime = token_lifetime

        if additional_claims is None:
            additional_claims = {}

        self._additional_claims = additional_claims
        self._cache = cachetools.LRUCache(maxsize=max_cache_size) 
Example #20
Source File: LookupTables.py    From data_pipeline with Apache License 2.0 5 votes vote down vote up
def __init__(self, es, index, cachesize):
        self._es = es
        self._es_index = index
        self.cache = cachetools.LRUCache(cachesize, getsizeof=sys.getsizeof)
        self.cache.hits = 0
        self.cache.queries = 0 
Example #21
Source File: LookupTables.py    From data_pipeline with Apache License 2.0 5 votes vote down vote up
def __init__(self, es, es_index, cache_gene_size, cache_u2e_size, cache_contains_size):
        self._es = es
        self._es_index = es_index

        self.cache_gene = cachetools.LRUCache(cache_gene_size, getsizeof=sys.getsizeof)
        self.cache_gene.hits = 0
        self.cache_gene.queries = 0
        self.cache_u2e = cachetools.LRUCache(cache_u2e_size, getsizeof=sys.getsizeof)
        self.cache_u2e.hits = 0
        self.cache_u2e.queries = 0
        self.cache_contains = cachetools.LRUCache(cache_contains_size, getsizeof=sys.getsizeof)
        self.cache_contains.hits = 0
        self.cache_contains.queries = 0 
Example #22
Source File: LookupTables.py    From data_pipeline with Apache License 2.0 5 votes vote down vote up
def __init__(self, es, es_index, cache_size):
        self._es = es
        self._es_index = es_index
        #TODO configure size
        self.cache = cachetools.LRUCache(cache_size, getsizeof=sys.getsizeof)
        self.cache.hits = 0
        self.cache.queries = 0 
Example #23
Source File: LookupTables.py    From data_pipeline with Apache License 2.0 5 votes vote down vote up
def __init__(self, es, index, cache_efo_size, cache_contains_size):
        self._es = es
        self._es_index = index
        #TODO configure size
        self.cache_efo = cachetools.LRUCache(cache_efo_size, getsizeof=sys.getsizeof)
        self.cache_efo.hits = 0
        self.cache_efo.queries = 0
        self.cache_contains = cachetools.LRUCache(cache_contains_size, getsizeof=sys.getsizeof)
        self.cache_contains.hits = 0
        self.cache_contains.queries = 0 
Example #24
Source File: test_method.py    From cachetools with MIT License 5 votes vote down vote up
def test_typed_dict(self):
        cached = Cached(LRUCache(maxsize=2))

        self.assertEqual(cached.get_typed(0), 0)
        self.assertEqual(cached.get_typed(1), 1)
        self.assertEqual(cached.get_typed(1), 1)
        self.assertEqual(cached.get_typed(1.0), 2)
        self.assertEqual(cached.get_typed(1.0), 2)
        self.assertEqual(cached.get_typed(0.0), 3)
        self.assertEqual(cached.get_typed(0), 4) 
Example #25
Source File: test_method.py    From cachetools with MIT License 5 votes vote down vote up
def test_lru(self):
        cached = Cached(LRUCache(maxsize=2))

        self.assertEqual(cached.get(0), 0)
        self.assertEqual(cached.get(1), 1)
        self.assertEqual(cached.get(1), 1)
        self.assertEqual(cached.get(1.0), 1)
        self.assertEqual(cached.get(1.0), 1)

        cached.cache.clear()
        self.assertEqual(cached.get(1), 2) 
Example #26
Source File: test_method.py    From cachetools with MIT License 5 votes vote down vote up
def test_typed_lru(self):
        cached = Cached(LRUCache(maxsize=2))

        self.assertEqual(cached.get_typed(0), 0)
        self.assertEqual(cached.get_typed(1), 1)
        self.assertEqual(cached.get_typed(1), 1)
        self.assertEqual(cached.get_typed(1.0), 2)
        self.assertEqual(cached.get_typed(1.0), 2)
        self.assertEqual(cached.get_typed(0.0), 3)
        self.assertEqual(cached.get_typed(0), 4) 
Example #27
Source File: test_method.py    From cachetools with MIT License 5 votes vote down vote up
def test_locked_nospace(self):
        cached = Locked(LRUCache(maxsize=0))

        self.assertEqual(cached.get(0), 1)
        self.assertEqual(cached.get(1), 3)
        self.assertEqual(cached.get(1), 5)
        self.assertEqual(cached.get(1.0), 7)
        self.assertEqual(cached.get(1.0), 9) 
Example #28
Source File: dns.py    From cocrawler with Apache License 2.0 5 votes vote down vote up
def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._crawllocalhost = config.read('Fetcher', 'CrawlLocalhost') or False
        self._crawlprivate = config.read('Fetcher', 'CrawlPrivate') or False
        self._cachemaxsize = config.read('Fetcher', 'DNSCacheMaxSize')
        self._cache = cachetools.LRUCache(int(self._cachemaxsize))
        self._refresh_in_progress = set()

        memory.register_debug(self.memory) 
Example #29
Source File: events.py    From wscelery with MIT License 5 votes vote down vote up
def __init__(self, capp, io_loop):
        """Monitors events that are received from celery.

        capp - The celery app
        io_loop - The event loop to use for dispatch
        """
        super().__init__()

        self.capp = capp
        self.timer = PeriodicCallback(self.on_enable_events,
                                      self.events_enable_interval)

        self.monitor = EventMonitor(self.capp, io_loop)
        self.listeners = {}
        self.finished_tasks = LRUCache(self.max_finished_history) 
Example #30
Source File: jwt.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def __init__(self, signer, issuer, subject,
                 additional_claims=None,
                 token_lifetime=_DEFAULT_TOKEN_LIFETIME_SECS,
                 max_cache_size=_DEFAULT_MAX_CACHE_SIZE):
        """
        Args:
            signer (google.auth.crypt.Signer): The signer used to sign JWTs.
            issuer (str): The `iss` claim.
            subject (str): The `sub` claim.
            additional_claims (Mapping[str, str]): Any additional claims for
                the JWT payload.
            token_lifetime (int): The amount of time in seconds for
                which the token is valid. Defaults to 1 hour.
            max_cache_size (int): The maximum number of JWT tokens to keep in
                cache. Tokens are cached using :class:`cachetools.LRUCache`.
        """
        super(OnDemandCredentials, self).__init__()
        self._signer = signer
        self._issuer = issuer
        self._subject = subject
        self._token_lifetime = token_lifetime

        if additional_claims is None:
            additional_claims = {}

        self._additional_claims = additional_claims
        self._cache = cachetools.LRUCache(maxsize=max_cache_size)