cache

package
v1.12.8 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2025 License: MIT Imports: 11 Imported by: 0

README

cache

redis and memory cache libraries.

Example of use

Using Redis Cache
package main

import (
	"github.com/redis/go-redis/v9"
	"github.com/go-dev-frame/sponge/pkg/cache"
	"github.com/go-dev-frame/sponge/pkg/encoding"
)

func main() {
	redisClient := redis.NewClient(&redis.Options{
		Addr:     "localhost:6379",
		Password: "",
		DB:       0,
	})

	cachePrefix := ""
	jsonEncoding := encoding.JSONEncoding{}
	newObject := func() interface{} {
		return &User{}
	}

	c := cache.NewRedisCache(redisClient, cachePrefix, jsonEncoding, newObject)
	// operations
	// c.Set(ctx, key, value, expiration)
	// c.Get(ctx, key)
	// c.Delete(ctx, key)
}
Using Memory Cache
package main

import (
	"github.com/go-dev-frame/sponge/pkg/cache"
	"github.com/go-dev-frame/sponge/pkg/encoding"
)

func main() {
	// set memory cache client
	//cache.InitGlobalMemory(
	//	WithNumCounters(1e7),
	//	WithMaxCost(1<<30),
	//	WithBufferItems(64),
	//)

	cachePrefix := ""
	jsonEncoding := encoding.JSONEncoding{}
	newObject := func() interface{} {
		return &User{}
	}

	c := cache.NewMemoryCache(redisClient, cachePrefix, jsonEncoding, newObject)
	// operations
	// c.Set(ctx, key, value, expiration)
	// c.Get(ctx, key)
	// c.Delete(ctx, key)
}

Documentation

Overview

Package cache is memory and redis cache libraries.

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultExpireTime default expiry time
	DefaultExpireTime = time.Hour * 24
	// DefaultNotFoundExpireTime expiry time when result is empty 1 minute,
	// often used for cache time when data is empty (cache pass-through)
	DefaultNotFoundExpireTime = time.Minute * 10

	// NotFoundPlaceholder placeholder
	NotFoundPlaceholder      = "*"
	NotFoundPlaceholderBytes = []byte(NotFoundPlaceholder)
	ErrPlaceholder           = errors.New("cache: placeholder")

	// DefaultClient generate a cache client, where keyPrefix is generally the business prefix
	DefaultClient Cache
)
View Source
var CacheNotFound = redis.Nil

CacheNotFound no hit cache

Functions

func BuildCacheKey

func BuildCacheKey(keyPrefix string, key string) (string, error)

BuildCacheKey construct a cache key with a prefix

func CloseGlobalMemory added in v1.12.8

func CloseGlobalMemory() error

CloseGlobalMemory close memory cache

func Del

func Del(ctx context.Context, keys ...string) error

Del multiple delete data

func Get

func Get(ctx context.Context, key string, val interface{}) error

Get data

func GetGlobalMemoryCli added in v1.12.8

func GetGlobalMemoryCli() *ristretto.Cache

GetGlobalMemoryCli get memory cache client

func InitGlobalMemory added in v1.12.8

func InitGlobalMemory(opts ...Option)

InitGlobalMemory init global memory cache

func InitMemory added in v1.12.8

func InitMemory(opts ...Option) *ristretto.Cache

InitMemory create a memory cache

func MultiGet

func MultiGet(ctx context.Context, keys []string, valueMap interface{}) error

MultiGet multiple get data

func MultiSet

func MultiSet(ctx context.Context, valMap map[string]interface{}, expiration time.Duration) error

MultiSet multiple set data

func Set

func Set(ctx context.Context, key string, val interface{}, expiration time.Duration) error

Set data

func SetCacheWithNotFound

func SetCacheWithNotFound(ctx context.Context, key string) error

SetCacheWithNotFound .

Types

type Cache

type Cache interface {
	Set(ctx context.Context, key string, val interface{}, expiration time.Duration) error
	Get(ctx context.Context, key string, val interface{}) error
	MultiSet(ctx context.Context, valMap map[string]interface{}, expiration time.Duration) error
	MultiGet(ctx context.Context, keys []string, valueMap interface{}) error
	Del(ctx context.Context, keys ...string) error
	SetCacheWithNotFound(ctx context.Context, key string) error
}

Cache driver interface

func NewMemoryCache

func NewMemoryCache(keyPrefix string, encode encoding.Encoding, newObject func() interface{}) Cache

NewMemoryCache create a memory cache

func NewRedisCache

func NewRedisCache(client *redis.Client, keyPrefix string, encode encoding.Encoding, newObject func() interface{}) Cache

NewRedisCache new a cache, client parameter can be passed in for unit testing

func NewRedisClusterCache

func NewRedisClusterCache(client *redis.ClusterClient, keyPrefix string, encode encoding.Encoding, newObject func() interface{}) Cache

NewRedisClusterCache new a cache

type Option added in v1.12.8

type Option func(*options)

Option set the jwt options.

func WithBufferItems added in v1.12.8

func WithBufferItems(bufferItems int64) Option

WithBufferItems set number of keys per Get buffer.

func WithMaxCost added in v1.12.8

func WithMaxCost(maxCost int64) Option

WithMaxCost set maximum cost of cache.

func WithNumCounters added in v1.12.8

func WithNumCounters(numCounters int64) Option

WithNumCounters set number of keys.

Jump to

Keyboard shortcuts

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