完善gcache示例

This commit is contained in:
John
2018-09-17 09:52:24 +08:00
parent 4db3d87253
commit 176ff2dd58
9 changed files with 78 additions and 68 deletions

View File

@ -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
}
// 获得所有的键名,组成数组返回

View File

@ -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

View File

@ -44,7 +44,7 @@ func Dump(i...interface{}) {
fmt.Errorf("%s", err.Error())
}
}
fmt.Println()
//fmt.Println()
}
}