From b316b9c0736ed6e08ec132d9d57ca8087e2120a2 Mon Sep 17 00:00:00 2001 From: John Date: Fri, 27 Aug 2021 01:01:41 +0800 Subject: [PATCH] example updates for package gcache --- .example/os/gcache/getorset_func_lock.go | 28 +++++++++++++++++++++ .example/os/gcache/note_interface_key.go | 19 ++++++++++++++ .example/os/gcache/note_interface_value.go | 29 ++++++++++++++++++++++ os/gcache/gcache_cache_adapter.go | 6 ++--- 4 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 .example/os/gcache/getorset_func_lock.go create mode 100644 .example/os/gcache/note_interface_key.go create mode 100644 .example/os/gcache/note_interface_value.go diff --git a/.example/os/gcache/getorset_func_lock.go b/.example/os/gcache/getorset_func_lock.go new file mode 100644 index 000000000..b5e3760bc --- /dev/null +++ b/.example/os/gcache/getorset_func_lock.go @@ -0,0 +1,28 @@ +package main + +import ( + "fmt" + "github.com/gogf/gf/os/gcache" + "github.com/gogf/gf/os/gctx" + "time" +) + +func main() { + var ( + ch = make(chan struct{}, 0) + ctx = gctx.New() + key = `key` + value = `value` + ) + for i := 0; i < 10; i++ { + go func(index int) { + <-ch + _, _ = gcache.Ctx(ctx).GetOrSetFuncLock(key, func() (interface{}, error) { + fmt.Println(index, "entered") + return value, nil + }, 0) + }(i) + } + close(ch) + time.Sleep(time.Second) +} diff --git a/.example/os/gcache/note_interface_key.go b/.example/os/gcache/note_interface_key.go new file mode 100644 index 000000000..4f5e1ee44 --- /dev/null +++ b/.example/os/gcache/note_interface_key.go @@ -0,0 +1,19 @@ +package main + +import ( + "fmt" + "github.com/gogf/gf/os/gcache" + "github.com/gogf/gf/os/gctx" +) + +func main() { + var ( + ctx = gctx.New() + key1 int32 = 1 + key2 float64 = 1 + value = `value` + ) + _ = gcache.Ctx(ctx).Set(key1, value, 0) + fmt.Println(gcache.Ctx(ctx).Get(key1)) + fmt.Println(gcache.Ctx(ctx).Get(key2)) +} diff --git a/.example/os/gcache/note_interface_value.go b/.example/os/gcache/note_interface_value.go new file mode 100644 index 000000000..b102e86f0 --- /dev/null +++ b/.example/os/gcache/note_interface_value.go @@ -0,0 +1,29 @@ +package main + +import ( + "fmt" + "github.com/gogf/gf/os/gcache" + "github.com/gogf/gf/os/gctx" +) + +func main() { + type User struct { + Id int + Name string + Site string + } + var ( + ctx = gctx.New() + user *User + key = `UserKey` + value = &User{ + Id: 1, + Name: "GoFrame", + Site: "https://goframe.org", + } + ) + _ = gcache.Ctx(ctx).Set(key, value, 0) + v, _ := gcache.Ctx(ctx).GetVar(key) + _ = v.Scan(&user) + fmt.Printf(`%#v`, user) +} diff --git a/os/gcache/gcache_cache_adapter.go b/os/gcache/gcache_cache_adapter.go index 7ad9547fe..dce6b19ef 100644 --- a/os/gcache/gcache_cache_adapter.go +++ b/os/gcache/gcache_cache_adapter.go @@ -27,10 +27,10 @@ func (c *Cache) Sets(data map[interface{}]interface{}, duration time.Duration) e } // SetIfNotExist sets cache with `key`-`value` pair which is expired after `duration` -// if `key` does not exist in the cache. It returns true the `key` dose not exist in the +// if `key` does not exist in the cache. It returns true the `key` does not exist in the // cache, and it sets `value` successfully to the cache, or else it returns false. // -// The parameter `value` can be type of , but it dose nothing if its +// The parameter `value` can be type of , but it does nothing if its // result is nil. // // It does not expire if `duration` == 0. @@ -122,7 +122,7 @@ func (c *Cache) Size() (size int, err error) { } // Data returns a copy of all key-value pairs in the cache as map type. -// Note that this function may leads lots of memory usage, you can implement this function +// Note that this function may lead lots of memory usage, you can implement this function // if necessary. func (c *Cache) Data() (map[interface{}]interface{}, error) { return c.adapter.Data(c.getCtx())