Skip to content

Latest commit

 

History

History

03_caching

Caching

When a feature is expensive or slow to compute, you may wish to cache its value. Chalk uses the terminology "maximum staleness" to describe how recently a feature value needs to have been computed to be returned without re-running a resolver.

https://docs.chalk.ai/docs/feature-caching

1. Basic Caching

Cache feature values rather than computing them realtime.

1_basic_caching.py

@features
class User:
    fico_score: int = feature(max_staleness="30d")

https://docs.chalk.ai/docs/feature-caching

2. Latest Computed Value

Cache the last computed example of the feature.

2_latest_value.py

@features
class User:
    fico_score: int = feature(max_staleness="infinity")

https://docs.chalk.ai/docs/feature-caching

3. Intermediate Feature Values

Cache intermediate feature values.

3_intermediates.py

ChalkClient().query(
    input={ ... },
    # User.fico_score is not requested in the output...
    output=[User.risk_score],
    # ...but you can specify the staleness anyhow!
    staleness={User.fico_score: "10m"},
)

https://docs.chalk.ai/docs/query-caching

4. Override Max-Staleness

Set max-staleness per-request.

4_override_max_staleness.py

@features
class User:
    fico_score: int = feature(max_staleness="30d")

ChalkClient().query(
    input={...},
    output=[User.fico_score],
    staleness={User.fico_score: "10m"},
)

https://docs.chalk.ai/docs/query-caching

5. Override Cache Values

Supply a feature value in the input to skip the cache and any resolver entirely.

5_override_cache_values.py

@features
class User:
    fico_score: int = feature(max_staleness="30d")

ChalkClient().query(
    input={User.fico_score: 1, ...},
    output=[...],
)

https://docs.chalk.ai/docs/query-caching

6. Cache Busting

Bypass the cache with a max-staleness of 0.

6_cache_busting.py

ChalkClient().query(
    input={...},
    output=[User.fico_score],
    staleness={User.fico_score: "0s"},
)

https://docs.chalk.ai/docs/query-caching#cache-busting

7. Pre-Fetching

Keep the cache warm by scheduling a resolver to run more frequently than the max-staleness.

7_prefetching.py

@features
class User:
    fico_score: int = feature(max_staleness="30d")

@realtime(cron="29d 11h")
def get_fico_score(name: User.name) -> User.fico_score:
    return requests.get("https://experian.com").json()["score"]

https://docs.chalk.ai/docs/resolver-cron