Preloader
A high-performance asynchronous data preloader library for Rust that provides efficient caching and concurrent access patterns.
Features
- 🚀 Asynchronous Loading: Load data asynchronously using Rust's
Futuretrait - 💾 Smart Caching: Once loaded, data is cached in memory for instant access
- 🔒 Thread Safe: Safe concurrent access across multiple threads
- 📊 State Management: Clear state-based behavior (Idle, Start, Loading, Loaded)
- ⚡ Non-blocking: Optional non-blocking data retrieval with
try_get() - 🔄 Idempotent: Multiple load calls are safely ignored after the first one
Quick Start
Add this to your Cargo.toml:
[]
= "0.1.0"
= { = "1.0", = ["full"] }
Basic Usage
use Preloader;
use tokio;
async
Advanced Usage
use Preloader;
use Arc;
use tokio;
async
API Reference
Preloader<T>
The main preloader struct that handles asynchronous data loading and caching.
Methods
new() -> Preloader<T>- Create a new preloader instanceload(future) -> ()- Start loading data asynchronouslyget() -> Result<&T, PreloaderError>- Get data (blocks until ready)try_get() -> Result<&T, PreloaderError>- Try to get data (non-blocking)
Error Types
Performance Characteristics
- Memory Overhead: Minimal - only stores the loaded data and state
- Concurrency: Excellent - supports unlimited concurrent readers
- Latency: Near-zero for cached data access
- Thread Safety: Full
Send + Syncimplementation
Use Cases
- Configuration Loading: Load app configuration once, access everywhere
- Database Connections: Preload connection pools
- File Caching: Cache frequently accessed files
- API Response Caching: Cache external API responses
- Resource Initialization: Initialize heavy resources on startup
Examples
Configuration Loading
use Preloader;
use ;
async
Database Connection Pool
use Preloader;
use PgPool;
async
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
Development Setup
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- Built with Tokio for async runtime
- Uses thiserror for error handling
- Inspired by modern caching patterns and concurrent programming best practices
Made with ❤️ in Rust