1 Star 0 Fork 0

liujinsuo/tool
暂停

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
toolcache.go 1.43 KB
一键复制 编辑 原始数据 按行查看 历史
liujinsuo 提交于 2026-01-05 11:13 +08:00 . toolcache.Fetch
package main
import (
"fmt"
"gitee.com/liujinsuo/tool/toolcache"
"sync"
"time"
)
func main() {
fn2()
}
func fn2() {
wg := &sync.WaitGroup{}
cache := toolcache.NewMemCache[string]()
wg.Add(2)
f := func() (string, error) {
time.Sleep(time.Second)
return "111", nil
}
go func() {
defer wg.Done()
for i := 0; i < 10; i++ {
fetch, err, shared := cache.Fetch("1111", time.Minute, f)
fmt.Println("aaaa", fetch, err, shared)
}
}()
go func() {
defer wg.Done()
for i := 0; i < 10; i++ {
fetch, err, shared := cache.Fetch("1111", time.Minute, f)
fmt.Println("bbbb", fetch, err, shared)
}
}()
wg.Wait()
}
func fn1() {
cache := toolcache.NewMemCache[string]()
cache.Set("111", "222", time.Second*5)
cache.Set("112", "222", time.Second*5)
cache.Set("113", "222", time.Second*5)
cache.Set("114", "222", time.Second*5)
cache.Set("115", "222", time.Second*5)
cache.Set("116", "222", time.Second*5)
{
del, loaded := cache.LoadAndDelete("111")
fmt.Println("删除后的结果", del, loaded)
content, ok := cache.Get("111")
fmt.Println("获取111", content, ok)
del2, loaded2 := cache.LoadAndDelete("111")
fmt.Println("删除后的结果", del2, loaded2)
}
time.Sleep(time.Second * 1000)
for i := 0; i < 300; i++ {
time.Sleep(time.Millisecond * 200)
content, ok := cache.Get("111")
if ok == false {
fmt.Println(time.Now(), "--------false")
} else {
fmt.Println(time.Now(), content)
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/liujinsuo/tool.git
git@gitee.com:liujinsuo/tool.git
liujinsuo
tool
tool
68eb72d9e930

搜索帮助