mongodb

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package mongodb provides a MongoDB 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 MongoDB and creates the necessary indexes. The caller should call Close() on the returned cache when done to clean up resources.

func NewWithClient

func NewWithClient(client *mongo.Client, database, collection string, config Config) (httpcache.Cache, error)

NewWithClient returns a new Cache with the given MongoDB client. This constructor is useful when you want to manage the MongoDB connection yourself. The returned cache will not close the MongoDB client when Close() is called.

Types

type Config

type Config struct {
	// URI is the MongoDB connection URI (e.g., "mongodb://localhost:27017").
	// Required field.
	URI string

	// Database is the name of the database to use for caching.
	// Required field.
	Database string

	// Collection is the name of the collection to use for caching.
	// Optional - defaults to "httpcache".
	Collection string

	// KeyPrefix is a prefix to add to all cache keys.
	// Optional - defaults to "cache:".
	KeyPrefix string

	// Timeout is the timeout for database operations.
	// Optional - defaults to 5 seconds.
	Timeout time.Duration

	// TTL is the time-to-live for cache entries.
	// Optional - if set, creates a TTL index on the createdAt field.
	TTL time.Duration

	// ClientOptions are additional options to pass to mongo.Connect.
	// Optional.
	ClientOptions *options.ClientOptions
}

Config holds the configuration for creating a MongoDB cache.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a Config with sensible defaults.

Jump to

Keyboard shortcuts

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