Socket
Book a DemoInstallSign in
Socket

github.com/SebastiaanPasterkamp/go-cache

Package Overview
Dependencies
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/SebastiaanPasterkamp/go-cache

Source
Go
Version
v1.0.0
Version published
Created
Source

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

FAQs

Package last updated on 03 Jul 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts