Skip to main content

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.
Parameters:
  • key (str): Unique identifier for the stored data
  • value (Any): The data to store (will be JSON serialized)
Example:

recall()

Retrieve previously stored data from the 0G network.
Parameters:
  • key (str): The key of the data to retrieve
Returns: Any - The stored data, or None if not found Example:

forget()

Remove data from persistent storage.
Parameters:
  • key (str): The key of the data to remove
Example:

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.
Example:

Conversation Storage

remember_conversation()

Save the current conversation to persistent storage.

recall_conversation()

Load a previously saved conversation.
Example:

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

  1. Key Naming: Use descriptive, hierarchical keys like user_123_preferences
  2. Data Size: Keep individual items under 1MB for optimal performance
  3. Batch Operations: Group related data to minimize storage calls
  4. Error Handling: Always handle potential storage failures gracefully
  5. 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

Next Steps