checkpointing.cache.in_mem_lru
1from checkpointing.cache.base import CacheBase 2from checkpointing._typing import ContextId, ReturnValue 3from checkpointing.exceptions import CheckpointNotExist 4from collections import OrderedDict 5 6 7class InMemoryLRUCache(CacheBase): 8 """ 9 An in-memory cache that has a maximum capacity. When the number of entries cached exceeds the `maxsize`, 10 the Least Recently Used entry will be deleted so that this cache won't consume infinitely large memory. 11 12 Note that: 13 1. This is an in-memory cache so the results can't be shared between two executions of the program 14 2. The retrieved value will be the same object every time, and will be the same as the saved value. This 15 means that any change on the saved/retrieved object will affect the objects to be retrieved with the 16 same context id later on. 17 3. Using this cache with any identifier that considers the parameter values is an analog to the built- 18 in `functools.lru_cache`. This module is mostly built for testing purpose, as the built-in function 19 should be much faster and is thus recommended to use. However, this might be useful in some cases, For 20 example, ignoring some parameters on purpose. In such case you could use a custom identifier together 21 with this cache. 22 4. If using in multiprocessing, this is not a shared memory object, so the cache will not be shared between 23 multiple processes 24 """ 25 26 def __init__(self, maxsize=None) -> None: 27 """ 28 Args: 29 maxsize: max size of this cache. If None, the size will be infinite 30 """ 31 self.__maxsize = maxsize 32 33 if self.__maxsize is None: 34 self.__d = {} 35 else: 36 self.__d = OrderedDict() 37 38 def save(self, context_id: ContextId, result: ReturnValue) -> None: 39 self.__d[context_id] = result 40 41 if self.__maxsize is not None: 42 self.__d.move_to_end(context_id) 43 44 if len(self.__d) > self.__maxsize: 45 self.__d.popitem(last=False) 46 47 def retrieve(self, context_id: ContextId) -> ReturnValue: 48 if context_id not in self.__d: 49 raise CheckpointNotExist 50 51 if self.__maxsize is not None: 52 self.__d.move_to_end(context_id) 53 54 return self.__d[context_id]
class
InMemoryLRUCache(abc.ABC, typing.Generic[~ContextId, ~ReturnValue]):
8class InMemoryLRUCache(CacheBase): 9 """ 10 An in-memory cache that has a maximum capacity. When the number of entries cached exceeds the `maxsize`, 11 the Least Recently Used entry will be deleted so that this cache won't consume infinitely large memory. 12 13 Note that: 14 1. This is an in-memory cache so the results can't be shared between two executions of the program 15 2. The retrieved value will be the same object every time, and will be the same as the saved value. This 16 means that any change on the saved/retrieved object will affect the objects to be retrieved with the 17 same context id later on. 18 3. Using this cache with any identifier that considers the parameter values is an analog to the built- 19 in `functools.lru_cache`. This module is mostly built for testing purpose, as the built-in function 20 should be much faster and is thus recommended to use. However, this might be useful in some cases, For 21 example, ignoring some parameters on purpose. In such case you could use a custom identifier together 22 with this cache. 23 4. If using in multiprocessing, this is not a shared memory object, so the cache will not be shared between 24 multiple processes 25 """ 26 27 def __init__(self, maxsize=None) -> None: 28 """ 29 Args: 30 maxsize: max size of this cache. If None, the size will be infinite 31 """ 32 self.__maxsize = maxsize 33 34 if self.__maxsize is None: 35 self.__d = {} 36 else: 37 self.__d = OrderedDict() 38 39 def save(self, context_id: ContextId, result: ReturnValue) -> None: 40 self.__d[context_id] = result 41 42 if self.__maxsize is not None: 43 self.__d.move_to_end(context_id) 44 45 if len(self.__d) > self.__maxsize: 46 self.__d.popitem(last=False) 47 48 def retrieve(self, context_id: ContextId) -> ReturnValue: 49 if context_id not in self.__d: 50 raise CheckpointNotExist 51 52 if self.__maxsize is not None: 53 self.__d.move_to_end(context_id) 54 55 return self.__d[context_id]
An in-memory cache that has a maximum capacity. When the number of entries cached exceeds the maxsize,
the Least Recently Used entry will be deleted so that this cache won't consume infinitely large memory.
Note that:
- This is an in-memory cache so the results can't be shared between two executions of the program
- The retrieved value will be the same object every time, and will be the same as the saved value. This means that any change on the saved/retrieved object will affect the objects to be retrieved with the same context id later on.
- Using this cache with any identifier that considers the parameter values is an analog to the built-
in
functools.lru_cache. This module is mostly built for testing purpose, as the built-in function should be much faster and is thus recommended to use. However, this might be useful in some cases, For example, ignoring some parameters on purpose. In such case you could use a custom identifier together with this cache. - If using in multiprocessing, this is not a shared memory object, so the cache will not be shared between multiple processes
InMemoryLRUCache(maxsize=None)
27 def __init__(self, maxsize=None) -> None: 28 """ 29 Args: 30 maxsize: max size of this cache. If None, the size will be infinite 31 """ 32 self.__maxsize = maxsize 33 34 if self.__maxsize is None: 35 self.__d = {} 36 else: 37 self.__d = OrderedDict()
Args
- maxsize: max size of this cache. If None, the size will be infinite
def
save(self, context_id: ~ContextId, result: ~ReturnValue) -> None:
39 def save(self, context_id: ContextId, result: ReturnValue) -> None: 40 self.__d[context_id] = result 41 42 if self.__maxsize is not None: 43 self.__d.move_to_end(context_id) 44 45 if len(self.__d) > self.__maxsize: 46 self.__d.popitem(last=False)
Save the result with the given context id.
Args
- context_id: identifier of the function call context
- result: return value of the function call
def
retrieve(self, context_id: ~ContextId) -> ~ReturnValue:
48 def retrieve(self, context_id: ContextId) -> ReturnValue: 49 if context_id not in self.__d: 50 raise CheckpointNotExist 51 52 if self.__maxsize is not None: 53 self.__d.move_to_end(context_id) 54 55 return self.__d[context_id]
Retrieve the function return value with the given context id. If there is no cached results for the context_id, throws a checkpointing.exceptions.CheckpointNotExist
Args
- context_id: identifier of the function call context
Returns
The return value of the function that corresponds to this context id