Skip to content

Commit

Permalink
Documentation updates
Browse files Browse the repository at this point in the history
  • Loading branch information
promptless[bot] authored Dec 7, 2024
1 parent 525d842 commit 8df7ca4
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions docs/3.0/develop/task-caching.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def my_stateful_task():
my_stateful_task() # sleeps again
```

Let us know if you need more help!
## Customizing the cache

Prefect allows you to configure task caching behavior in numerous ways.
Expand Down Expand Up @@ -160,6 +161,30 @@ my_cached_task(1)
my_cached_task(1, debug=True) # still uses the cache
```

### Handling Non-Serializable Objects in Task Inputs

To handle non-serializable objects in task inputs, you can use one of these two approaches:

1. **Custom Cache Key Function**: Serialize only the relevant properties of the input by defining a custom cache key function. Example:

```python
def custom_cache_key_fn(context, parameters):
return parameters["some_object"].name
```

2. **Pydantic Custom Serialization**: Use Pydantic’s `@model_serializer` to control which parts of the object are serialized. Example:

```python
@model_serializer
def ser_model(self):
return {"name": self.name}
```

Choose the approach based on your needs:
- Custom cache keys for task-specific logic.
- Pydantic for consistent model serialization.

Let us know if you need more help!
### Cache key functions

You can configure custom cache policy logic through the use of cache key functions.
Expand Down Expand Up @@ -446,6 +471,26 @@ def hello_flow(name_input):
hello_task(name_input)
```

### Handling Non-Serializable Objects

To handle non-serializable objects in task inputs, you can use one of these two approaches:

1. **Custom Cache Key Function**: Serialize only the relevant properties of the input by defining a custom cache key function. Example:
```python
def custom_cache_key_fn(context, parameters):
return parameters["some_object"].name
```

2. **Pydantic Custom Serialization**: Use Pydantic’s `@model_serializer` to control which parts of the object are serialized. Example:
```python
@model_serializer
def ser_model(self):
return {"name": self.name}
```

Choose the approach based on your needs:
- Custom cache keys for task-specific logic.
- Pydantic for consistent model serialization.
## Force ignore the cache

A cache "refresh" instructs Prefect to ignore the data associated with a task's cache key and rerun
Expand Down

0 comments on commit 8df7ca4

Please sign in to comment.