diff --git a/bench/decorator-bench_test.go b/bench/decorator-bench_test.go index 48937f7..2a4a547 100644 --- a/bench/decorator-bench_test.go +++ b/bench/decorator-bench_test.go @@ -36,6 +36,7 @@ var ( ) func benchmark(b *testing.B, f func(int) UserInfo) { + b.Helper() for i := 0; i < b.N; i++ { f(50) } diff --git a/examples/decorator-err_test.go b/examples/decorator-err_test.go index 2d0f945..684fa65 100644 --- a/examples/decorator-err_test.go +++ b/examples/decorator-err_test.go @@ -55,7 +55,7 @@ func TestNeedCacheErrWithTTL(t *testing.T) { if age <= 0 { return UserInfo{}, errors.New("invalid age") } - return UserInfo{Name: "Anonymous", Age: 9}, nil + return UserInfo{Name: "Anonymous", Age: age}, nil } // 1. Cacheable Function getUserAndErrCached := gofnext.CacheFn1Err(getUserAndErr, &gofnext.Config{ diff --git a/examples/decorator-key_test.go b/examples/decorator-key_test.go index 34ac33f..dd23547 100644 --- a/examples/decorator-key_test.go +++ b/examples/decorator-key_test.go @@ -43,14 +43,14 @@ func TestCacheFuncKeyStruct(t *testing.T) { } } func TestCacheFuncKeyStructUnexportedKey(t *testing.T) { - type info struct{ - name string // unexported field - } + type info struct { + name string // unexported field + } type UserInfo struct { - info struct{ - name string // unexported field - } + info struct { + name string // unexported field + } } // Original function getUserName := func(user UserInfo, flag *string) string { @@ -58,10 +58,10 @@ func TestCacheFuncKeyStructUnexportedKey(t *testing.T) { } // Cacheable Function - getUserName2 := gofnext.CacheFn2(getUserName, ) + getUserName2 := gofnext.CacheFn2(getUserName) // Execute the function - flag := "flag" + flag := "flag" name := getUserName2(UserInfo{info{name: "Alex"}}, &flag) if name != "Alex" { t.Errorf("name should be 'Alex', but get %s", name) diff --git a/examples/decorator_test.go b/examples/decorator_test.go index 0e01a87..2b60e5c 100644 --- a/examples/decorator_test.go +++ b/examples/decorator_test.go @@ -99,6 +99,38 @@ func TestCacheFuncWith3Params(t *testing.T) { } } +func TestCacheCtxFuncWith3Params(t *testing.T) { + // Original function + executeCount := 0 + sum := func(ctx context.Context, b, c int) int { + executeCount++ + time.Sleep(10 * time.Millisecond) + return b + c + } + + // Cacheable Function + sumCache := gofnext.CacheFn3(sum, &gofnext.Config{ + TTL: time.Hour, + }) // accept 3 parameter + + // Execute the function multi times in parallel. + parallelCall(func() { + ctx := context.Background() + score := sumCache(ctx, 3, 5) + if score != 8 { + t.Errorf("score should be 99, but get %d", score) + } + ctx = context.Background() + sumCache(ctx, 3, 5) + ctx = context.Background() + sumCache(ctx, 3, 6) + }, 5) + + if executeCount != 2 { + t.Errorf("executeCount should be 2, but get %d", executeCount) + } +} + // Cache Function with more parameter(>3) func TestCacheFuncWithMoreParams(t *testing.T) { executeCount := 0 @@ -110,8 +142,8 @@ func TestCacheFuncWithMoreParams(t *testing.T) { } // Original function - fn := func(name string, age, gender, height int) int { - _ = age+ gender+ height + getUserScoreOrigin := func(name string, age, gender, height int) int { + _ = age + gender + height executeCount++ // select score from db where name=name and age=age and gender=gender switch name { @@ -124,23 +156,23 @@ func TestCacheFuncWithMoreParams(t *testing.T) { // Convert to extra parameters to a 1 parameter(2 or 3 prameters) fnWrap := func(arg Stu) int { - return fn(arg.name, arg.age, arg.gender, arg.height) + return getUserScoreOrigin(arg.name, arg.age, arg.gender, arg.height) } // Cacheable Function fnCachedInner := gofnext.CacheFn1(fnWrap) - fnCached := func(name string, age, gender, height int) int { + getUserScore := func(name string, age, gender, height int) int { return fnCachedInner(Stu{name, age, gender, height}) } // Execute the function multi times in parallel. parallelCall(func() { - score := fnCached("Alex", 20, 1, 160) + score := getUserScore("Alex", 20, 1, 160) if score != 10 { t.Errorf("score should be 10, but get %d", score) } - fnCached("Jhon", 21, 0, 160) - fnCached("Alex", 20, 1, 160) + getUserScore("Jhon", 21, 0, 160) + getUserScore("Alex", 20, 1, 160) }, 10) // Test count @@ -148,35 +180,3 @@ func TestCacheFuncWithMoreParams(t *testing.T) { t.Errorf("executeCount should be 2, but get %d", executeCount) } } - -func TestCacheCtxFuncWith3Params(t *testing.T) { - // Original function - executeCount := 0 - sum := func(ctx context.Context, b, c int) int { - executeCount++ - time.Sleep(10 * time.Millisecond) - return b + c - } - - // Cacheable Function - sumCache := gofnext.CacheFn3(sum, &gofnext.Config{ - TTL: time.Hour, - }) // accept 3 parameter - - // Execute the function multi times in parallel. - parallelCall(func() { - ctx := context.Background() - score := sumCache(ctx, 3, 5) - if score != 8 { - t.Errorf("score should be 99, but get %d", score) - } - ctx = context.Background() - sumCache(ctx, 3, 5) - ctx = context.Background() - sumCache(ctx, 3, 6) - }, 5) - - if executeCount != 2 { - t.Errorf("executeCount should be 2, but get %d", executeCount) - } -} diff --git a/funcs.go b/funcs.go new file mode 100644 index 0000000..7af6de7 --- /dev/null +++ b/funcs.go @@ -0,0 +1,8 @@ +package gofnext + +func If[T any](condition bool, trueVal, falseVal T) T { + if condition { + return trueVal + } + return falseVal +} diff --git a/version b/version index cce2b1f..70be42f 100644 --- a/version +++ b/version @@ -1 +1 @@ -v0.0.22 \ No newline at end of file +v0.0.23 \ No newline at end of file