Memory Adapter
TheMemoryAdapter provides temporary in-memory storage that implements the StorageAdapter interface. Useful for testing, SSR fallback, or when persistent storage is not available.
Import
Basic Usage
Using in Components
Features
- In-memory storage - Data stored in a JavaScript Map
- Non-persistent - Data lost on page refresh
- SSR-safe - Works in Node.js and browser
- Storage API compatible - Implements same interface as localStorage
- Synchronous - No async operations
- Reactive - Integrates with Vue’s reactivity system
Storage Operations
Set Item
Get Item
Remove Item
Clear All
Check Existence
SSR Usage
TheMemoryAdapter is automatically used as a fallback during SSR:
createStorage:
Testing
Perfect for unit tests:Temporary Storage
Use for session-specific data that shouldn’t persist:TypeScript
Storage Adapter Interface
TheMemoryAdapter implements the StorageAdapter interface:
Internal Implementation
The adapter uses a JavaScriptMap internally:
Limitations
Comparison with Other Adapters
| Feature | MemoryAdapter | localStorage | sessionStorage |
|---|---|---|---|
| Persistence | No | Yes | Session only |
| Capacity | Memory limit | ~5-10MB | ~5-10MB |
| Cross-tab | No | Yes | No |
| SSR support | Yes | No | No |
| Speed | Fastest | Fast | Fast |
| Use case | Testing, SSR | Persistent data | Tab-specific |
Custom Storage Adapter
Create custom adapters by implementing theStorageAdapter interface:
API Reference
Constructor
Properties
| Property | Type | Description |
|---|---|---|
length | number | Number of stored items |
Methods
| Method | Description |
|---|---|
getItem(key) | Get item by key (returns null if not found) |
setItem(key, value) | Store item |
removeItem(key) | Remove item by key |
key(index) | Get key at index |
When to Use
MemoryAdapter
Testing / SSR
Non-persistent, fast, memory-based
localStorage
Persistent
Cross-session, cross-tab
sessionStorage
Session
Tab-specific, cleared on close
See Also
- useStorage - Storage composable
- Storage API - MDN documentation
- localStorage - Persistent storage API