How to expire the buildCache contents for a particular task?

given a custom task annotated with CacheableTask, how do I control when the cached output should be refreshed?

For example, I want output to be cached for at most 24hrs (c.f. dependency resolution caches for dynamic versions).

CacheableTask itself only refers to decisions on whether to cache the output or not and does not comment on the interaction with upToDateWhen. It seems like I should override upToDateWhen but, for the time limited case, how to find out the age of the cache?

1 Like

I think you simply cannot.
You can configure for the whole cache when its entries will expire in days.
But to my knowledge not for a single task.
upToDateWhen is totally unrelated to this - kind of.
It determines whether the outputs of the task that are currently present on disk (or wherever) are already up-to-date or not. If this is true and the inputs didn’t change, the task is not rerun.
If it is false, the output is taken from the cache if enabled and the task is cachable (including its cacheIf actions return true).
You might now be tempted to use cacheIf for your use-case, but that will not only control reading to the cache but also writing, those cannot be controlled individually as far as I know.

I think the only chance you might have to somehow fake it is to have some magic input that controls whether the cache key changes or not and thus expires the entry. But for that you would also need to keep track of when the cache entry was written last and so on. I don’t really think this is possible currently.

2 Likes