You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -25,8 +25,93 @@ Initialize PostgreSQL triggers to emit NOTIFY events on data changes. PGCacheWat
25
25
pgcachewatch install <tables-to-cache>
26
26
```
27
27
28
-
### FastAPI Example
29
-
Example showing how to use PGCacheWatch for cache invalidation in a FastAPI app
28
+
## Automating User Data Enrichment with PGCacheWatch and Asyncio
29
+
30
+
In the era of data-driven applications, keeping user information comprehensive and up-to-date is paramount. However, the challenge often lies in efficiently updating user profiles with additional information fetched from external sources, especially in response to new user registrations. This process can significantly benefit from automation, ensuring that every new user's data is enriched without manual intervention.
31
+
32
+
The following Python example leverages `PGCacheWatch` in conjunction with `asyncio` and `asyncpg` to automate the enrichment of new user data in a PostgreSQL database. By listening for new user events, the application fetches additional information asynchronously from simulated external REST APIs and updates the user's record. This seamless integration not only enhances data quality but also optimizes backend workflows by reducing the need for constant database polling.
33
+
34
+
### What This Example Covers
35
+
36
+
-**Listening for New User Registrations**: Utilizing `PGCacheWatch` to listen for new user events in a PostgreSQL database, triggering data enrichment processes.
37
+
-**Fetching Additional Information**: Simulating asynchronous calls to external REST APIs to fetch additional information for newly registered users.
38
+
-**Updating User Profiles**: Demonstrating how to update user records in the database with the fetched information, completing the data enrichment cycle.
39
+
40
+
This guide is intended for developers seeking to automate data enrichment processes in their applications, particularly those using PostgreSQL for data management. The example provides a practical approach to integrating real-time event handling with asynchronous programming for efficient data updates.
Listens for new user events and processes each event as it arrives.
84
+
85
+
This function establishes a connection to the database and listens on a specified
86
+
channel for new user events. When a new user is added (detected via an "insert" operation),
87
+
it triggers the processing of new user events to fetch and update additional information.
88
+
"""
89
+
conn =await asyncpg.connect() # Connect to your PostgreSQL database
90
+
listener = listeners.PGEventQueue()
91
+
await listener.connect(conn)
92
+
93
+
try:
94
+
print("Listening for new user events...")
95
+
asyncfor event in listener.aiter():
96
+
if event.operation =="insert":
97
+
await process_new_user_event()
98
+
finally:
99
+
await conn.close()
100
+
101
+
if__name__=="__main__":
102
+
asyncio.run(listen_for_new_users())
103
+
```
104
+
105
+
## Integrating PGCacheWatch with FastAPI for Dynamic Cache Invalidation
106
+
In modern web applications, maintaining data consistency while ensuring high performance can be a significant challenge. Caching is a common strategy to enhance performance, but it introduces complexity when it comes to invalidating cached data upon updates. `PGCacheWatch` offers a robust solution by leveraging PostgreSQL's NOTIFY/LISTEN features to invalidate cache entries in real-time, ensuring your application's data remains fresh and consistent.
107
+
108
+
This example demonstrates how to integrate `PGCacheWatch` with FastAPI, a popular asynchronous web framework, to create an efficient and responsive web application. By combining FastAPI's scalability with `PGCacheWatch`'s real-time cache invalidation capabilities, developers can build applications that automatically update cached data upon changes in the database, minimizing latency and improving user experience.
109
+
110
+
### What You'll Learn
111
+
112
+
-**Setting Up `PGCacheWatch` with FastAPI**: How to configure `PGCacheWatch` to work within the FastAPI application lifecycle, including database connection setup and teardown.
113
+
-**Implementing Cache Invalidation Strategies**: Utilizing `PGCacheWatch`'s decorators and strategies to invalidate cached data based on database events, specifically focusing on updates.
114
+
-**Creating Responsive Endpoints**: Building FastAPI routes that serve dynamically updated data, ensuring that the information presented to the user is always up-to-date.
30
115
31
116
```python
32
117
import contextlib
@@ -36,33 +121,53 @@ import asyncpg
36
121
from fastapi import FastAPI
37
122
from pgcachewatch import decorators, listeners, models, strategies
38
123
124
+
# Initialize a PGEventQueue listener to listen for database events.
0 commit comments