database

command module
v0.0.0-...-e6a2e24 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 11, 2026 License: MIT Imports: 8 Imported by: 0

README ΒΆ

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
);

Documentation ΒΆ

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL