cache

package module
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2026 License: Apache-2.0 Imports: 3 Imported by: 1

README

Cache

Installation

$ go get bitbucket.org/amotus/cache

Usage

Each implementation package contains a New function to obtain a new instance of the implementation.

Unit tests

A mocked implementation of the cache interface is auto-generated and kept updated every release.
Simply use the mocked interface in your unit tests.

Redis

Use the Config struct to configure the Redis wrapper and the New function to instantiate.

Maintainers

Documentation

Index

Constants

This section is empty.

Variables

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

Functions

func GetJSON added in v1.1.40

func GetJSON[T any](c Cache, key string) (value T, err error)

GetJSON tries to get a value from the Cache and unmarshals it into the provided type.

func PutJSON added in v1.1.40

func PutJSON[T any](c Cache, key string, value T, duration time.Duration) error

PutJSON marshals the value and puts it into the Cache.

func RememberJSON added in v1.1.40

func RememberJSON[T any](c Cache, key string, duration time.Duration, getValue func() (T, error)) (T, error)

RememberJSON tries to get a value from the Cache, if not found, it invokes the callback.

Types

type Cache

type Cache interface {
	// Get returns the value associated with the provided key, if any, and a boolean value representing if a value was associated with the key.
	Get(key string) (string, bool, error)

	// Put stores a value and associates it to a key for X seconds after which it expires.
	// If the duration is 0, the value never expires.
	Put(key string, value string, duration time.Duration) error

	// Forget removes the key-value pair.
	Forget(key string) error

	// Increment increments the value of a key by 1.
	Increment(key string) (int64, error)

	// Expire sets the expiration time for a key.
	Expire(key string, duration time.Duration) error

	// IncrementWithInitialExpiry increments the value of a key by 1 and set the expiration time for the key if it does not have one.
	IncrementWithInitialExpiry(key string, duration time.Duration) (int64, error)

	// ForgetAll removes all key-value that match the given pattern
	ForgetAll(keyPattern string) (int, error)

	// Keys get all keys match given pattern
	Keys(keyPattern string) ([]string, error)
}

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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