#11Cache-before-search, write-after-fetchMediumAgentic AI
Cache-before-search, write-after-fetch
Background
The shared-memory executor pattern: before searching, check the cache; on a miss, search and write the result back so others benefit.
cached = memory.get(query)
if cached is not None:
results = cached # free, no search call
else:
results = search(query)
memory.set(query, results) # write-after-fetch
Problem statement
Implement cached_search(query, memory, search) that returns the results, using memory (a dict) as the cache.
Input
query— the search query string.memory— a dict acting as the shared store ({query: results}); mutate it in place.search— callablequery -> results(only call it on a cache miss).
Output
Returns the results list. On a hit, returns the cached value without calling search. On a miss, calls search(query), stores it in memory, and returns it.
Examples
Example 1 — cache hit (no search)
memory = {"q": ["cached"]}
cached_search("q", memory, search) # -> ["cached"], search NOT called
Example 2 — cache miss (search + write)
memory = {}
cached_search("q", memory, lambda q: ["fresh"]) # -> ["fresh"]; memory == {"q": ["fresh"]}
Constraints
- If
query in memory: returnmemory[query]and do not callsearch. - Else:
results = search(query), setmemory[query] = results, returnresults. - Mutate
memoryin place on a miss.
Notes
- Calling
searchonly on a miss is the whole point — that's where the dedup savings come from.
Python
Loading...
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Reference example: cache hit does not call search
- •Sample: cache miss searches and writes back
- •Reference: returns the cached value on a hit