Visitar URL original
feat: add ttl and time_field support to CacheChecker by Mariam-Amin12 · Pull Request #12987 · deepset-ai/haystack · GitHub
Skip to content

feat: add ttl and time_field support to CacheChecker - #12987

Open
Mariam-Amin12 wants to merge 5 commits into
deepset-ai:mainfrom
Mariam-Amin12:add-TTL-support-to-CacheChecker
Open

Mariam-Amin12 wants to merge 5 commits into
deepset-ai:mainfrom
Mariam-Amin12:add-TTL-support-to-CacheChecker

Conversation

@Mariam-Amin12

Copy link
Copy Markdown

Proposed Changes:

Added optional TTL support to CacheChecker to allow cached documents to expire.

  • Added an optional ttl parameter.
  • Added a configurable time_field parameter for the cache timestamp.
  • A cache entry is considered a hit only when its timestamp is within the configured TTL.
  • Missing, malformed, or invalid timestamps are treated as cache misses when TTL is enabled.
  • Preserved the existing behavior when ttl=None.
  • Added support for both synchronous and asynchronous cache checking.

How did you test it?

Added and ran unit tests covering:

  • Fresh cache entries within the TTL
  • Expired cache entries
  • Missing timestamp fields
  • ISO timestamp strings
  • Naive datetime timestamps (no tzinfo)
  • Malformed timestamp strings
  • Non-datetime timestamp values
  • Numeric TTL values (seconds, not just timedelta)
  • Serialization/deserialization round-tripping with ttl and time_field set
  • Behavior when TTL is not configured (backward compatibility)
  • Async CacheChecker behavior with TTL (fresh + expired)

Ran:

hatch run test:unit test/components/caching/

Result: 26 passed

Notes for the reviewer

The existing behavior is preserved when ttl=None. TTL validation is only applied when TTL is configured.

Checklist

@Mariam-Amin12
Mariam-Amin12 requested a review from a team as a code owner September 27, 2026 14:32
@Mariam-Amin12
Mariam-Amin12 requested review from bogdankostic and removed request for a team September 27, 2026 14:32
@vercel

vercel Bot commented Sep 27, 2026

Copy link
Copy Markdown
Contributor

@Mariam-Amin12 is attempting to deploy a commit to the deepset Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions

Copy link
Copy Markdown
Contributor

Hi @Mariam-Amin12, thanks for your interest in contributing to Haystack! 🙏

⚠️ Issue #12967 is already being addressed by open pull request(s) #12979. Before opening a PR for an issue, please check whether a PR is already linked to it, and consider contributing to the existing PR instead. We may close duplicate PRs to keep the review queue manageable.

This is an automated message to help us keep the review queue healthy.

@github-actions github-actions Bot added the type:documentation Improvements on the docs label Oct 6, 2026
@github-actions

github-actions Bot commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/components/agents
  agent.py
  haystack/components/builders
  chat_prompt_builder.py
  haystack/components/caching
  cache_checker.py
  haystack/components/converters
  csv.py
  docx.py
  json.py
  xlsx.py
  haystack/components/generators/chat
  azure.py
  openai.py
  openai_responses.py
  haystack/components/joiners
  branch.py
  haystack/components/preprocessors
  recursive_splitter.py
  haystack/components/rankers
  meta_field.py
  haystack/components/retrievers
  sentence_window_retriever.py
  haystack/components/validators
  json_schema.py
  haystack/dataclasses
  chat_message.py
  haystack/document_stores/in_memory
  document_store.py
  haystack/hooks/compaction
  hooks.py
  haystack/tools
  component_tool.py
  searchable_toolset.py
  tool.py
  toolset.py
  utils.py
  haystack/tools/skills
  skill_toolset.py
  haystack/utils
  misc.py
Project Total  

The report is truncated to 25 files out of 31. To see the full report, please visit the workflow summary page.

This report was generated by python-coverage-comment-action

@github-actions github-actions Bot added the P3 Low priority, leave it in the backlog label Oct 8, 2026
@CLAassistant

CLAassistant commented Nov 5, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@HaystackBot

Copy link
Copy Markdown
Contributor

Hi @Mariam-Amin12, thanks a lot for your contribution! 🙏

We noticed that the Contributor License Agreement (CLA) check (license/cla) hasn't passed yet, so we've temporarily moved this PR to draft and paused the review assignment.

To get your PR reviewed, please sign the CLA via the link in the license/cla check below (or in the CLA bot comment). As soon as the check turns green, this PR will automatically be marked ready for review again and a reviewer will be re-assigned.

@HaystackBot
HaystackBot removed the request for review from bogdankostic November 5, 2026 08:53
@HaystackBot HaystackBot added the cla-pending PR is in draft until the contributor signs the CLA label Nov 5, 2026
@HaystackBot
HaystackBot marked this pull request as draft November 5, 2026 08:53
@HaystackBot
HaystackBot marked this pull request as ready for review November 5, 2026 09:24
@HaystackBot HaystackBot removed the cla-pending PR is in draft until the contributor signs the CLA label Nov 5, 2026
@HaystackBot

Copy link
Copy Markdown
Contributor

Thanks for signing the CLA, @Mariam-Amin12! 🎉 This PR is now ready for review again and the reviewer has been re-assigned.

@bogdankostic bogdankostic left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this @Mariam-Amin12! Keeping ttl optional and storing it as seconds looks good. The main things to fix are naive timestamps being treated as UTC and time_field not accepting the meta. prefix.

found = self.document_store.filter_documents(filters=filters)
if found:
found_documents.extend(found)
fresh = [doc for doc in found if self._is_fresh(doc)]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if we could push the cutoff into the filter instead of checking each document in Python:

cutoff = datetime.now(UTC) - self.ttl
filters = {
    "operator": "AND",
    "conditions": [
        {"field": self.cache_field, "operator": "==", "value": item},
        {"field": self.time_field, "operator": ">=", "value": cutoff.isoformat()},
    ],
}

The store would only return fresh documents and we wouldn't need our own timestamp parsing. It would also help with stale copies: a new cached_at changes the document ID, so re-caching an item writes a second document instead of overwriting the old one, and right now every check fetches all of them. The trade-off is that time_field would need to be stored in a format the store can range-filter on. What do you think?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to include this optimization in the current PR if possible. Can we handle invalid timestamps safely while keeping compatibility across the supported DocumentStores and preserving the existing behavior (invalid or missing timestamps result in cache misses)?

@Mariam-Amin12

Copy link
Copy Markdown
Author

@bogdankostic I've addressed the review feedback and pushed the latest changes. The implementation and tests have been updated accordingly. Ready for another review. Thanks!

This branch has not been deployed

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

Labels

P3 Low priority, leave it in the backlog topic:tests type:documentation Improvements on the docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add TTL / expiration support to CacheChecker

4 participants