Chu
11/21/2025, 8:09 PMPREFECT_TASKS_DEFAULT_NO_CACHE set globally?Marvin
11/21/2025, 8:09 PMMarvin
11/21/2025, 8:12 PMPREFECT_TASKS_DEFAULT_NO_CACHE=true, tasks default to NO_CACHE only if you don’t explicitly set caching on the task. To enable caching for a specific task, provide either a cache_policy or a cache_key_fn on that task. There’s no no_cache=False flag.
Common options:
- Use the default policy (recommended)
from prefect import task
from prefect.cache_policies import DEFAULT
@task(cache_policy=DEFAULT)
def my_task(x):
return x * 2
- Cache based on inputs only
from prefect import task
from prefect.cache_policies import Inputs
@task(cache_policy=Inputs())
def my_task(x):
return x * 2
- Custom cache key (optionally with expiration)
from datetime import timedelta
from prefect import task
from prefect.tasks import task_input_hash
@task(cache_key_fn=task_input_hash, cache_expiration=timedelta(hours=1))
def my_task(x):
return x * 2
- If you can’t edit the decorator, override with with_options
from prefect.cache_policies import DEFAULT
cached_task = my_task.with_options(cache_policy=DEFAULT)
Notes:
- cache_key_fn takes precedence if both cache_policy and cache_key_fn are set.
- Setting only cache_expiration does not enable caching; pair it with a cache_policy or cache_key_fn.