cachekit

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2025 License: MIT Imports: 5 Imported by: 1

README

cachekit

Go Report Card Go codecov Known Vulnerabilities License


cachekit is a unified caching interface for Go that supports both in-memory (e.g. Ristretto) and remote (e.g. Redis) backends.
It provides a clean abstraction for setting, getting, and managing cache values using a common API.


✨ Features

  • 🔄 Unified interface for remote/local caches
  • 🔐 Redis support (via go-redis/v9)
  • 🧠 In-memory support (via dgraph-io/ristretto)
  • 🧪 Well-tested with real and mock backends
  • ✅ Graceful error handling and type-safe GetInto()

📦 Installation

go get github.com/DucTran999/cachekit

🚀 Quick Start

Setup redis with docker compose

# run task start redis with docker compose
task testenv
# Also available script with make
make testenv

Example cache with redis. See more in examples

package main

import (
	"log"

	"github.com/DucTran999/cachekit"
)

func main() {
	cfg := cachekit.RedisConfig{
		Host:     "localhost",
		Port:     6379,
		Username: "default",
		Password: "example",
		DB:       1,
	}

	cache, err := cachekit.NewRedisCache(cfg)
	if err != nil {
		log.Fatalf("[FATAL] failed to connect redis: %v", err)
	}

	log.Println("[INFO] redis connected")
	defer cache.Close()
}

📜 License

This project is licensed under the MIT License.

🙌 Contributions

Contributions are welcome! Please open an issue or submit a pull request.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMissingHost = cacheerr.ErrMissingHost
	ErrInvalidPort = cacheerr.ErrInvalidPort
	ErrInvalidDB   = cacheerr.ErrInvalidDB
)

Configuration-related errors

View Source
var (
	ErrKeyNotFound    = cacheerr.ErrKeyNotFound
	ErrDecode         = cacheerr.ErrDecode
	ErrSetNil         = cacheerr.ErrSetNil
	ErrSerializeValue = cacheerr.ErrSerializeValue
)

Operation/runtime errors

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Get retrieves the value associated with the given key as a string.
	Get(ctx context.Context, key string) (string, error)

	// GetInto retrieves the value and unmarshals it into the provided destination.
	GetInto(ctx context.Context, key string, dest any) error

	// Set stores a value with the specified key and TTL (time to live).
	Set(ctx context.Context, key string, value any, ttl time.Duration) error

	// Del deletes one or more keys from the cache.
	Del(ctx context.Context, keys ...string) error

	// Has checks if the given key exists in the cache.
	Has(ctx context.Context, key string) (bool, error)

	// ExistingKeys returns a list of keys that currently exist among the provided ones.
	ExistingKeys(ctx context.Context, keys ...string) ([]string, error)

	// MissingKeys returns the subset of keys that do not exist.
	MissingKeys(ctx context.Context, keys ...string) ([]string, error)

	// Expire updates the TTL of the given key.
	Expire(ctx context.Context, key string, expiration time.Duration) error

	// TTL returns the remaining TTL (in seconds) for the given key.
	TTL(ctx context.Context, key string) (int64, error)

	// FlushAll removes all keys from the cache.
	FlushAll(ctx context.Context) error
}

Cache defines a generic caching interface supporting basic key-value operations with optional TTL, key existence checking, and cache lifecycle management.

type RedisConfig

type RedisConfig = config.RedisConfig

type RemoteCache

type RemoteCache interface {
	Cache

	// Ping checks the health/status of the cache backend.
	Ping(ctx context.Context) error

	// Close releases any resources held by the cache (e.g., closes connections).
	Close() error
}

func NewRedisCache

func NewRedisCache(config RedisConfig) (RemoteCache, error)

Directories

Path Synopsis
examples
redis command
test

Jump to

Keyboard shortcuts

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