Store Memory
Store data persistently using the 0G decentralized storage network.Overview
The Memory class provides both ephemeral (temporary) and persistent storage capabilities. Persistent storage uses the 0G network to ensure your data is decentralized, secure, and always available.Constructor
Parameters
ZGStorageClient
required
The storage client for connecting to the 0G network
str
required
The storage bucket/stream ID for organizing data
int
default:"50"
Maximum number of ephemeral messages to keep in memory
Methods
remember()
Store data persistently on the 0G network.key(str): Unique identifier for the stored datavalue(Any): The data to store (will be JSON serialized)
recall()
Retrieve previously stored data from the 0G network.key(str): The key of the data to retrieve
Any - The stored data, or None if not found
Example:
forget()
Remove data from persistent storage.key(str): The key of the data to remove
Examples
Ephemeral Storage
For temporary data that doesn’t need persistence:set_ephemeral()
Store data in temporary memory.get_ephemeral()
Retrieve data from temporary memory.delete_ephemeral()
Remove data from temporary memory.Conversation Storage
remember_conversation()
Save the current conversation to persistent storage.recall_conversation()
Load a previously saved conversation.Data Types and Serialization
The Memory system automatically handles JSON serialization for common Python types:Supported Types
- Basic types:
str,int,float,bool,None - Collections:
list,dict,tuple - Datetime objects (converted to ISO strings)
- Custom objects (if JSON serializable)
Custom Serialization
Error Handling
Best Practices
- Key Naming: Use descriptive, hierarchical keys like
user_123_preferences - Data Size: Keep individual items under 1MB for optimal performance
- Batch Operations: Group related data to minimize storage calls
- Error Handling: Always handle potential storage failures gracefully
- Data Validation: Validate data before storing to prevent corruption
Performance Considerations
- Caching: Frequently accessed data is cached locally
- Compression: Large data is automatically compressed
- Batching: Multiple operations are batched when possible
- Retry Logic: Automatic retry for transient network failures