liveBook · Manning

Caching

As explained in Natural Language Processing in Action, Second Edition

Caching is the practice of storing computed results so repeated requests can be served without recomputing them.

Caching stores a function’s results so Streamlit can reuse them instead of rerunning expensive work whenever the app loads or its input changes. It is especially useful for heavy datasets and slow-to-load models. section 10.3.12

A Streamlit cache works by:

  • Hashing the function input so Streamlit can compare it with previous inputs. section 10.3.12
  • Saving the function output in a pickle file. section 10.3.12
  • Caching compatible models while recognizing that the FAISS document store cannot be cached or hashed in this setup. section 10.3.12
Listing 10.25 Loading the Reader and Retriever
>>> @st.cache_resource
>>> def load_retriever(document_store):                          #1
>>>     embedder = "sentence-transformers/multi-qa-mpnet-base-dot-v1"
>>>     return EmbeddingRetriever(
>>>         document_store=document_store,
>>>         embedding_model=embedder)
>>>
>>> @st.cache_resource
>>> def load_reader():
...     return TransformersReader(
...         model_name_or_path="deepset/roberta-base-squad2")

Decorate the retriever and reader loaders with st.cache_resource so model-loading work can be reused by the QA application. section 10.3.12

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Explains input hashing, pickle-backed outputs, and compatibility limits, then demonstrates resource decorators without covering cache invalidation.
Examples
Uses cached FAISS document-store loading and cached embedding-retriever and reader construction in a Streamlit QA app.
What it assumes
Assumes familiarity with Python functions and Streamlit application structure while explaining caching from its performance purpose.
What you can do after
Operate/configure Streamlit applications with st.cache_resource around expensive model-loading functions.
How it connects
Connects caching to Streamlit, function inputs, model loading, document stores, and question answering.

Also explained in

Compare how 38 books explain caching →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.