# resilient-python-cache
**Python library for syncing ResilientDB data via WebSocket and HTTP with seamless reconnection.**
[![License](https://img.shields.io/badge/license-Apache%202-blue)](https://www.apache.org/licenses/LICENSE-2.0)
`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.
## Features
- Real-time sync between ResilientDB and MongoDB
- Handles WebSocket and HTTP requests with auto-reconnection
- Configurable synchronization intervals and batch processing
- Provides MongoDB querying for ResilientDB transaction data
## Installation
To use the library, install it via pip:
```bash
pip install resilient-python-cache
```
## Configuration
### MongoDB Configuration
The library uses MongoDB to store ResilientDB data. Configure it by specifying:
- `uri`: MongoDB connection string
- `db_name`: Database name in MongoDB
- `collection_name`: Collection name in MongoDB where ResilientDB data is stored
### ResilientDB Configuration
For 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)
## Usage
### 1. Syncing Data from ResilientDB to MongoDB
Create a sync script to initialize and start the data synchronization from ResilientDB to MongoDB.
```python
# 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:
- Initialize the connection to MongoDB and ResilientDB
- Continuously sync new blocks received from ResilientDB to MongoDB
### API Documentation
#### Class `ResilientPythonCache`
- **constructor(mongo_config: MongoConfig, resilient_db_config: ResilientDBConfig)**:
- Initializes the sync object with MongoDB and ResilientDB configurations.
- **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.
### MongoDB Collection Structure
Each document in the MongoDB collection corresponds to a ResilientDB block, containing:
- **id**: Block identifier
- **createdAt**: Timestamp for block creation
- **transactions**: Array of transactions in the block, with details for each transaction’s inputs, outputs, and asset metadata
### License
This library is licensed under the [Apache License, Version 2.0](https://www.apache.org/licenses/LICENSE-2.0).
---
Enjoy using `resilient-python-cache` to seamlessly synchronize your ResilientDB data to MongoDB with real-time updates and efficient querying!
Raw data
{
"_id": null,
"home_page": "https://github.com/ResilientEcosystem/resilient-python-cache",
"name": "resilient-python-cache",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.8",
"maintainer_email": null,
"keywords": "websocket, mongodb, sync, reconnection, resilientdb",
"author": "Apratim Shukla",
"author_email": "apratimshukla6@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/c4/eb/5078ea833906ad2a8c2ad11e2132e2fb2c20f6f38a8298b2798aa202a2c1/resilient_python_cache-0.1.1.tar.gz",
"platform": null,
"description": "\n# resilient-python-cache\n\n**Python library for syncing ResilientDB data via WebSocket and HTTP with seamless reconnection.**\n\n[![License](https://img.shields.io/badge/license-Apache%202-blue)](https://www.apache.org/licenses/LICENSE-2.0)\n\n`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.\n\n## Features\n\n- Real-time sync between ResilientDB and MongoDB\n- Handles WebSocket and HTTP requests with auto-reconnection\n- Configurable synchronization intervals and batch processing\n- Provides MongoDB querying for ResilientDB transaction data\n\n## Installation\n\nTo use the library, install it via pip:\n\n```bash\npip install resilient-python-cache\n```\n\n## Configuration\n\n### MongoDB Configuration\n\nThe library uses MongoDB to store ResilientDB data. Configure it by specifying:\n\n- `uri`: MongoDB connection string\n- `db_name`: Database name in MongoDB\n- `collection_name`: Collection name in MongoDB where ResilientDB data is stored\n\n### ResilientDB Configuration\n\nFor ResilientDB configuration, specify:\n\n- `base_url`: The base URL for ResilientDB, e.g., `resilientdb://localhost:18000`\n- `http_secure`: Use HTTPS if set to `true`\n- `ws_secure`: Use WSS if set to `true`\n- `reconnect_interval`: Reconnection interval in milliseconds (optional)\n- `fetch_interval`: Fetch interval in milliseconds for periodic syncs (optional)\n\n## Usage\n\n### 1. Syncing Data from ResilientDB to MongoDB\n\nCreate a sync script to initialize and start the data synchronization from ResilientDB to MongoDB.\n\n```python\n# sync.py\n\nimport asyncio\nfrom resilient_python_cache import ResilientPythonCache, MongoConfig, ResilientDBConfig\n\nasync def main():\n mongo_config = MongoConfig(\n uri=\"mongodb://localhost:27017\",\n db_name=\"myDatabase\",\n collection_name=\"myCollection\"\n )\n\n resilient_db_config = ResilientDBConfig(\n base_url=\"resilientdb://crow.resilientdb.com\",\n http_secure=True,\n ws_secure=True\n )\n\n cache = ResilientPythonCache(mongo_config, resilient_db_config)\n\n cache.on(\"connected\", lambda: print(\"WebSocket connected.\"))\n cache.on(\"data\", lambda new_blocks: print(\"Received new blocks:\", new_blocks))\n cache.on(\"error\", lambda error: print(\"Error:\", error))\n cache.on(\"closed\", lambda: print(\"Connection closed.\"))\n\n try:\n await cache.initialize()\n print(\"Synchronization initialized.\")\n\n try:\n await asyncio.Future() # Run indefinitely\n except asyncio.CancelledError:\n pass\n\n except Exception as error:\n print(\"Error during sync initialization:\", error)\n finally:\n await cache.close()\n\nif __name__ == \"__main__\":\n try:\n asyncio.run(main())\n except KeyboardInterrupt:\n print(\"Interrupted by user\")\n```\n\nThis script will:\n- Initialize the connection to MongoDB and ResilientDB\n- Continuously sync new blocks received from ResilientDB to MongoDB\n\n### API Documentation\n\n#### Class `ResilientPythonCache`\n\n- **constructor(mongo_config: MongoConfig, resilient_db_config: ResilientDBConfig)**:\n - Initializes the sync object with MongoDB and ResilientDB configurations.\n\n- **initialize()**: Connects to MongoDB, fetches initial blocks, starts periodic fetching, and opens the WebSocket connection to ResilientDB.\n\n- **close()**: Closes the MongoDB and WebSocket connections, stopping the periodic fetching.\n\n### MongoDB Collection Structure\n\nEach document in the MongoDB collection corresponds to a ResilientDB block, containing:\n- **id**: Block identifier\n- **createdAt**: Timestamp for block creation\n- **transactions**: Array of transactions in the block, with details for each transaction\u2019s inputs, outputs, and asset metadata\n\n### License\n\nThis library is licensed under the [Apache License, Version 2.0](https://www.apache.org/licenses/LICENSE-2.0).\n\n---\n\nEnjoy using `resilient-python-cache` to seamlessly synchronize your ResilientDB data to MongoDB with real-time updates and efficient querying!",
"bugtrack_url": null,
"license": "Apache-2.0",
"summary": "Python library for syncing ResilientDB data via WebSocket and HTTP with seamless reconnection.",
"version": "0.1.1",
"project_urls": {
"Homepage": "https://github.com/ResilientEcosystem/resilient-python-cache",
"Repository": "https://github.com/ResilientEcosystem/resilient-python-cache"
},
"split_keywords": [
"websocket",
" mongodb",
" sync",
" reconnection",
" resilientdb"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "cdcf222da818b23f70e32d0cd9f063d8463480cf4a952ac18483cf821fcb7fbe",
"md5": "0d14762fc0769a7b1e8e8b3fe3a2c77c",
"sha256": "5e43a053e3a2d192a10cc2a5e1578a9422baf55c8d40c781e75763133f2e38db"
},
"downloads": -1,
"filename": "resilient_python_cache-0.1.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "0d14762fc0769a7b1e8e8b3fe3a2c77c",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.8",
"size": 10791,
"upload_time": "2024-11-08T12:08:11",
"upload_time_iso_8601": "2024-11-08T12:08:11.906314Z",
"url": "https://files.pythonhosted.org/packages/cd/cf/222da818b23f70e32d0cd9f063d8463480cf4a952ac18483cf821fcb7fbe/resilient_python_cache-0.1.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "c4eb5078ea833906ad2a8c2ad11e2132e2fb2c20f6f38a8298b2798aa202a2c1",
"md5": "fb34777f9aea9acf1975e5bb42f6cf35",
"sha256": "02396b4f445a8927fdc5248ec845c0e9d722c5d52c7d892b2af8962b7297875f"
},
"downloads": -1,
"filename": "resilient_python_cache-0.1.1.tar.gz",
"has_sig": false,
"md5_digest": "fb34777f9aea9acf1975e5bb42f6cf35",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.8",
"size": 9295,
"upload_time": "2024-11-08T12:08:12",
"upload_time_iso_8601": "2024-11-08T12:08:12.939655Z",
"url": "https://files.pythonhosted.org/packages/c4/eb/5078ea833906ad2a8c2ad11e2132e2fb2c20f6f38a8298b2798aa202a2c1/resilient_python_cache-0.1.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-08 12:08:12",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "ResilientEcosystem",
"github_project": "resilient-python-cache",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "resilient-python-cache"
}