koladata

Home
Overview
Fundamentals
Glossary
Cheatsheet
API Reference
Quick Recipes
Deep Dive
Common Pitfalls and Gotchas
Persistent Storage

View the Project on GitHub google/koladata

kd.caching.LruCache API

LRU cache Object/Entity -> DataSlice.
Subcategory Description
Mode Mode for LruCache.

LruCache.Mode(*values)

Mode for LruCache.

LruCache.__init__(self, capacity: int, mode: Mode)

Initialize self.  See help(type(self)) for accurate signature.

LruCache.cache_fn(self, func_id: str, is_hit_fn: Callable[[DataSlice], DataSlice] | None = None) -> Callable[[Callable[[DataSlice], DataSlice]], Callable[[DataSlice], DataSlice]]

Caching decorator for DataSlice->DataSlice functions.

In case of a partial cache hit, the newly calculated results and the cached
results will be interleaved directly using '|', so the schema it required
to be stable between runs.

Example:
  cache = kd.caching.LruCache(capacity=10)

  @cache.cache_fn('my_func')
  def fn(x):
    print(f'arg: {x}')
    return x * x

  print('[2, 3] ->', fn(kd.slice([2, 3])))  # no cache hit
  # arg: DataSlice([2, 3])
  # [2, 3] -> DataSlice([4, 9])
  print('[3, 4] ->', fn(kd.slice([3, 4])))  # partial cache hit
  # arg: DataSlice([None, 4])
  # [3, 4] -> DataSlice([9, 16])
  print('[3, 4] ->', fn(kd.slice([3, 4])))  # full cache hit
  # [3, 4] -> DataSlice([9, 16])

Args:
  func_id: Function id is added to each key. It is needed to avoid
    collisions if one LruCache is used for caching results of different
    functions. If func_id is reused by two function, then results of one
    function will override results of another function in the cache.
  is_hit_fn: Optional function returning MASK DataSlice. If specified and
    returns kd.missing for some value, then this value will not be
    considered a cache hit.

Returns:
  Decorator function.

LruCache.clear(self) -> None

No description