Storage Adapters
RunCache includes built-in storage adapters that allow you to persist cache data across application restarts. This page provides a complete reference for the available storage adapters and how to use them.
Overview
Storage adapters implement a common interface to save and load cache data to/from persistent storage. RunCache includes three built-in adapters:
LocalStorageAdapter: For browser environments, uses the browser's localStorage API
IndexedDBAdapter: For browser environments, uses the browser's IndexedDB API for larger datasets
FilesystemAdapter: For Node.js environments, stores cache data in the filesystem
Common Configuration Options
All storage adapters accept the following common options:
LocalStorageAdapter
The LocalStorageAdapter
uses the browser's localStorage API to persist cache data. This adapter is suitable for small to medium-sized cache data in web applications.
Import
Constructor
Parameters
config?
:StorageAdapterConfig
- Optional configuration optionsstorageKey?
:string
- Key to use in localStorage (default: "run-cache-data")autoSaveInterval?
:number
- Auto-save interval in milliseconds (default: 0)autoLoadOnInit?
:boolean
- Whether to load cache automatically when adapter is initialized (default: true)
Example Usage
Limitations
Limited to approximately 5-10MB of data (varies by browser)
Synchronous API that can block the main thread with large data
Only supports string data
IndexedDBAdapter
The IndexedDBAdapter
uses the browser's IndexedDB API to persist cache data. This adapter is suitable for larger datasets in web applications.
Import
Constructor
Parameters
config?
:StorageAdapterConfig
- Optional configuration optionsstorageKey?
:string
- Database and store name (default: "run-cache-data")autoSaveInterval?
:number
- Auto-save interval in milliseconds (default: 0)autoLoadOnInit?
:boolean
- Whether to load cache automatically when adapter is initialized (default: true)
Example Usage
Advantages
Supports much larger data sizes (typically 50-100MB or more)
Asynchronous API that doesn't block the main thread
Better performance with large datasets
Limitations
More complex API
Not available in all browser contexts (e.g., some private browsing modes)
FilesystemAdapter
The FilesystemAdapter
stores cache data in the filesystem. This adapter is suitable for Node.js applications.
Import
Constructor
Parameters
config?
:FilesystemAdapterConfig
- Optional configuration optionsstorageKey?
:string
- Filename to use (default: "run-cache-data")autoSaveInterval?
:number
- Auto-save interval in milliseconds (default: 0)autoLoadOnInit?
:boolean
- Whether to load cache automatically when adapter is initialized (default: true)filePath?
:string
- Custom file path (default: current working directory)
Example Usage
Advantages
Supports very large data sizes (limited only by disk space)
Persists across application restarts
Can be configured to store in specific locations
Limitations
Only available in Node.js environments
Requires appropriate file system permissions
May have performance implications with very frequent writes
Custom Storage Adapters
You can create your own storage adapter by implementing the StorageAdapter
interface:
Example Custom Adapter
Here's an example of a custom adapter that uses a RESTful API for storage:
Working with Storage Adapters
Manual Save and Load
You can manually trigger save and load operations:
Auto-Save Configuration
Configure automatic saving at regular intervals:
Storage Cleanup
To clear persistent storage:
Best Practices
1. Choose the Right Adapter
Use
LocalStorageAdapter
for simple web applications with small cache dataUse
IndexedDBAdapter
for web applications with larger cache dataUse
FilesystemAdapter
for Node.js applications
2. Handle Storage Errors
Always handle potential storage errors:
3. Manage Storage Size
Be mindful of storage limits, especially in browsers:
4. Use Appropriate Save Intervals
Balance between data freshness and performance:
5. Consider Data Serialization
Be aware that all data is serialized to strings:
Next Steps
RunCache API - Learn about the main RunCache API
Events - Understand the event system
Types - Explore TypeScript type definitions
Persistent Storage - Learn more about persistence strategies
Last updated