!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){var t=null;try{t=new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}return t}()||function(){var t=null;try{t=localStorage.getItem("theme")}catch(t){}return t}();t(null!==e?e:"light")}(),document.documentElement.setAttribute("data-announcement-bar-initially-dismissed",function(){try{return"true"===localStorage.getItem("docusaurus.announcement.dismiss")}catch(t){}return!1}())

[Skip to main content](#__docusaurus_skipToContent_fallback)

LangChain v0.2 is coming soon! Preview the new docs [here](/v0.2/docs/introduction/).

[

![🦜️🔗 Langchain](/v0.1/img/brand/wordmark.png)![🦜️🔗 Langchain](/v0.1/img/brand/wordmark-dark.png)

](/v0.1/)[Docs](/v0.1/docs/get_started/introduction/)[Use cases](/v0.1/docs/use_cases/)[Integrations](/v0.1/docs/integrations/platforms/)[API Reference](https://api.js.langchain.com)

[More](#)

*   [People](/v0.1/docs/people/)
*   [Community](/v0.1/docs/community/)
*   [Tutorials](/v0.1/docs/additional_resources/tutorials/)
*   [Contributing](/v0.1/docs/contributing/)

[v0.1](#)

*   [v0.2](https://js.langchain.com/v0.2/docs/introduction)
*   [v0.1](/v0.1/docs/get_started/introduction/)

[🦜🔗](#)

*   [LangSmith](https://smith.langchain.com)
*   [LangSmith Docs](https://docs.smith.langchain.com)
*   [LangChain Hub](https://smith.langchain.com/hub)
*   [LangServe](https://github.com/langchain-ai/langserve)
*   [Python Docs](https://python.langchain.com/)

[Chat](https://chatjs.langchain.com)[](https://github.com/langchain-ai/langchainjs)

Search

*   [Providers](/v0.1/docs/integrations/platforms/)
    
    *   [Providers](/v0.1/docs/integrations/platforms/)
    *   [Anthropic](/v0.1/docs/integrations/platforms/anthropic/)
    *   [AWS](/v0.1/docs/integrations/platforms/aws/)
    *   [Google](/v0.1/docs/integrations/platforms/google/)
    *   [Microsoft](/v0.1/docs/integrations/platforms/microsoft/)
    *   [OpenAI](/v0.1/docs/integrations/platforms/openai/)
*   [Components](/v0.1/docs/integrations/components/)
    
    *   [LLMs](/v0.1/docs/integrations/llms/)
        
    *   [Chat models](/v0.1/docs/integrations/chat/)
        
    *   [Document loaders](/v0.1/docs/integrations/document_loaders/)
        
    *   [Document transformers](/v0.1/docs/integrations/document_transformers/)
        
    *   [Document compressors](/v0.1/docs/integrations/document_compressors/)
        
    *   [Text embedding models](/v0.1/docs/integrations/text_embedding/)
        
    *   [Vector stores](/v0.1/docs/integrations/vectorstores/)
        
    *   [Retrievers](/v0.1/docs/integrations/retrievers/)
        
    *   [Tools](/v0.1/docs/integrations/tools/)
        
    *   [Agents and toolkits](/v0.1/docs/integrations/toolkits/)
        
    *   [Chat Memory](/v0.1/docs/integrations/chat_memory/)
        
    *   [Stores](/v0.1/docs/integrations/stores/)
        
        *   [Cassandra KV](/v0.1/docs/integrations/stores/cassandra_storage/)
        *   [File System Store](/v0.1/docs/integrations/stores/file_system/)
        *   [In Memory Store](/v0.1/docs/integrations/stores/in_memory/)
        *   [Stores](/v0.1/docs/integrations/stores/)
        *   [IORedis](/v0.1/docs/integrations/stores/ioredis_storage/)
        *   [Upstash Redis](/v0.1/docs/integrations/stores/upstash_redis_storage/)
        *   [Vercel KV](/v0.1/docs/integrations/stores/vercel_kv_storage/)

*   [](/v0.1/)
*   [Components](/v0.1/docs/integrations/components/)
*   Stores

On this page

Stores
======

Storing data in key value format is quick and efficient, and can be a powerful tool for LLM applications. The `BaseStore` class provides a simple interface for getting, setting, deleting and iterating over lists of key value pairs.

The public API of `BaseStore` in LangChain JS offers four main methods:

    abstract mget(keys: K[]): Promise<(V | undefined)[]>;abstract mset(keyValuePairs: [K, V][]): Promise<void>;abstract mdelete(keys: K[]): Promise<void>;abstract yieldKeys(prefix?: string): AsyncGenerator<K | string>;

The `m` prefix stands for multiple, and indicates that these methods can be used to get, set and delete multiple key value pairs at once. The `yieldKeys` method is a generator function that can be used to iterate over all keys in the store, or all keys with a given prefix.

It's that simple!

So far LangChain.js has two base integrations for `BaseStore`:

*   [`InMemoryStore`](/v0.1/docs/integrations/stores/in_memory/)
*   [`LocalFileStore`](/v0.1/docs/integrations/stores/file_system/) (Node.js only)

Use Cases[​](#use-cases "Direct link to Use Cases")
---------------------------------------------------

### Chat history[​](#chat-history "Direct link to Chat history")

If you're building web apps with chat, the `BaseStore` family of integrations can come in very handy for storing and retrieving chat history.

### Caching[​](#caching "Direct link to Caching")

The `BaseStore` family can be a useful alternative to our other caching integrations. For example the [`LocalFileStore`](/v0.1/docs/integrations/stores/file_system/) allows for persisting data through the file system. It also is incredibly fast, so your users will be able to access cached data in a snap.

See the individual sections for deeper dives on specific storage providers.

Reading Data[​](#reading-data "Direct link to Reading Data")
------------------------------------------------------------

### In Memory[​](#in-memory "Direct link to In Memory")

Reading data is simple with KV stores. Below is an example using the [`InMemoryStore`](/v0.1/docs/integrations/stores/in_memory/) and the `.mget()` method. We'll also set our generic value type to `string` so we can have type safety setting our strings.

Import the [`InMemoryStore`](/v0.1/docs/integrations/stores/in_memory/) class.

    import { InMemoryStore } from "langchain/storage/in_memory";

Instantiate a new instance and pass `string` as our generic for the value type.

    const store = new InMemoryStore<string>();

Next we can call `.mset()` to write multiple values at once.

    const data: [string, string][] = [  ["key1", "value1"],  ["key2", "value2"],];await store.mset(data);

Finally, call the `.mget()` method to retrieve the values from our store.

    const data = await store.mget(["key1", "key2"]);console.log(data);/** * ["value1", "value2"] */

### File System[​](#file-system "Direct link to File System")

When using the file system integration we need to instantiate via the `fromPath` method. This is required because it needs to preform checks to ensure the directory exists and is readable/writable. You also must use a directory when using [`LocalFileStore`](/v0.1/docs/integrations/stores/file_system/) because each entry is stored as a unique file in the directory.

    import { LocalFileStore } from "langchain/storage/file_system";

    const pathToStore = "./my-store-directory";const store = await LocalFileStore.fromPath(pathToStore);

To do this we can define an encoder for initially setting our data, and a decoder for when we retrieve data.

    const encoder = new TextEncoder();const decoder = new TextDecoder();

    const data: [string, Uint8Array][] = [  ["key1", encoder.encode(new Date().toDateString())],  ["key2", encoder.encode(new Date().toDateString())],];await store.mset(data);

    const data = await store.mget(["key1", "key2"]);console.log(data.map((v) => decoder.decode(v)));/** * [ 'Wed Jan 03 2024', 'Wed Jan 03 2024' ] */

Writing Data[​](#writing-data "Direct link to Writing Data")
------------------------------------------------------------

### In Memory[​](#in-memory-1 "Direct link to In Memory")

Writing data is simple with KV stores. Below is an example using the [`InMemoryStore`](/v0.1/docs/integrations/stores/in_memory/) and the `.mset()` method. We'll also set our generic value type to `Date` so we can have type safety setting our dates.

Import the [`InMemoryStore`](/v0.1/docs/integrations/stores/in_memory/) class.

    import { InMemoryStore } from "langchain/storage/in_memory";

Instantiate a new instance and pass `Date` as our generic for the value type.

    const store = new InMemoryStore<Date>();

Finally we can call `.mset()` to write multiple values at once.

    const data: [string, Date][] = [  ["date1", new Date()],  ["date2", new Date()],];await store.mset(data);

### File System[​](#file-system-1 "Direct link to File System")

When using the file system integration we need to instantiate via the `fromPath` method. This is required because it needs to preform checks to ensure the directory exists and is readable/writable. You also must use a directory when using [`LocalFileStore`](/v0.1/docs/integrations/stores/file_system/) because each entry is stored as a unique file in the directory.

    import { LocalFileStore } from "langchain/storage/file_system";

    const pathToStore = "./my-store-directory";const store = await LocalFileStore.fromPath(pathToStore);

When defining our data we must convert the values to `Uint8Array` because the file system integration only supports binary data.

To do this we can define an encoder for initially setting our data, and a decoder for when we retrieve data.

    const encoder = new TextEncoder();const decoder = new TextDecoder();

    const data: [string, Uint8Array][] = [  ["key1", encoder.encode(new Date().toDateString())],  ["key2", encoder.encode(new Date().toDateString())],];await store.mset(data);

* * *

#### Help us out by providing feedback on this documentation page:

[

Previous

Zep Memory

](/v0.1/docs/integrations/chat_memory/zep_memory/)[

Next

Cassandra KV

](/v0.1/docs/integrations/stores/cassandra_storage/)

*   [Use Cases](#use-cases)
    *   [Chat history](#chat-history)
    *   [Caching](#caching)
*   [Reading Data](#reading-data)
    *   [In Memory](#in-memory)
    *   [File System](#file-system)
*   [Writing Data](#writing-data)
    *   [In Memory](#in-memory-1)
    *   [File System](#file-system-1)

Community

*   [Discord](https://discord.gg/cU2adEyC7w)
*   [Twitter](https://twitter.com/LangChainAI)

GitHub

*   [Python](https://github.com/langchain-ai/langchain)
*   [JS/TS](https://github.com/langchain-ai/langchainjs)

More

*   [Homepage](https://langchain.com)
*   [Blog](https://blog.langchain.dev)

Copyright © 2024 LangChain, Inc.