Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions backend/gopkgs/cachex/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/jellydator/ttlcache/v3"
"time"

"github.com/cloudzenith/DouTok/backend/gopkgs/gofer"
Expand All @@ -29,6 +29,7 @@ type Cache[T any] struct {
localTTL *time.Duration
redisTTL *time.Duration
client *redis.Client
localCache *ttlcache.Cache[string, []byte]
}

func NewCache[T any](options ...CacheOption[T]) *Cache[T] {
Expand All @@ -50,6 +51,10 @@ func NewCache[T any](options ...CacheOption[T]) *Cache[T] {
cache.redisTTL = &DefaultTTL
}

if cache.useLocal {
cache.localCache = ttlcache.New[string, []byte]()
}

return cache
}

Expand Down Expand Up @@ -79,17 +84,18 @@ func (c *Cache[T]) fetchSet(ctx context.Context, key string, fetch func(ctx cont
ttl = c.localTTL
}

// TODO: set local cache
fmt.Println("when use local will todo something", ttl)
c.localCache.Set(key, val, *ttl)
}

return value, nil
}

func (c *Cache[T]) getCache(ctx context.Context, key string) ([]byte, error) {
if c.useLocal {
// TODO: 读本地缓存并返回
fmt.Println("when use local will todo something")
item := c.localCache.Get(key)
if item != nil {
return item.Value(), nil
}
}

result, err := c.client.Get(ctx, key).Result()
Expand All @@ -109,7 +115,7 @@ func (c *Cache[T]) Fetch(
key string,
fetch func(ctx context.Context) (T, error),
) (t T, err error) {
value, err, _ := gofer.SingleFlightDo(key, func() (interface{}, error) {
value, err, _ := gofer.SingleFlightDo(key, func() (any, error) {
if c.useFallback {
val, err := c.fetchSet(ctx, key, fetch)
if err == nil {
Expand Down
26 changes: 26 additions & 0 deletions backend/gopkgs/errorx/errorx_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package errorx

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestNew(t *testing.T) {
err := New(1, "some error")
assert.NotNil(t, err)
assert.Equal(t, int32(1), err.Code)
assert.Equal(t, "some error", err.Error())
}

func TestNewWithCode(t *testing.T) {
err := NewWithCode(1)
assert.NotNil(t, err)
assert.Equal(t, int32(1), err.Code)
assert.Equal(t, "unknown error", err.Error())

RegisterErrors(1, "some error")
err = NewWithCode(1)
assert.NotNil(t, err)
assert.Equal(t, int32(1), err.Code)
assert.Equal(t, "some error", err.Error())
}
7 changes: 5 additions & 2 deletions backend/gopkgs/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ require (
github.com/go-kratos/kratos/v2 v2.8.0
github.com/google/uuid v1.6.0
github.com/hashicorp/consul/api v1.29.2
github.com/jellydator/ttlcache/v3 v3.3.0
github.com/minio/minio-go/v7 v7.0.75
github.com/panjf2000/ants/v2 v2.10.0
github.com/redis/go-redis/v9 v9.6.1
github.com/samber/lo v1.46.0
github.com/stretchr/testify v1.9.0
go.etcd.io/etcd/client/v3 v3.5.15
go.uber.org/zap v1.27.0
golang.org/x/sync v0.8.0
Expand All @@ -34,12 +36,11 @@ require (
github.com/armon/go-metrics v0.4.1 // indirect
github.com/bytedance/sonic/loader v0.2.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/coreos/go-semver v0.3.1 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
Expand All @@ -51,6 +52,7 @@ require (
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-playground/assert/v2 v2.2.0 // indirect
github.com/go-playground/form/v4 v4.2.0 // indirect
github.com/go-redis/redismock/v9 v9.2.0 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
Expand Down Expand Up @@ -79,6 +81,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rs/xid v1.5.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/tidwall/gjson v1.17.3 // indirect
Expand Down
6 changes: 6 additions & 0 deletions backend/gopkgs/gofer/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package gofer

var (
ErrSubmittedTaskNil = "submitted task can't be nil"
ErrGroupHashBeenStarted = "can't submit task to a error group after Wait()"
)
4 changes: 4 additions & 0 deletions backend/gopkgs/gofer/gofer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ func SetUseGlobalPool(value bool) {

func Go(f func()) {
if useGlobalPool {
if pool == nil {
InitGlobalPool()
}

_ = pool.Submit(f)
return
}
Expand Down
59 changes: 59 additions & 0 deletions backend/gopkgs/gofer/gofer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package gofer

import (
"context"
"github.com/stretchr/testify/assert"
"sync"
"testing"
)

func TestSetUseGlobalPool(t *testing.T) {
assert.Equal(t, false, useGlobalPool)

SetUseGlobalPool(true)
assert.Equal(t, true, useGlobalPool)
}

func submitGo() bool {
var wg sync.WaitGroup
wg.Add(1)
var flag bool
Go(func() {
flag = true
wg.Done()
})

wg.Wait()
return flag
}

func TestGo(t *testing.T) {
assert.Equal(t, true, submitGo())

InitGlobalPool()
SetUseGlobalPool(true)
assert.Equal(t, true, submitGo())
}

func submitGoWithCtx() bool {
var wg sync.WaitGroup
wg.Add(1)

var flag bool
ctx := context.Background()
GoWithCtx(ctx, func(ctx context.Context) {
flag = true
wg.Done()
})

wg.Wait()
return flag
}

func TestGoWithCtx(t *testing.T) {
assert.Equal(t, true, submitGoWithCtx())

InitGlobalPool()
SetUseGlobalPool(true)
assert.Equal(t, true, submitGoWithCtx())
}