cache

package
v1.3.6 Latest Latest
Warning

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

Go to latest
Published: May 5, 2024 License: MIT Imports: 7 Imported by: 0

README

Read this in other languages: English, 中文.

cache

func_cache_test.go

TestRdscCacheFunc

ctx := context.Background()
cacher := mockGetCacher()

err := DeleteCache(ctx, cacher, "UTKey")
if err != nil {
	t.Error(err)
}

value1, err := CacheFunc0(ctx, cacher, "UTKey", 60*time.Second, rawGetFunc0)
if err != nil {
	t.Error(err)
}

value2, err := CacheFunc0(ctx, cacher, "UTKey", 60*time.Second, rawGetFunc0)
if err != nil {
	t.Error(err)
}

if value1 != value2 {
	t.Error(value1, value2)
}

err = DeleteCache(ctx, cacher, fmt.Sprintf("UT:%v", "p1"))
if err != nil {
	t.Error(err)
}

value1, err = CacheFunc1(ctx, cacher, fmt.Sprintf("UT:%v", "p1"), 60*time.Second, rawGetFunc1, "p1")
if err != nil {
	t.Error(err)
}

value2, err = CacheFunc1(ctx, cacher, fmt.Sprintf("UT:%v", "p1"), 60*time.Second, rawGetFunc1, "p1")
if err != nil {
	t.Error(err)
}

if value1 != value2 {
	t.Error(value1, value2)
}

err = DeleteCache(ctx, cacher, fmt.Sprintf("UT:%v:%v", "p1", "p2"))
if err != nil {
	t.Error(err)
}

value1, err = CacheFunc2(ctx, cacher, fmt.Sprintf("UT:%v:%v", "p1", "p2"), 60*time.Second, rawGetFunc2, "p1", "p2")
if err != nil {
	t.Error(err)
}

value2, err = CacheFunc2(ctx, cacher, fmt.Sprintf("UT:%v:%v", "p1", "p2"), 60*time.Second, rawGetFunc2, "p1", "p2")
if err != nil {
	t.Error(err)
}

if value1 != value2 {
	t.Error(value1, value2)
}

param3 := &testCacheParam{Param1: "p3"}
err = DeleteCache(ctx, cacher, fmt.Sprintf("UT:%v:%v:%v", "p1", "p2", param3.Param1))
if err != nil {
	t.Error(err)
}

value1, err = CacheFunc3(ctx, cacher, fmt.Sprintf("UT:%v:%v:%v", "p1", "p2", param3.Param1), 60*time.Second, rawGetFunc3, "p1", "p2", param3)
if err != nil {
	t.Error(err)
}

value2, err = CacheFunc3(ctx, cacher, fmt.Sprintf("UT:%v:%v:%v", "p1", "p2", param3.Param1), 60*time.Second, rawGetFunc3, "p1", "p2", param3)
if err != nil {
	t.Error(err)
}

if value1 != value2 {
	t.Error(value1, value2)
}
TestRdscCacheFuncErr

ctx := context.Background()
cacher := mockGetCacher()

err := DeleteCache(ctx, cacher, "UTKey")
if err != nil {
	t.Error(err)
}

value1, _ := CacheFunc0(ctx, cacher, "UTKey", 60*time.Second, rawGetFunc0Err)
value2, _ := CacheFunc0(ctx, cacher, "UTKey", 60*time.Second, rawGetFunc0Err)

if value1 != value2 {
	t.Error(value1, value2)
}

err = DeleteCache(ctx, cacher, fmt.Sprintf("UT:%v", "error"))
if err != nil {
	t.Error(err)
}

value1, _ = CacheFunc1(ctx, cacher, fmt.Sprintf("UT:%v", "error"), 60*time.Second, rawGetFunc1, "error")
value2, _ = CacheFunc1(ctx, cacher, fmt.Sprintf("UT:%v", "error"), 60*time.Second, rawGetFunc1, "error")

if value1 != value2 {
	t.Error(value1, value2)
}

err = DeleteCache(ctx, cacher, fmt.Sprintf("UT:%v:%v", "error", "p2"))
if err != nil {
	t.Error(err)
}

value1, _ = CacheFunc2(ctx, cacher, fmt.Sprintf("UT:%v:%v", "error", "p2"), 60*time.Second, rawGetFunc2, "error", "p2")
value2, _ = CacheFunc2(ctx, cacher, fmt.Sprintf("UT:%v:%v", "error", "p2"), 60*time.Second, rawGetFunc2, "error", "p2")

if value1 != value2 {
	t.Error(value1, value2)
}

