Skip to main content

Cache decorator

Caching stores the result of slow function calls so they only need to run once. This persists objects across reruns and makes UDFs faster.

Basic

To cache a function decorate it with @fused.cache.

@fused.udf
def udf():
import pandas as pd

@fused.cache
def load_data(i):
return pd.DataFrame({'id': i})

first = load_data(i=1)
second = load_data(i=2)
return pd.concat([first, second])

The first time Fused sees the function code and parameters, Fused runs the function and stores the return value in a cache. The next time the function is called with the same parameters and code, Fused skips running the function and returns the cached value.

Advanced

This UDF illustrates the use of:

  • Passing bbox to make the output unique to each Tile
  • Setting a custom cache directory with the optional path parameter
  • Reseting the cache by running the function once with reset=True
@fused.udf
def udf(bbox: fused.types.TileGDF=None):

@fused.cache(path='optional_cache_dir', reset=True)
def set_name(bbox, name):
bbox['name'] = name
return bbox

return set_name(bbox, "San Francisco")