cache

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: MIT Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-cache

go-cache provides a common cache repository interface backed by existing Go cache packages:

All implementations support Get, Set, Delete, Exists, Clear, TTL, and Touch. Values use JSON encoding by default, and MessagePack is also available.

Installation

go get github.com/vafrcor/go-cache

Usage

go-cache
package main

import (
	"context"
	"errors"
	"fmt"
	"time"

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

type User struct {
	ID   int    `json:"id"`
	Name string `json:"name"`
}

func main() {
	ctx := context.Background()
	repository := cache.NewGoCacheRepository(5*time.Minute, 10*time.Minute, nil)

	if err := repository.Set(ctx, "user:1", User{ID: 1, Name: "John Doe"}, time.Minute); err != nil {
		panic(err)
	}

	var user User
	if err := repository.Get(ctx, "user:1", &user); err != nil {
		if errors.Is(err, cache.ErrCacheNotFound) {
			fmt.Println("cache miss")
			return
		}
		panic(err)
	}

	fmt.Println(user.Name)
}
Freecache

The first argument is the cache size in bytes.

repository := cache.NewFreecacheRepository(100*1024*1024, nil) // 100 MiB

err := repository.Set(ctx, "user:1", user, 5*time.Minute)
Redis
import "github.com/redis/go-redis/v9"

client := redis.NewClient(&redis.Options{
	Addr: "localhost:6379",
})
defer client.Close()

repository := cache.NewGoRedisRepository(client, nil)

err := repository.Set(ctx, "user:1", user, 5*time.Minute)

Custom serialization

Pass a codec.Codec to a constructor to replace the default JSON codec. The package includes a MessagePack implementation:

import cachecodec "github.com/vafrcor/go-cache/codec"

repository := cache.NewGoCacheRepository(
	5*time.Minute,
	10*time.Minute,
	cachecodec.MsgpackCodec{},
)

Logging

Repositories use Go's standard log.Default() logger. When a dedicated output or prefix is needed, set the concrete repository's Logger field:

import (
	"log"
	"os"
)

repository := cache.NewGoCacheRepository(5*time.Minute, 10*time.Minute, nil).(*cache.GoCacheRepository)
repository.Logger = log.New(os.Stderr, "cache: ", log.LstdFlags)

Testing

go test ./...

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCacheNotFound = errors.New("cache: key not found")

Functions

This section is empty.

Types

type FreecacheRepository

type FreecacheRepository struct {
	Logger *log.Logger
	// contains filtered or unexported fields
}

func (*FreecacheRepository) Clear

func (*FreecacheRepository) Delete

func (c *FreecacheRepository) Delete(_ context.Context, key string) error

func (*FreecacheRepository) Exists

func (c *FreecacheRepository) Exists(_ context.Context, key string) bool

func (*FreecacheRepository) Get

func (c *FreecacheRepository) Get(
	_ context.Context,
	key string,
	dst any,
) error

func (*FreecacheRepository) Set

func (c *FreecacheRepository) Set(
	_ context.Context,
	key string,
	value any,
	ttl time.Duration,
) error

func (*FreecacheRepository) TTL

func (*FreecacheRepository) Touch

func (c *FreecacheRepository) Touch(
	_ context.Context,
	key string,
	ttl time.Duration,
) error

type GoCacheRepository

type GoCacheRepository struct {
	Logger *log.Logger
	// contains filtered or unexported fields
}

func (*GoCacheRepository) Clear

func (c *GoCacheRepository) Clear(_ context.Context) error

func (*GoCacheRepository) Delete

func (c *GoCacheRepository) Delete(_ context.Context, key string) error

func (*GoCacheRepository) Exists

func (c *GoCacheRepository) Exists(_ context.Context, key string) bool

func (*GoCacheRepository) Get

func (c *GoCacheRepository) Get(
	_ context.Context,
	key string,
	dst any,
) error

func (*GoCacheRepository) Set

func (c *GoCacheRepository) Set(
	_ context.Context,
	key string,
	value any,
	ttl time.Duration,
) error

func (*GoCacheRepository) TTL

func (*GoCacheRepository) Touch

func (c *GoCacheRepository) Touch(
	_ context.Context,
	key string,
	ttl time.Duration,
) error

type GoRedisRepository

type GoRedisRepository struct {
	RDB *redis.Client

	Logger *log.Logger
	// contains filtered or unexported fields
}

func (*GoRedisRepository) Clear

func (c *GoRedisRepository) Clear(ctx context.Context) error

func (*GoRedisRepository) Delete

func (c *GoRedisRepository) Delete(ctx context.Context, key string) error

func (*GoRedisRepository) Exists

func (c *GoRedisRepository) Exists(ctx context.Context, key string) bool

func (*GoRedisRepository) Get

func (c *GoRedisRepository) Get(
	ctx context.Context,
	key string,
	dst any,
) error

func (*GoRedisRepository) Set

func (c *GoRedisRepository) Set(
	ctx context.Context,
	key string,
	value any,
	ttl time.Duration,
) error

func (*GoRedisRepository) TTL

func (*GoRedisRepository) Touch

func (c *GoRedisRepository) Touch(
	ctx context.Context,
	key string,
	ttl time.Duration,
) error

type ICacheRepository

type ICacheRepository interface {
	Get(ctx context.Context, key string, destination any) error
	Set(ctx context.Context, key string, value any, ttl time.Duration) error
	Delete(ctx context.Context, key string) error
	Exists(ctx context.Context, key string) bool
	Clear(ctx context.Context) error
	TTL(ctx context.Context, key string) (time.Duration, error)
	Touch(ctx context.Context, key string, ttl time.Duration) error
}

func NewFreecacheRepository

func NewFreecacheRepository(size int, codec adapter_codec.Codec) ICacheRepository

func NewGoCacheRepository

func NewGoCacheRepository(
	defaultExpiration time.Duration,
	cleanupInterval time.Duration,
	codec adapter_codec.Codec,
) ICacheRepository

func NewGoRedisRepository

func NewGoRedisRepository(client *redis.Client, codec adapter_codec.Codec) ICacheRepository

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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