natskv

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2025 License: MIT Imports: 6 Imported by: 0

README

NATS JetStream Key/Value Cache Backend

This package provides a NATS JetStream Key/Value store implementation for httpcache.

Features

  • HTTP response caching using NATS JetStream Key/Value store
  • Automatic TTL management for cache entries
  • Two construction methods:
    • New(): Manages NATS connection internally (recommended)
    • NewWithKeyValue(): For manual connection management
  • Thread-safe operations
  • Configurable bucket settings

Installation

go get github.com/sandrolain/httpcache/natskv

Prerequisites

A running NATS server with JetStream enabled. You can start one using Docker:

docker run -p 4222:4222 nats:latest -js

Or install and run NATS locally:

# Install NATS server
go install github.com/nats-io/nats-server/v2@latest

# Run with JetStream enabled
nats-server -js

Usage

The New() constructor manages the NATS connection internally and is the recommended approach for most use cases:

package main

import (
    "context"
    "time"
    
    "github.com/sandrolain/httpcache"
    "github.com/sandrolain/httpcache/natskv"
)

func main() {
    ctx := context.Background()
    
    // Create cache with automatic connection management
    cache, err := natskv.New(ctx, natskv.Config{
        NATSUrl:     "nats://localhost:4222", // Optional, defaults to nats.DefaultURL
        Bucket:      "http-cache",
        Description: "HTTP response cache",
        TTL:         24 * time.Hour, // Cache entries expire after 24 hours
    })
    if err != nil {
        panic(err)
    }
    defer cache.(interface{ Close() error }).Close()
    
    // Use the cache with httpcache
    transport := httpcache.NewTransport(cache)
    client := transport.Client()
    
    resp, err := client.Get("https://example.com")
    // ...
}
Advanced Usage with NewWithKeyValue()

For cases where you need more control over the NATS connection:

package main

import (
    "context"
    "time"
    
    "github.com/nats-io/nats.go"
    "github.com/nats-io/nats.go/jetstream"
    "github.com/sandrolain/httpcache"
    "github.com/sandrolain/httpcache/natskv"
)

func main() {
    // Connect to NATS manually
    nc, err := nats.Connect(nats.DefaultURL)
    if err != nil {
        panic(err)
    }
    defer nc.Close()
    
    // Create JetStream context
    js, err := jetstream.New(nc)
    if err != nil {
        panic(err)
    }
    
    // Create or update K/V bucket
    ctx := context.Background()
    kv, err := js.CreateOrUpdateKeyValue(ctx, jetstream.KeyValueConfig{
        Bucket:      "http-cache",
        Description: "HTTP response cache",
        TTL:         24 * time.Hour,
    })
    if err != nil {
        panic(err)
    }
    
    // Create cache with existing KeyValue
    cache := natskv.NewWithKeyValue(kv)
    
    // Use the cache
    transport := httpcache.NewTransport(cache)
    client := transport.Client()
    
    resp, err := client.Get("https://example.com")
    // ...
}

Configuration

The Config struct accepts the following fields:

  • NATSUrl (string): URL of the NATS server. Defaults to nats.DefaultURL if empty.
  • Bucket (string): Required. Name of the K/V bucket to use.
  • Description (string): Optional description for the bucket.
  • TTL (time.Duration): Time-to-live for cache entries. Zero means no expiration.
  • NATSOptions ([]nats.Option): Additional NATS connection options.

Integration Tests

To run integration tests, ensure you have a NATS server running with JetStream enabled:

# Start NATS server
docker run -p 4222:4222 nats:latest -js

# Run tests
go test -v ./natskv/...

Performance Considerations

  • NATS JetStream provides high-performance, distributed caching
  • Suitable for microservices architectures
  • Supports clustering for high availability
  • TTL is managed at the bucket level by NATS

License

See the main LICENSE.txt file in the repository root.

Documentation

Overview

Package natskv provides a NATS JetStream Key/Value store interface for http caching.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(ctx context.Context, config Config) (httpcache.Cache, error)

New creates a new Cache with the given configuration. It establishes a connection to NATS, creates a JetStream context, and creates or updates the K/V bucket according to the configuration. The caller should call Close() on the returned cache when done to clean up resources.

func NewWithKeyValue

func NewWithKeyValue(kv jetstream.KeyValue) httpcache.Cache

NewWithKeyValue returns a new Cache with the given NATS JetStream KeyValue store. This constructor is useful when you want to manage the NATS connection yourself. The returned cache will not close the NATS connection when Close() is called.

Types

type Config

type Config struct {
	// NATSUrl is the URL of the NATS server (e.g., "nats://localhost:4222").
	// If empty, defaults to nats.DefaultURL.
	NATSUrl string

	// Bucket is the name of the K/V bucket to use for caching.
	// Required field.
	Bucket string

	// Description is an optional description for the K/V bucket.
	Description string

	// TTL is the time-to-live for cache entries.
	// If zero, entries don't expire (unless deleted by NATS based on other policies).
	TTL time.Duration

	// NATSOptions are additional options to pass to nats.Connect.
	// Optional.
	NATSOptions []nats.Option
}

Config holds the configuration for creating a NATS K/V cache.

Jump to

Keyboard shortcuts

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