Database Integration Example
Demonstrates how to integrate HOT cache with PostgreSQL database for improved performance.
What it does
- Creates a database manager with caching layers
- Implements automatic cache loading from database
- Shows CRUD operations with cache invalidation
- Demonstrates cache-aside pattern with database fallback
Key Features Demonstrated
- Cache-Aside Pattern: Load data from cache, fallback to database
- Automatic Loading: Cache automatically loads missing data from DB
- Cache Invalidation: Update/delete operations invalidate cache
- Prometheus Metrics: Monitor cache performance
- Error Handling: Graceful handling of database errors
Quick Start
# Install PostgreSQL driver
go mod download
# Set up database connection
export DATABASE_URL="postgres://user:password@localhost/dbname?sslmode=disable"
# Run the example
go run postgresql-cache.go
Expected Output
ποΈ PostgreSQL Cache Example
============================
β
Database connected successfully
β
User cache created with 1000 items capacity
β
Product cache created with 2000 items capacity
π Example 1: User Operations
------------------------------
β
Created user: Alice (ID: 1)
β
Retrieved user: Alice (ID: 1) - Cache HIT
β
Updated user: Alice Johnson (ID: 1)
β
Retrieved user: Alice Johnson (ID: 1) - Cache HIT
β
Deleted user: Alice Johnson (ID: 1)
π Example 2: Product Operations
--------------------------------
β
Created product: Laptop (ID: 1)
β
Retrieved product: Laptop (ID: 1) - Cache HIT
β
Retrieved product: Laptop (ID: 1) - Cache HIT
β
Updated product: Gaming Laptop (ID: 1)
β
Deleted product: Gaming Laptop (ID: 1)
π Cache Statistics
--------------------
User Cache: 0/1000 items (0.0% utilization)
Product Cache: 0/2000 items (0.0% utilization)
Database Schema
The example creates these tables:
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
age INTEGER,
created_at TIMESTAMP,
updated_at TIMESTAMP
);
CREATE TABLE products (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
price DECIMAL(10,2),
category VARCHAR(100),
stock INTEGER,
created_at TIMESTAMP,
updated_at TIMESTAMP
);