How can I cache a task result for two hours to prevent re-computation?

Prefect 2.0

Orion provides cache_key_fn and cache_expiration arguments in the @task decorator. You can use both in tandem to specify the cache condition and expiration.

import datetime
from prefect import task
from prefect.tasks import task_input_hash

@task(cache_key_fn=task_input_hash, cache_expiration=datetime.timedelta(hours=2))
def my_task():
    pass

For more details, check out the Caching docs page.

Prefect 1.0

Prefect 1.0 provides a cache_for argument in the @task decorator.

import datetime
from prefect import task

@task(cache_for=datetime.timedelta(hours=2))
def my_task():
    ...

For a deep dive on this topic, check out:

See also: