Categorygithub.com/SebastiaanPasterkamp/go-cache
modulepackage
1.0.0
Repository: https://github.com/sebastiaanpasterkamp/go-cache.git
Documentation: pkg.go.dev

# README

go-cache

A uniform cache repository interface for multiple implementations. Supports in-memory (internal), and redis (external).

Usage

import (
	"context"
	"time"

	"github.com/SebastiaanPasterkamp/go-cache"
)

func example() {
	ctx := context.Background()

	// This configuration is for demonstration purpose only. Only one of the
	// settings will be used to initialize the cache where in-memory takes
	// the priority.
	test := cache.Configuration{
		// Demonstrate the in-memory cache implementation, which should work
		// as an example
		InMemorySettings: &cache.InMemorySettings{},
		// For real-world use-cases use the Redis settings instead
		RedisSettings: &cache.RedisSettings{
			Address:  "redis-host:6379",
			Password: "S3(r37!", // Please configure this using an environment variable
			Database: 0,
		},
	}

	c, err := test.Factory(ctx)
	if err != nil {
		log.Fatalf("Failed to initialize cache: %v", err)
	}

	err = c.Set(ctx, "key", "value", 5*time.Second)
	if err != nil {
		log.Fatalf("Failed to store cache item: %v", err)
	}

	value := ""
	err = c.Get(ctx, "key", &value)
	if err != nil {
		log.Fatalf("Failed to retrieve cache item: %v", err)
	}

	err = c.Del(ctx, "key")
	if err != nil {
		log.Fatalf("Failed to delete cache item: %v", err)
	}

	log.Printf("Value retrieved: %q\n", value)
}

# Packages

No description provided by the author

# Variables

ErrBadConfig is the error returned if the factory cannot instantiate a cache repository based on the provided configuration.
ErrMissingConfig is the error returned if the Configuration does not contain settings for a supported cache.Repository type.
ErrNotFound is the error returned if the requested item does not exist in the cache repository.
ErrNotRecovered is the error returned if the stored item cannot be retrieved or the retrieved item is malformed.
ErrNotStored is the error returned if the stored item cannot be stored in the repository.

# Structs

Configuration offers settings for all cache.Repository implementations.
InMemorySettings contains all settings for the in-memory cache implementation.
Memory is an in-memory implementation of the cache.Repository.
Redis is an redis based implementation of the cache.Repository.
RedisSettings contains all settings for the redis based cache implementation.

# Interfaces

Repository is a simple interface to any cache offering Set, Get, and Del operations.