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