diff --git a/g/os/gcache/gcache_mem_cache.go b/g/os/gcache/gcache_mem_cache.go index 821651c70..a760e26e9 100644 --- a/g/os/gcache/gcache_mem_cache.go +++ b/g/os/gcache/gcache_mem_cache.go @@ -176,20 +176,26 @@ func (c *memCache) Contains(key interface{}) bool { return c.Get(key) != nil } -// 删除指定键值对 -func (c *memCache) Remove(key interface{}) { +// 删除指定键值对,并返回被删除的键值 +func (c *memCache) Remove(key interface{}) interface{} { c.dmu.Lock() - delete(c.data, key) + item, ok := c.data[key] + if ok { + delete(c.data, key) + } c.dmu.Unlock() + return item.v } -// 批量删除键值对 -func (c *memCache) BatchRemove(keys []interface{}) { +// 批量删除键值对,并返回被删除的键值对数据 +func (c *memCache) BatchRemove(keys []interface{}) map[interface{}]interface{} { + m := make(map[interface{}]interface{}) for _, key := range keys { - c.dmu.Lock() - delete(c.data, key) - c.dmu.Unlock() + if v := c.Remove(key); v != nil { + m[key] = v + } } + return m } // 获得所有的键名,组成数组返回 diff --git a/g/os/gcache/gcache_test.go b/g/os/gcache/gcache_test.go index ebb9b0bc2..1f15aedd5 100644 --- a/g/os/gcache/gcache_test.go +++ b/g/os/gcache/gcache_test.go @@ -4,7 +4,7 @@ // If a copy of the MIT was not distributed with this file, // You can obtain one at https://gitee.com/johng/gf. -// go test *.go -bench=".*" +// go test *.go -bench=".*" -benchmem package gcache diff --git a/g/util/gutil/gutil.go b/g/util/gutil/gutil.go index 0f6298a6d..1b8ec0aa7 100644 --- a/g/util/gutil/gutil.go +++ b/g/util/gutil/gutil.go @@ -44,7 +44,7 @@ func Dump(i...interface{}) { fmt.Errorf("%s", err.Error()) } } - fmt.Println() + //fmt.Println() } } diff --git a/geg/os/gcache/basic.go b/geg/os/gcache/basic.go deleted file mode 100644 index 981321c66..000000000 --- a/geg/os/gcache/basic.go +++ /dev/null @@ -1,36 +0,0 @@ -package main - -import ( - "gitee.com/johng/gf/g/os/gcache" - "time" - "fmt" -) - -func main() { - for i := 0; i < 10; i++ { - gcache.Set(i, i, 0) - } - fmt.Println(gcache.Size()) - fmt.Println(gcache.Keys()) - gcache.SetCap(2) - time.Sleep(3*time.Second) - fmt.Println(gcache.Size()) - fmt.Println(gcache.Keys()) - - return - gcache.Set("k1", "v1", 1000) - gcache.Set("k2", "v2", 2000) - fmt.Println(gcache.Keys()) - fmt.Println(gcache.Values()) - fmt.Println(gcache.Size()) - time.Sleep(500*time.Millisecond) - fmt.Println(gcache.Get("k1")) - fmt.Println(gcache.Get("k2")) - time.Sleep(400*time.Millisecond) - fmt.Println(gcache.Get("k1")) - fmt.Println(gcache.Get("k2")) - time.Sleep(3000*time.Millisecond) - fmt.Println(gcache.Get("k1")) - fmt.Println(gcache.Get("k2")) - time.Sleep(3000*time.Millisecond) -} \ No newline at end of file diff --git a/geg/os/gcache/expire.go b/geg/os/gcache/expire.go deleted file mode 100644 index 1c4801909..000000000 --- a/geg/os/gcache/expire.go +++ /dev/null @@ -1,19 +0,0 @@ -package main - -import ( - "gitee.com/johng/gf/g/os/gcache" - "fmt" - "time" -) - -func main() { - gcache.Set("k1", "v1", 1000) - gcache.Set("k2", "v2", 2000) - fmt.Println(gcache.Keys()) - fmt.Println(gcache.Values()) - - time.Sleep(1*time.Second) - - fmt.Println(gcache.Keys()) - fmt.Println(gcache.Values()) -} \ No newline at end of file diff --git a/geg/os/gcache/usage_basic.go b/geg/os/gcache/usage_basic.go new file mode 100644 index 000000000..3cc2a4545 --- /dev/null +++ b/geg/os/gcache/usage_basic.go @@ -0,0 +1,29 @@ +package main + +import ( + "gitee.com/johng/gf/g/os/gcache" + "gitee.com/johng/gf/g" +) + +func main() { + // 创建一个缓存对象,当然也可以直接使用gcache包方法 + c := gcache.New() + + // 设置缓存,不过期 + c.Set("k1", "v1", 0) + + // 获取缓存 + g.Dump(c.Get("k1")) + + // 获取缓存大小 + g.Dump(c.Size()) + + // 缓存中是否存在指定键名 + g.Dump(c.Contains("k1")) + + // 删除并返回被删除的键值 + g.Dump(c.Remove("k1")) + + // 关闭缓存对象,让GC回收资源 + c.Close() +} \ No newline at end of file diff --git a/geg/os/gcache/lru.go b/geg/os/gcache/usage_lru.go similarity index 100% rename from geg/os/gcache/lru.go rename to geg/os/gcache/usage_lru.go diff --git a/geg/os/gcache/usage_senior.go b/geg/os/gcache/usage_senior.go new file mode 100644 index 000000000..ae5a4b91d --- /dev/null +++ b/geg/os/gcache/usage_senior.go @@ -0,0 +1,30 @@ +package main + +import ( + "gitee.com/johng/gf/g/os/gcache" + "gitee.com/johng/gf/g" + "time" +) + +func main() { + // 当键名不存在时写入,设置过期时间1000毫秒 + gcache.SetIfNotExist("k1", "k1", 1000) + + // 打印当前的键名列表 + g.Dump(gcache.Keys()) + + // 打印当前的键名列表 []string 类型 + g.Dump(gcache.KeyStrings()) + + // 获取指定键值,如果不存在时写入,并返回键值 + g.Dump(gcache.GetOrSet("k2", "v2", 0)) + + // 打印当前的键值列表 + g.Dump(gcache.Values()) + + // 等待1秒,以便k1:v1自动过期 + time.Sleep(time.Second) + + // 再次打印当前的键值列表,发现k1:v1已经过期,只剩下k2:v2 + g.Dump(gcache.Values()) +} \ No newline at end of file diff --git a/geg/other/test.go b/geg/other/test.go index 6cb226606..0a7d6c8ae 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -1,10 +1,10 @@ package main import ( - "fmt" - "math" + "gitee.com/johng/gf/g" ) func main() { - fmt.Println(int64(math.Ceil(float64(11111/10000) + 1)*10000)) + g.Dump(1,2,3) + g.Dump(1,2,3) } \ No newline at end of file