跳到主要内容
Memcached 是一个免费、开源、高性能、分布式内存对象缓存系统,通用性强,旨在通过减轻数据库负载来加速动态 Web 应用程序。
本页介绍了如何使用 Langchain 和 pymemcache 作为客户端连接到已运行的 Memcached 实例。

安装和设置

pip install pymemcache

LLM 缓存

将 Memcached 缓存集成到您的应用程序中
from langchain.globals import set_llm_cache
from langchain_openai import OpenAI

from langchain_community.cache import MemcachedCache
from pymemcache.client.base import Client

llm = OpenAI(model="gpt-3.5-turbo-instruct", n=2, best_of=2)
set_llm_cache(MemcachedCache(Client('localhost')))

# The first time, it is not yet in cache, so it should take longer
llm.invoke("Which city is the most crowded city in the USA?")

# The second time it is, so it goes faster
llm.invoke("Which city is the most crowded city in the USA?")

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.