mirror of
https://gitee.com/johng/gf.git
synced 2024-12-03 04:37:49 +08:00
完善gcache示例
This commit is contained in:
parent
0d19e05fb4
commit
c929a2f32e
@ -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
|
||||
}
|
||||
|
||||
// 获得所有的键名,组成数组返回
|
||||
|
@ -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
|
||||
|
||||
|
@ -44,7 +44,7 @@ func Dump(i...interface{}) {
|
||||
fmt.Errorf("%s", err.Error())
|
||||
}
|
||||
}
|
||||
fmt.Println()
|
||||
//fmt.Println()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
@ -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())
|
||||
}
|
29
geg/os/gcache/usage_basic.go
Normal file
29
geg/os/gcache/usage_basic.go
Normal file
@ -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()
|
||||
}
|
30
geg/os/gcache/usage_senior.go
Normal file
30
geg/os/gcache/usage_senior.go
Normal file
@ -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())
|
||||
}
|
@ -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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user