Paul Gierz
10/09/2024, 9:01 AMMarvin
10/09/2024, 9:04 AMPREFECT_LOCAL_STORAGE_PATH
setting. Caching is limited to flows with access to that storage path.
- Manual Cache Key: You can define a custom cache key function to alter caching behavior. Here’s an example:
python
def cache_within_flow_run(context, parameters):
return f"{context.task_run.flow_run_id}-{task_input_hash(context, parameters)}"
@task(cache_key_fn=cache_within_flow_run)
def cached_task():
print('running an expensive operation')
return 42
- Refreshing Cache: To refresh the cache and force a task to update its data, use the refresh_cache
option:
python
def static_cache_key(context, parameters):
return "static cache key"
@task(cache_key_fn=static_cache_key, refresh_cache=True)
def caching_task():
return random.random()
### Prefect 3.x
- Task Caching: In Prefect 3.x, caching involves tasks entering a Completed
state without re-running the defined code, based on inputs, code definition, and flow/task run ID.
- Cache Key: Customize the cache key similarly to Prefect 2.x using attributes like input and run IDs for idempotency.
- Refresh Cache: Use refresh_cache
to ignore existing cache and rerun tasks. You can also set PREFECT_TASKS_REFRESH_CACHE=true
to refresh caches for all tasks globally.
- Example:
python
import random
def static_cache_key(context, parameters):
return "static cache key"
@task(cache_key_fn=static_cache_key, refresh_cache=True)
def caching_task():
return random.random()
Links to Prefect documentation:
- Prefect 2.x: Prefect 2.x Caching
- Prefect 3.x: Prefect 3.x Caching
These examples and explanations should help you to interact with and manage cached results in an interactive Python session using Prefect. If you have specific use cases or need more detailed guidance, feel free to ask!Paul Gierz
10/09/2024, 9:06 AMPaul Gierz
10/09/2024, 10:35 AMMarvin
10/09/2024, 10:36 AM