/ docs-website / reference / haystack-api / cachings_api.md
cachings_api.md
 1  ---
 2  title: "Caching"
 3  id: caching-api
 4  description: "Checks if any document coming from the given URL is already present in the store."
 5  slug: "/caching-api"
 6  ---
 7  
 8  
 9  ## cache_checker
10  
11  ### CacheChecker
12  
13  Checks for the presence of documents in a Document Store based on a specified field in each document's metadata.
14  
15  If matching documents are found, they are returned as "hits". If not found in the cache, the items
16  are returned as "misses".
17  
18  ### Usage example
19  
20  ```python
21  from haystack import Document
22  from haystack.document_stores.in_memory import InMemoryDocumentStore
23  from haystack.components.caching.cache_checker import CacheChecker
24  
25  docstore = InMemoryDocumentStore()
26  documents = [
27      Document(content="doc1", meta={"url": "https://example.com/1"}),
28      Document(content="doc2", meta={"url": "https://example.com/2"}),
29      Document(content="doc3", meta={"url": "https://example.com/1"}),
30      Document(content="doc4", meta={"url": "https://example.com/2"}),
31  ]
32  docstore.write_documents(documents)
33  checker = CacheChecker(docstore, cache_field="url")
34  results = checker.run(items=["https://example.com/1", "https://example.com/5"])
35  assert results == {"hits": [documents[0], documents[2]], "misses": ["https://example.com/5"]}
36  ```
37  
38  #### __init__
39  
40  ```python
41  __init__(document_store: DocumentStore, cache_field: str) -> None
42  ```
43  
44  Creates a CacheChecker component.
45  
46  **Parameters:**
47  
48  - **document_store** (<code>DocumentStore</code>) – Document Store to check for the presence of specific documents.
49  - **cache_field** (<code>str</code>) – Name of the document's metadata field
50    to check for cache hits.
51  
52  #### to_dict
53  
54  ```python
55  to_dict() -> dict[str, Any]
56  ```
57  
58  Serializes the component to a dictionary.
59  
60  **Returns:**
61  
62  - <code>dict\[str, Any\]</code> – Dictionary with serialized data.
63  
64  #### from_dict
65  
66  ```python
67  from_dict(data: dict[str, Any]) -> CacheChecker
68  ```
69  
70  Deserializes the component from a dictionary.
71  
72  **Parameters:**
73  
74  - **data** (<code>dict\[str, Any\]</code>) – Dictionary to deserialize from.
75  
76  **Returns:**
77  
78  - <code>CacheChecker</code> – Deserialized component.
79  
80  #### run
81  
82  ```python
83  run(items: list[Any]) -> dict[str, Any]
84  ```
85  
86  Checks if any document associated with the specified cache field is already present in the store.
87  
88  **Parameters:**
89  
90  - **items** (<code>list\[Any\]</code>) – Values to be checked against the cache field.
91  
92  **Returns:**
93  
94  - <code>dict\[str, Any\]</code> – A dictionary with two keys:
95  - `hits` - Documents that matched with at least one of the items.
96  - `misses` - Items that were not present in any documents.