Skip to content

Add TTL / expiration support to CacheChecker #12967

Description

@Mariam-Amin12

Problem

CacheChecker checks whether an item exists in a document store’s cache_field, but it has no expiration support. Once a document is cached, it is treated as a hit indefinitely, even when the data is stale.

Description

Add optional TTL support to CacheChecker:

ttl: timedelta | None = None
time_field: str = "cached_at"

When ttl is configured, a matching document should count as a cache hit only if:

datetime.now() - document.meta[time_field] < ttl

Documents with a missing timestamp or an expired timestamp should be treated as misses, just like items that are not cached.

Setting ttl=None should preserve the current behavior for full backward compatibility. The same expiration logic should apply to both run() and run_async().

Example:

from datetime import timedelta

cache_checker = CacheChecker(
    document_store,
    cache_field="url",
    ttl=timedelta(hours=24),
)

Alternatives considered

A separate TTLCacheChecker component or subclass was considered, but extending CacheChecker with optional parameters seems simpler and avoids duplicating existing cache-checking behavior.

Additional context

The feature should include tests for fresh entries, expired entries, missing timestamps, synchronous and asynchronous execution, serialization, and backward compatibility when ttl=None.

If this feature is of interest, I would be happy to work on the implementation and add the corresponding tests and documentation.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P3Low priority, leave it in the backlog

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions