Bird
0
0

Given this code snippet using Langchain caching:

medium📝 state output Q13 of 15
LangChain - Production Deployment
Given this code snippet using Langchain caching:
cache = InMemoryCache()
result1 = cache.get_or_set('key1', lambda: 'data1')
result2 = cache.get_or_set('key1', lambda: 'data2')
What will be the value of result2?
A'data2'
BNone
C'data1'
DRaises an error
Step-by-Step Solution
Solution:
  1. Step 1: Understand get_or_set behavior

    If the key exists, it returns cached value without calling the function.
  2. Step 2: Apply to given code

    First call caches 'data1' under 'key1'. Second call finds 'key1' and returns 'data1', ignoring 'data2'.
  3. Final Answer:

    'data1' -> Option C
  4. Quick Check:

    Cache returns stored value, not new function result [OK]
Quick Trick: get_or_set returns cached value if key exists [OK]
Common Mistakes:
MISTAKES
  • Assuming second lambda runs and returns 'data2'
  • Expecting None if key exists
  • Thinking it raises an error on duplicate keys

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LangChain Quizzes