param3 := &testCacheParam{Param1: "p3"}
err = DeleteCache(ctx, cacher, fmt.Sprintf("UT:%v:%v:%v", "error", "p2", param3.Param1))
if err != nil {
	t.Error(err)
}

value1, _ = CacheFunc3(ctx, cacher, fmt.Sprintf("UT:%v:%v:%v", "error", "p2", param3.Param1), 60*time.Second, rawGetFunc3, "error", "p2", param3)
value2, _ = CacheFunc3(ctx, cacher, fmt.Sprintf("UT:%v:%v:%v", "error", "p2", param3.Param1), 60*time.Second, rawGetFunc3, "error", "p2", param3)
if value1 != value2 {
	t.Error(value1, value2)
}
TestRdsDeleteCacheTestMore

ctx := context.Background()
cacher := mockGetCacher()

var err error

err = DeleteCache(ctx, cacher, fmt.Sprintf("GUT:%v:%v", "p1", "p2"))
if err != nil {
	t.Error(err)
}

result1, err := CacheFunc2(ctx, cacher, fmt.Sprintf("GUT:%v:%v", "p1", "p2"), 60*time.Second, rawGetFunc4, "p1", "p2")
if err != nil {
	t.Error(err)
}
log.Info(ctx, "%v %v %v", result1, err, mockPrintKind(result1))

result2, err := CacheFunc2(ctx, cacher, fmt.Sprintf("GUT:%v:%v", "p1", "p2"), 60*time.Second, rawGetFunc4, "p1", "p2")
if err != nil {
	t.Error(err)
}
log.Info(ctx, "%v %v %v", result2, err, mockPrintKind(result2))

if !reflect.DeepEqual(result1, result2) {
	t.Error(result1, result2)
}

err = DeleteCache(ctx, cacher, fmt.Sprintf("GUT:%v:%v", "p1", "p2"))
if err != nil {
	t.Error(err)
}

result3, err := CacheFunc2(ctx, cacher, fmt.Sprintf("GUT:%v:%v", "p1", "p2"), 60*time.Second, rawGetFunc5, "p1", "p2")
if err != nil {
	t.Error(err)
}
log.Info(ctx, "%v %v %v", result3, err, mockPrintKind(result3))

result4, err := CacheFunc2(ctx, cacher, fmt.Sprintf("GUT:%v:%v", "p1", "p2"), 60*time.Second, rawGetFunc5, "p1", "p2")
if err != nil {
	t.Error(err)
}
log.Info(ctx, "%v %v %v", result4, err, mockPrintKind(result4))

if !reflect.DeepEqual(result3, result4) {
	t.Error(result3, result4)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DurPrevCacheThrough = 10 * time.Second //防止缓存穿透
)

Functions

func CacheFunc0 added in v1.3.6

func CacheFunc0[Tr any](ctx context.Context, cacher ICacher, key string, expire time.Duration,
	fn func(context.Context) (Tr, error)) (tr Tr, err error)

func CacheFunc1 added in v1.3.6

func CacheFunc1[T1, Tr any](ctx context.Context, cacher ICacher, key string, expire time.Duration,
	fn func(context.Context, T1) (Tr, error), t1 T1) (tr Tr, err error)

func CacheFunc2 added in v1.3.6

func CacheFunc2[T1, T2, Tr any](ctx context.Context, cacher ICacher, key string, expire time.Duration,
	fn func(context.Context, T1, T2) (Tr, error), t1 T1, t2 T2) (tr Tr, err error)

func CacheFunc3 added in v1.3.6

func CacheFunc3[T1, T2, T3, Tr any](ctx context.Context, cacher ICacher, key string, expire time.Duration,
	fn func(context.Context, T1, T2, T3) (Tr, error), t1 T1, t2 T2, t3 T3) (tr Tr, err error)

func DeleteCache added in v1.3.6

func DeleteCache(ctx context.Context, cacher ICacher, key string) (err error)

Types

type ICacher added in v1.3.6

type ICacher interface {
	Del(context.Context, string) error
	Get(context.Context, string) (string, error)
	Set(context.Context, string, string, time.Duration) error
}

type RdsCacher added in v1.3.6

type RdsCacher struct {
	Rds redis.UniversalClient
}

func (*RdsCacher) Del added in v1.3.6

func (r *RdsCacher) Del(ctx context.Context, key string) error

func (*RdsCacher) Get added in v1.3.6

func (r *RdsCacher) Get(ctx context.Context, key string) (string, error)

func (*RdsCacher) Set added in v1.3.6

func (r *RdsCacher) Set(ctx context.Context, key string, str string, expire time.Duration) error

Jump to

Keyboard shortcuts

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