diff --git a/.gitignore b/.gitignore index 7bc37af..4fe98f8 100644 --- a/.gitignore +++ b/.gitignore @@ -21,4 +21,4 @@ .idea .envrc .coverage.cov - +.vscode diff --git a/VERSION b/VERSION deleted file mode 100644 index b0f3d96..0000000 --- a/VERSION +++ /dev/null @@ -1 +0,0 @@ -1.0.8 diff --git a/cache/memory/base_cache.go b/cache/memory/base_cache.go index a67987a..e424c1a 100644 --- a/cache/memory/base_cache.go +++ b/cache/memory/base_cache.go @@ -12,6 +12,7 @@ package memory import ( + "context" "fmt" "time" @@ -47,17 +48,17 @@ type EmptyCache struct { } // Exists returns true if the cache has a value for the given key. -func (c *BaseCache) Exists(key cache.Key) bool { +func (c *BaseCache) Exists(ctx context.Context, key cache.Key) bool { k := key.Key() _, ok := c.backend.Get(k) return ok } // Get will get the key from cache, if missing, will call the retrieveFunc to get the data, add to cache, then return -func (c *BaseCache) Get(key cache.Key) (interface{}, error) { +func (c *BaseCache) Get(ctx context.Context, key cache.Key) (interface{}, error) { // 1. if cache is disabled, fetch and return if c.disabled { - value, err := c.retrieveFunc(key) + value, err := c.retrieveFunc(ctx, key) if err != nil { return nil, err } @@ -77,16 +78,16 @@ func (c *BaseCache) Get(key cache.Key) (interface{}, error) { } // 3. if not exists in cache, retrieve it - return c.doRetrieve(key) + return c.doRetrieve(ctx, key) } // doRetrieve will retrieve the real data from database, redis, apis, etc. -func (c *BaseCache) doRetrieve(k cache.Key) (interface{}, error) { +func (c *BaseCache) doRetrieve(ctx context.Context, k cache.Key) (interface{}, error) { key := k.Key() // 3.2 fetch value, err, _ := c.g.Do(key, func() (interface{}, error) { - return c.retrieveFunc(k) + return c.retrieveFunc(ctx, k) }) if err != nil { @@ -102,19 +103,19 @@ func (c *BaseCache) doRetrieve(k cache.Key) (interface{}, error) { } // Set will set key-value into cache. -func (c *BaseCache) Set(key cache.Key, data interface{}) { +func (c *BaseCache) Set(ctx context.Context, key cache.Key, data interface{}) { k := key.Key() c.backend.Set(k, data, 0) } // Delete deletes the value from the cache for the given key. -func (c *BaseCache) Delete(key cache.Key) error { +func (c *BaseCache) Delete(ctx context.Context, key cache.Key) error { k := key.Key() return c.backend.Delete(k) } // DirectGet will get key from cache, without calling the retrieveFunc -func (c *BaseCache) DirectGet(key cache.Key) (interface{}, bool) { +func (c *BaseCache) DirectGet(ctx context.Context, key cache.Key) (interface{}, bool) { k := key.Key() return c.backend.Get(k) } @@ -128,8 +129,8 @@ func (c *BaseCache) Disabled() bool { // GetString returns a string representation of the value for the given key. // will error if the type is not a string. -func (c *BaseCache) GetString(k cache.Key) (string, error) { - value, err := c.Get(k) +func (c *BaseCache) GetString(ctx context.Context, k cache.Key) (string, error) { + value, err := c.Get(ctx, k) if err != nil { return "", err } @@ -143,8 +144,8 @@ func (c *BaseCache) GetString(k cache.Key) (string, error) { // GetBool returns a bool representation of the value for the given key. // will error if the type is not a bool. -func (c *BaseCache) GetBool(k cache.Key) (bool, error) { - value, err := c.Get(k) +func (c *BaseCache) GetBool(ctx context.Context, k cache.Key) (bool, error) { + value, err := c.Get(ctx, k) if err != nil { return false, err } @@ -160,8 +161,8 @@ var defaultZeroTime = time.Time{} // GetTime returns a time representation of the value for the given key. // will error if the type is not an time.Time. -func (c *BaseCache) GetTime(k cache.Key) (time.Time, error) { - value, err := c.Get(k) +func (c *BaseCache) GetTime(ctx context.Context, k cache.Key) (time.Time, error) { + value, err := c.Get(ctx, k) if err != nil { return defaultZeroTime, err } @@ -175,8 +176,8 @@ func (c *BaseCache) GetTime(k cache.Key) (time.Time, error) { // GetInt returns an int representation of the value for the given key. // will error if the type is not an int. -func (c *BaseCache) GetInt(k cache.Key) (int, error) { - value, err := c.Get(k) +func (c *BaseCache) GetInt(ctx context.Context, k cache.Key) (int, error) { + value, err := c.Get(ctx, k) if err != nil { return 0, err } @@ -190,8 +191,8 @@ func (c *BaseCache) GetInt(k cache.Key) (int, error) { // GetInt8 returns an int8 representation of the value for the given key. // will error if the type is not an int8. -func (c *BaseCache) GetInt8(k cache.Key) (int8, error) { - value, err := c.Get(k) +func (c *BaseCache) GetInt8(ctx context.Context, k cache.Key) (int8, error) { + value, err := c.Get(ctx, k) if err != nil { return 0, err } @@ -205,8 +206,8 @@ func (c *BaseCache) GetInt8(k cache.Key) (int8, error) { // GetInt16 returns an int16 representation of the value for the given key. // will error if the type is not an int16. -func (c *BaseCache) GetInt16(k cache.Key) (int16, error) { - value, err := c.Get(k) +func (c *BaseCache) GetInt16(ctx context.Context, k cache.Key) (int16, error) { + value, err := c.Get(ctx, k) if err != nil { return 0, err } @@ -220,8 +221,8 @@ func (c *BaseCache) GetInt16(k cache.Key) (int16, error) { // GetInt32 returns an int32 representation of the value for the given key. // will error if the type is not an int32. -func (c *BaseCache) GetInt32(k cache.Key) (int32, error) { - value, err := c.Get(k) +func (c *BaseCache) GetInt32(ctx context.Context, k cache.Key) (int32, error) { + value, err := c.Get(ctx, k) if err != nil { return 0, err } @@ -235,8 +236,8 @@ func (c *BaseCache) GetInt32(k cache.Key) (int32, error) { // GetInt64 returns an int64 representation of the value for the given key. // will error if the type is not an int64. -func (c *BaseCache) GetInt64(k cache.Key) (int64, error) { - value, err := c.Get(k) +func (c *BaseCache) GetInt64(ctx context.Context, k cache.Key) (int64, error) { + value, err := c.Get(ctx, k) if err != nil { return 0, err } @@ -250,8 +251,8 @@ func (c *BaseCache) GetInt64(k cache.Key) (int64, error) { // GetUint returns an uint representation of the value for the given key. // will error if the type is not an uint. -func (c *BaseCache) GetUint(k cache.Key) (uint, error) { - value, err := c.Get(k) +func (c *BaseCache) GetUint(ctx context.Context, k cache.Key) (uint, error) { + value, err := c.Get(ctx, k) if err != nil { return 0, err } @@ -265,8 +266,8 @@ func (c *BaseCache) GetUint(k cache.Key) (uint, error) { // GetUint8 returns an uint8 representation of the value for the given key. // will error if the type is not an uint8. -func (c *BaseCache) GetUint8(k cache.Key) (uint8, error) { - value, err := c.Get(k) +func (c *BaseCache) GetUint8(ctx context.Context, k cache.Key) (uint8, error) { + value, err := c.Get(ctx, k) if err != nil { return 0, err } @@ -280,8 +281,8 @@ func (c *BaseCache) GetUint8(k cache.Key) (uint8, error) { // GetUint16 returns an uint16 representation of the value for the given key. // will error if the type is not an uint16. -func (c *BaseCache) GetUint16(k cache.Key) (uint16, error) { - value, err := c.Get(k) +func (c *BaseCache) GetUint16(ctx context.Context, k cache.Key) (uint16, error) { + value, err := c.Get(ctx, k) if err != nil { return 0, err } @@ -295,8 +296,8 @@ func (c *BaseCache) GetUint16(k cache.Key) (uint16, error) { // GetUint32 returns an uint32 representation of the value for the given key. // will error if the type is not an uint32. -func (c *BaseCache) GetUint32(k cache.Key) (uint32, error) { - value, err := c.Get(k) +func (c *BaseCache) GetUint32(ctx context.Context, k cache.Key) (uint32, error) { + value, err := c.Get(ctx, k) if err != nil { return 0, err } @@ -310,8 +311,8 @@ func (c *BaseCache) GetUint32(k cache.Key) (uint32, error) { // GetUint64 returns an uint64 representation of the value for the given key. // will error if the type is not an uint64. -func (c *BaseCache) GetUint64(k cache.Key) (uint64, error) { - value, err := c.Get(k) +func (c *BaseCache) GetUint64(ctx context.Context, k cache.Key) (uint64, error) { + value, err := c.Get(ctx, k) if err != nil { return 0, err } @@ -325,8 +326,8 @@ func (c *BaseCache) GetUint64(k cache.Key) (uint64, error) { // GetFloat32 returns a float32 representation of the value for the given key. // will error if the type is not a float32. -func (c *BaseCache) GetFloat32(k cache.Key) (float32, error) { - value, err := c.Get(k) +func (c *BaseCache) GetFloat32(ctx context.Context, k cache.Key) (float32, error) { + value, err := c.Get(ctx, k) if err != nil { return 0, err } @@ -340,8 +341,8 @@ func (c *BaseCache) GetFloat32(k cache.Key) (float32, error) { // GetFloat64 returns a float64 representation of the value for the given key. // will error if the type is not a float64. -func (c *BaseCache) GetFloat64(k cache.Key) (float64, error) { - value, err := c.Get(k) +func (c *BaseCache) GetFloat64(ctx context.Context, k cache.Key) (float64, error) { + value, err := c.Get(ctx, k) if err != nil { return 0, err } diff --git a/cache/memory/base_cache_test.go b/cache/memory/base_cache_test.go index 4c1cf82..840ed73 100644 --- a/cache/memory/base_cache_test.go +++ b/cache/memory/base_cache_test.go @@ -12,6 +12,7 @@ package memory import ( + "context" "errors" "time" @@ -22,7 +23,7 @@ import ( "github.com/TencentBlueKing/gopkg/cache/memory/backend" ) -func retrieveTest(k cache.Key) (interface{}, error) { +func retrieveTest(ctx context.Context, k cache.Key) (interface{}, error) { kStr := k.Key() switch kStr { case "a": @@ -64,18 +65,20 @@ func retrieveTest(k cache.Key) (interface{}, error) { } } -func retrieveError(k cache.Key) (interface{}, error) { +func retrieveError(ctx context.Context, k cache.Key) (interface{}, error) { return nil, errors.New("test error") } var _ = Describe("BaseCache", func() { var c Cache var be *backend.MemoryBackend + var ctx context.Context BeforeEach(func() { expiration := 5 * time.Minute be = backend.NewMemoryBackend("test", expiration, nil) c = NewBaseCache(false, retrieveTest, be) + ctx = context.Background() }) It("Disabled", func() { @@ -84,19 +87,19 @@ var _ = Describe("BaseCache", func() { It("Get", func() { aKey := cache.NewStringKey("a") - x, err := c.Get(aKey) + x, err := c.Get(ctx, aKey) assert.NoError(GinkgoT(), err) assert.Equal(GinkgoT(), "1", x.(string)) - x, err = c.Get(aKey) + x, err = c.Get(ctx, aKey) assert.NoError(GinkgoT(), err) assert.Equal(GinkgoT(), "1", x.(string)) }) It("Set", func() { setKey := cache.NewStringKey("s") - c.Set(setKey, "1") - x, err := c.GetString(setKey) + c.Set(ctx, setKey, "1") + x, err := c.GetString(ctx, setKey) assert.NoError(GinkgoT(), err) assert.Equal(GinkgoT(), "1", x) }) @@ -105,11 +108,11 @@ var _ = Describe("BaseCache", func() { c = NewBaseCache(true, retrieveTest, be) aKey := cache.NewStringKey("a") - x, err := c.Get(aKey) + x, err := c.Get(ctx, aKey) assert.NoError(GinkgoT(), err) assert.Equal(GinkgoT(), "1", x.(string)) - x, err = c.Get(aKey) + x, err = c.Get(ctx, aKey) assert.NoError(GinkgoT(), err) assert.Equal(GinkgoT(), "1", x.(string)) }) @@ -117,256 +120,256 @@ var _ = Describe("BaseCache", func() { It("Exists", func() { aKey := cache.NewStringKey("a") // missing before first Get->Retrieve - assert.False(GinkgoT(), c.Exists(aKey)) + assert.False(GinkgoT(), c.Exists(ctx, aKey)) - _, _ = c.Get(aKey) - assert.True(GinkgoT(), c.Exists(aKey)) + _, _ = c.Get(ctx, aKey) + assert.True(GinkgoT(), c.Exists(ctx, aKey)) }) It("DirectGet", func() { aKey := cache.NewStringKey("a") - _, ok := c.DirectGet(aKey) + _, ok := c.DirectGet(ctx, aKey) assert.False(GinkgoT(), ok) - _, _ = c.Get(aKey) - _, ok = c.DirectGet(aKey) + _, _ = c.Get(ctx, aKey) + _, ok = c.DirectGet(ctx, aKey) assert.True(GinkgoT(), ok) }) It("Delete", func() { aKey := cache.NewStringKey("a") - _, _ = c.Get(aKey) - assert.True(GinkgoT(), c.Exists(aKey)) + _, _ = c.Get(ctx, aKey) + assert.True(GinkgoT(), c.Exists(ctx, aKey)) - _ = c.Delete(aKey) - assert.False(GinkgoT(), c.Exists(aKey)) + _ = c.Delete(ctx, aKey) + assert.False(GinkgoT(), c.Exists(ctx, aKey)) }) It("GetString", func() { aKey := cache.NewStringKey("a") - x, err := c.GetString(aKey) + x, err := c.GetString(ctx, aKey) assert.NoError(GinkgoT(), err) assert.Equal(GinkgoT(), "1", x) int64Key := cache.NewStringKey("int64") - _, err = c.GetString(int64Key) + _, err = c.GetString(ctx, int64Key) assert.Error(GinkgoT(), err) errorKey := cache.NewStringKey("error") - _, err = c.GetString(errorKey) + _, err = c.GetString(ctx, errorKey) assert.Error(GinkgoT(), err) }) It("GetBool", func() { aKey := cache.NewStringKey("bool") - x, err := c.GetBool(aKey) + x, err := c.GetBool(ctx, aKey) assert.NoError(GinkgoT(), err) assert.Equal(GinkgoT(), true, x) int64Key := cache.NewStringKey("int64") - _, err = c.GetBool(int64Key) + _, err = c.GetBool(ctx, int64Key) assert.Error(GinkgoT(), err) errorKey := cache.NewStringKey("error") - _, err = c.GetBool(errorKey) + _, err = c.GetBool(ctx, errorKey) assert.Error(GinkgoT(), err) }) It("GetInt", func() { aKey := cache.NewStringKey("int") - x, err := c.GetInt(aKey) + x, err := c.GetInt(ctx, aKey) assert.NoError(GinkgoT(), err) assert.Equal(GinkgoT(), int(1), x) boolKey := cache.NewStringKey("bool") - _, err = c.GetInt(boolKey) + _, err = c.GetInt(ctx, boolKey) assert.Error(GinkgoT(), err) errorKey := cache.NewStringKey("error") - _, err = c.GetInt(errorKey) + _, err = c.GetInt(ctx, errorKey) assert.Error(GinkgoT(), err) }) It("GetInt8", func() { aKey := cache.NewStringKey("int8") - x, err := c.GetInt8(aKey) + x, err := c.GetInt8(ctx, aKey) assert.NoError(GinkgoT(), err) assert.Equal(GinkgoT(), int8(1), x) boolKey := cache.NewStringKey("bool") - _, err = c.GetInt8(boolKey) + _, err = c.GetInt8(ctx, boolKey) assert.Error(GinkgoT(), err) errorKey := cache.NewStringKey("error") - _, err = c.GetInt8(errorKey) + _, err = c.GetInt8(ctx, errorKey) assert.Error(GinkgoT(), err) }) It("GetInt16", func() { aKey := cache.NewStringKey("int16") - x, err := c.GetInt16(aKey) + x, err := c.GetInt16(ctx, aKey) assert.NoError(GinkgoT(), err) assert.Equal(GinkgoT(), int16(1), x) boolKey := cache.NewStringKey("bool") - _, err = c.GetInt16(boolKey) + _, err = c.GetInt16(ctx, boolKey) assert.Error(GinkgoT(), err) errorKey := cache.NewStringKey("error") - _, err = c.GetInt16(errorKey) + _, err = c.GetInt16(ctx, errorKey) assert.Error(GinkgoT(), err) }) It("GetInt32", func() { aKey := cache.NewStringKey("int32") - x, err := c.GetInt32(aKey) + x, err := c.GetInt32(ctx, aKey) assert.NoError(GinkgoT(), err) assert.Equal(GinkgoT(), int32(1), x) boolKey := cache.NewStringKey("bool") - _, err = c.GetInt32(boolKey) + _, err = c.GetInt32(ctx, boolKey) assert.Error(GinkgoT(), err) errorKey := cache.NewStringKey("error") - _, err = c.GetInt32(errorKey) + _, err = c.GetInt32(ctx, errorKey) assert.Error(GinkgoT(), err) }) It("GetInt64", func() { aKey := cache.NewStringKey("int64") - x, err := c.GetInt64(aKey) + x, err := c.GetInt64(ctx, aKey) assert.NoError(GinkgoT(), err) assert.Equal(GinkgoT(), int64(1), x) strKey := cache.NewStringKey("a") - _, err = c.GetInt64(strKey) + _, err = c.GetInt64(ctx, strKey) assert.Error(GinkgoT(), err) errorKey := cache.NewStringKey("error") - _, err = c.GetInt64(errorKey) + _, err = c.GetInt64(ctx, errorKey) assert.Error(GinkgoT(), err) }) It("GetUint", func() { aKey := cache.NewStringKey("uint") - x, err := c.GetUint(aKey) + x, err := c.GetUint(ctx, aKey) assert.NoError(GinkgoT(), err) assert.Equal(GinkgoT(), uint(1), x) strKey := cache.NewStringKey("a") - _, err = c.GetUint(strKey) + _, err = c.GetUint(ctx, strKey) assert.Error(GinkgoT(), err) errorKey := cache.NewStringKey("error") - _, err = c.GetUint(errorKey) + _, err = c.GetUint(ctx, errorKey) assert.Error(GinkgoT(), err) }) It("GetUint8", func() { aKey := cache.NewStringKey("uint8") - x, err := c.GetUint8(aKey) + x, err := c.GetUint8(ctx, aKey) assert.NoError(GinkgoT(), err) assert.Equal(GinkgoT(), uint8(1), x) strKey := cache.NewStringKey("a") - _, err = c.GetUint8(strKey) + _, err = c.GetUint8(ctx, strKey) assert.Error(GinkgoT(), err) errorKey := cache.NewStringKey("error") - _, err = c.GetUint8(errorKey) + _, err = c.GetUint8(ctx, errorKey) assert.Error(GinkgoT(), err) }) It("GetUint16", func() { aKey := cache.NewStringKey("uint16") - x, err := c.GetUint16(aKey) + x, err := c.GetUint16(ctx, aKey) assert.NoError(GinkgoT(), err) assert.Equal(GinkgoT(), uint16(1), x) strKey := cache.NewStringKey("a") - _, err = c.GetUint16(strKey) + _, err = c.GetUint16(ctx, strKey) assert.Error(GinkgoT(), err) errorKey := cache.NewStringKey("error") - _, err = c.GetUint16(errorKey) + _, err = c.GetUint16(ctx, errorKey) assert.Error(GinkgoT(), err) }) It("GetUint32", func() { aKey := cache.NewStringKey("uint32") - x, err := c.GetUint32(aKey) + x, err := c.GetUint32(ctx, aKey) assert.NoError(GinkgoT(), err) assert.Equal(GinkgoT(), uint32(1), x) strKey := cache.NewStringKey("a") - _, err = c.GetUint32(strKey) + _, err = c.GetUint32(ctx, strKey) assert.Error(GinkgoT(), err) errorKey := cache.NewStringKey("error") - _, err = c.GetUint32(errorKey) + _, err = c.GetUint32(ctx, errorKey) assert.Error(GinkgoT(), err) }) It("GetUint64", func() { aKey := cache.NewStringKey("uint64") - x, err := c.GetUint64(aKey) + x, err := c.GetUint64(ctx, aKey) assert.NoError(GinkgoT(), err) assert.Equal(GinkgoT(), uint64(1), x) strKey := cache.NewStringKey("a") - _, err = c.GetUint64(strKey) + _, err = c.GetUint64(ctx, strKey) assert.Error(GinkgoT(), err) errorKey := cache.NewStringKey("error") - _, err = c.GetUint64(errorKey) + _, err = c.GetUint64(ctx, errorKey) assert.Error(GinkgoT(), err) }) It("GetFloat32", func() { aKey := cache.NewStringKey("float32") - x, err := c.GetFloat32(aKey) + x, err := c.GetFloat32(ctx, aKey) assert.NoError(GinkgoT(), err) assert.Equal(GinkgoT(), float32(1), x) strKey := cache.NewStringKey("a") - _, err = c.GetFloat32(strKey) + _, err = c.GetFloat32(ctx, strKey) assert.Error(GinkgoT(), err) errorKey := cache.NewStringKey("error") - _, err = c.GetFloat32(errorKey) + _, err = c.GetFloat32(ctx, errorKey) assert.Error(GinkgoT(), err) }) It("GetFloat64", func() { aKey := cache.NewStringKey("float64") - x, err := c.GetFloat64(aKey) + x, err := c.GetFloat64(ctx, aKey) assert.NoError(GinkgoT(), err) assert.Equal(GinkgoT(), float64(1), x) strKey := cache.NewStringKey("a") - _, err = c.GetFloat64(strKey) + _, err = c.GetFloat64(ctx, strKey) assert.Error(GinkgoT(), err) errorKey := cache.NewStringKey("error") - _, err = c.GetFloat64(errorKey) + _, err = c.GetFloat64(ctx, errorKey) assert.Error(GinkgoT(), err) }) It("GetTime", func() { aKey := cache.NewStringKey("time") - x, err := c.GetTime(aKey) + x, err := c.GetTime(ctx, aKey) assert.NoError(GinkgoT(), err) assert.Equal(GinkgoT(), time.Time{}, x) int64Key := cache.NewStringKey("int64") - _, err = c.GetTime(int64Key) + _, err = c.GetTime(ctx, int64Key) assert.Error(GinkgoT(), err) errorKey := cache.NewStringKey("error") - _, err = c.GetTime(errorKey) + _, err = c.GetTime(ctx, errorKey) assert.Error(GinkgoT(), err) }) It("GetError", func() { aKey := cache.NewStringKey("error") - x, err := c.Get(aKey) + x, err := c.Get(ctx, aKey) assert.Error(GinkgoT(), err) assert.Nil(GinkgoT(), x) - x, err2 := c.Get(aKey) + x, err2 := c.Get(ctx, aKey) assert.Error(GinkgoT(), err2) assert.Nil(GinkgoT(), x) @@ -378,10 +381,9 @@ var _ = Describe("BaseCache", func() { c = NewBaseCache(true, retrieveError, be) assert.NotNil(GinkgoT(), c) aKey := cache.NewStringKey("a") - _, err := c.Get(aKey) + _, err := c.Get(ctx, aKey) assert.Error(GinkgoT(), err) }) - }) // TODO: mock the backend first Get fail, second Get ok diff --git a/cache/memory/cache_test.go b/cache/memory/cache_test.go index 6131816..00e35e8 100644 --- a/cache/memory/cache_test.go +++ b/cache/memory/cache_test.go @@ -12,6 +12,7 @@ package memory import ( + "context" "time" . "github.com/onsi/ginkgo/v2" @@ -20,7 +21,7 @@ import ( "github.com/TencentBlueKing/gopkg/cache" ) -func retrieveOK(k cache.Key) (interface{}, error) { +func retrieveOK(ctx context.Context, k cache.Key) (interface{}, error) { return "ok", nil } @@ -36,5 +37,4 @@ var _ = Describe("Cache", func() { c := NewMockCache(retrieveOK) assert.NotNil(GinkgoT(), c) }) - }) diff --git a/cache/memory/readme.md b/cache/memory/readme.md index db72700..13fbee5 100644 --- a/cache/memory/readme.md +++ b/cache/memory/readme.md @@ -18,6 +18,7 @@ cache the data in memory, auto to fetch the data if missing. package main import ( + "context" "fmt" "time" @@ -49,7 +50,8 @@ func main() { // 4. use it k := memory.NewStringKey("hello") - data, err := c.Get(k) + var ctx = context.Background() + data, err := c.Get(ctx, k) fmt.Println("err == nil: ", err == nil) fmt.Println("data from cache: ", data) } @@ -62,6 +64,7 @@ func main() { package main import ( + "context" "fmt" "time" @@ -80,7 +83,7 @@ func (k ExampleKey) Key() string { } // 2. impl the reterive func -func RetrieveExample(inKey cache.Key) (interface{}, error) { +func RetrieveExample(ctx context.Context, inKey cache.Key) (interface{}, error) { k := inKey.(ExampleKey) fmt.Println("ExampleKey Field1 and Field2 value:", k.Field1, k.Field2) // data, err := GetFromDatabase(k.Field1, k.Field2) @@ -105,11 +108,12 @@ func main() { Field2: 42, } - data, err := c.Get(k) + var ctx = context.Background() + data, err := c.Get(ctx, k) fmt.Println("err == nil: ", err == nil) fmt.Println("data from cache: ", data) - dataStr, err := c.GetString(k) + dataStr, err := c.GetString(ctx, k) fmt.Println("err == nil: ", err == nil) fmt.Printf("data type is %T, value is %s\n", dataStr, dataStr) } diff --git a/cache/memory/types.go b/cache/memory/types.go index 52f0c05..3113b56 100644 --- a/cache/memory/types.go +++ b/cache/memory/types.go @@ -12,6 +12,7 @@ package memory import ( + "context" "time" "github.com/TencentBlueKing/gopkg/cache" @@ -19,33 +20,33 @@ import ( // RetrieveFunc is the type of the retrieve function. // it retrieves the value from database, redis, apis, etc. -type RetrieveFunc func(key cache.Key) (interface{}, error) +type RetrieveFunc func(ctx context.Context, key cache.Key) (interface{}, error) // Cache is the interface for the cache. type Cache interface { - Get(key cache.Key) (interface{}, error) - Set(key cache.Key, data interface{}) - - GetString(key cache.Key) (string, error) - GetBool(key cache.Key) (bool, error) - GetInt(key cache.Key) (int, error) - GetInt8(key cache.Key) (int8, error) - GetInt16(key cache.Key) (int16, error) - GetInt32(key cache.Key) (int32, error) - GetInt64(key cache.Key) (int64, error) - GetUint(key cache.Key) (uint, error) - GetUint8(key cache.Key) (uint8, error) - GetUint16(key cache.Key) (uint16, error) - GetUint32(key cache.Key) (uint32, error) - GetUint64(key cache.Key) (uint64, error) - GetFloat32(key cache.Key) (float32, error) - GetFloat64(key cache.Key) (float64, error) - GetTime(key cache.Key) (time.Time, error) - - Delete(key cache.Key) error - Exists(key cache.Key) bool - - DirectGet(key cache.Key) (interface{}, bool) + Get(ctx context.Context, key cache.Key) (interface{}, error) + Set(ctx context.Context, key cache.Key, data interface{}) + + GetString(ctx context.Context, key cache.Key) (string, error) + GetBool(ctx context.Context, key cache.Key) (bool, error) + GetInt(ctx context.Context, key cache.Key) (int, error) + GetInt8(ctx context.Context, key cache.Key) (int8, error) + GetInt16(ctx context.Context, key cache.Key) (int16, error) + GetInt32(ctx context.Context, key cache.Key) (int32, error) + GetInt64(ctx context.Context, key cache.Key) (int64, error) + GetUint(ctx context.Context, key cache.Key) (uint, error) + GetUint8(ctx context.Context, key cache.Key) (uint8, error) + GetUint16(ctx context.Context, key cache.Key) (uint16, error) + GetUint32(ctx context.Context, key cache.Key) (uint32, error) + GetUint64(ctx context.Context, key cache.Key) (uint64, error) + GetFloat32(ctx context.Context, key cache.Key) (float32, error) + GetFloat64(ctx context.Context, key cache.Key) (float64, error) + GetTime(ctx context.Context, key cache.Key) (time.Time, error) + + Delete(ctx context.Context, key cache.Key) error + Exists(ctx context.Context, key cache.Key) bool + + DirectGet(ctx context.Context, key cache.Key) (interface{}, bool) Disabled() bool } diff --git a/release.md b/release.md deleted file mode 100644 index c8b075c..0000000 --- a/release.md +++ /dev/null @@ -1,39 +0,0 @@ -版本日志 -================ -## v1.0.8 - -- add: logger interface -- add: cryptography - -## v1.0.7 - -- refactor: move cache/memory/key.go to cache/key.go, as a common type of cache module - -## v1.0.6 - -- add: cache/memory add interface `Set(key Key, data interface{})` - -## v1.0.5 - -- bugfix: cache/memory package name wrong - -## v1.0.4 - -- add pkg: cache/memory -- add pkg: conv - -## v1.0.3 - -- add pkg: stringx - -## v1.0.2 - -- add pkg: collection/set - -## v1.0.1 - -- add pkg: errorx - -## v1.0.0 - -- init the project dir