Example Documentation: Using Distcache with PostgreSQL
Overview:
This example demonstrates how to integrate Distcache, a distributed caching library for Go, with a PostgreSQL database to improve application performance by caching data fetched from the database. The example showcases fetching and caching individual todo items as well as fetching and caching all todo items.
Prerequisites:
- Go installed on your machine.
- PostgreSQL database setup with a
todos
table.
Setup:
-
Clone the GitHub repository containing the example code:
git clone <repository_url>
-
Navigate to the directory containing the example code:
cd dist-cache-example
-
Install dependencies:
go mod download
-
Start the PostgreSQL server and ensure it is running.
-
Run the provided SQL script (
schema.sql
) to create thetodos
table in the PostgreSQL database.psql -U <username> -d <database_name> -a -f schema.sql
Usage:
-
Start the application server by running the provided script:
./run.sh
Alternatively, you can start the server manually by running:
go run main.go
-
Once the server is running, you can use Postman or any other HTTP client to interact with the API endpoints.
Endpoints:
GET /api/todos?key={todo_id}
: Retrieves a todo item by its ID. If the item is not found in the cache, it is fetched from the database and cached.GET /api/todos
: Retrieves all todo items. If all todos are requested for the first time, they are fetched from the database, cached, and returned. Subsequent requests for all todos retrieve them from the cache.POST /api/todos
: Creates a new todo item in the database.
Postman Collection:
A Postman collection (distcache-postgres-example.postman_collection.json
) is provided with pre-configured requests to test the API endpoints. Import the collection into Postman to easily test the endpoints.
GitHub Repository:
The source code for the Distcache:
Conclusion:
By integrating Distcache with a PostgreSQL database, this example demonstrates how to efficiently cache data fetched from the database, thereby reducing database load and improving application performance. Distcache provides a simple yet powerful caching solution for Go applications, offering flexibility and scalability for various caching needs.