resilient-python-cache
is a library for establishing and managing real-time synchronization between ResilientDB and MongoDB using WebSocket and HTTP. It includes automatic reconnection, batching, and concurrency management for high-performance syncing.
To use the library, install it via pip:
pip install resilient-python-cache
The library uses MongoDB to store ResilientDB data. Configure it by specifying:
uri
: MongoDB connection stringdb_name
: Database name in MongoDBcollection_name
: Collection name in MongoDB where ResilientDB data is storedFor ResilientDB configuration, specify:
base_url
: The base URL for ResilientDB, e.g., resilientdb://localhost:18000
http_secure
: Use HTTPS if set to true
ws_secure
: Use WSS if set to true
reconnect_interval
: Reconnection interval in milliseconds (optional)fetch_interval
: Fetch interval in milliseconds for periodic syncs (optional)Create a sync script to initialize and start the data synchronization from ResilientDB to MongoDB.
# sync.py import asyncio from resilient_python_cache import ResilientPythonCache, MongoConfig, ResilientDBConfig async def main(): mongo_config = MongoConfig( uri="mongodb://localhost:27017", db_name="myDatabase", collection_name="myCollection" ) resilient_db_config = ResilientDBConfig( base_url="resilientdb://crow.resilientdb.com", http_secure=True, ws_secure=True ) cache = ResilientPythonCache(mongo_config, resilient_db_config) cache.on("connected", lambda: print("WebSocket connected.")) cache.on("data", lambda new_blocks: print("Received new blocks:", new_blocks)) cache.on("error", lambda error: print("Error:", error)) cache.on("closed", lambda: print("Connection closed.")) try: await cache.initialize() print("Synchronization initialized.") try: await asyncio.Future() # Run indefinitely except asyncio.CancelledError: pass except Exception as error: print("Error during sync initialization:", error) finally: await cache.close() if __name__ == "__main__": try: asyncio.run(main()) except KeyboardInterrupt: print("Interrupted by user")
This script will:
ResilientPythonCache
constructor(mongo_config: MongoConfig, resilient_db_config: ResilientDBConfig):
initialize(): Connects to MongoDB, fetches initial blocks, starts periodic fetching, and opens the WebSocket connection to ResilientDB.
close(): Closes the MongoDB and WebSocket connections, stopping the periodic fetching.
Each document in the MongoDB collection corresponds to a ResilientDB block, containing:
This library is licensed under the Apache License, Version 2.0.
Enjoy using resilient-python-cache
to seamlessly synchronize your ResilientDB data to MongoDB with real-time updates and efficient querying!