> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-HK/llms.txt # Mastra.getMemory() `.getMemory()` 方法會按鍵從 Mastra 註冊表擷取記憶體執行個體。記憶體執行個體會在 Mastra 建構函式中註冊,並可由已儲存的 Agent 參照。 ## 使用範例 ```typescript const memory = mastra.getMemory('conversationMemory') // Use the memory instance const thread = await memory.createThread({ resourceId: 'user-123', title: 'New Conversation', }) ``` ## 參數 **key** (`TMemoryKey extends keyof TMemory`): 要擷取的記憶體執行個體的註冊表鍵。必須與在 Mastra 建構函式中註冊記憶體時使用的鍵相符。 ## 傳回值 **memory** (`TMemory[TMemoryKey]`): 具有指定鍵的記憶體執行個體。如找不到記憶體,則會擲回錯誤。 ## 範例:註冊及擷取記憶體 ```typescript import { Mastra } from '@mastra/core' import { Memory } from '@mastra/memory' import { LibSQLStore } from '@mastra/libsql' const conversationMemory = new Memory({ storage: new LibSQLStore({ id: 'conversation-store', url: ':memory:' }), }) const mastra = new Mastra({ memory: { conversationMemory, }, }) // Later, retrieve the memory instance const memory = mastra.getMemory('conversationMemory') ``` ## 相關內容 - [Mastra.listMemory()](https://mastra.zisheng.pro/zh-HK/reference/core/listMemory) - [記憶體概覽](https://mastra.zisheng.pro/zh-HK/docs/memory/overview)