V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
hbolive
V2EX  ›  Go 编程语言

go 超级萌新,求问 go-cache 的问题

  •  
  •   hbolive · 2020-09-21 12:24:29 +08:00 · 2096 次点击
    这是一个创建于 1285 天前的主题,其中的信息可能已经有所发展或是发生改变。
    背景:刚过完 go 语法,还没入门那种,现在有个 go-cache 的例子,有 3 个问题请教大家
    目的:设置缓存,在缓存未过期时从缓存读取数据,如果读取失败,则将缓存内容写入缓存;最后将内容打印出来。

    package main

    import (
    "fmt"
    "time"

    "github.com/patrickmn/go-cache"
    )

    func main() {
    var id cateId = 20
    data := id.getCache()
    fmt.Println(data)
    }

    type cateId int

    func (id cateId) getCache() string {
    cacheName := "cache_20"
    //cacheName := "cache_" + string(id)
    c := cache.New(30*time.Second, 20*time.Second)
    value, found := c.Get(cacheName)
    if found {
    return value.(string)
    } else {
    c.Set(cacheName, "testdata_20", cache.DefaultExpiration)
    return "testdata_200"
    //return "testdata_" + string(id) //始终输出 testdata_
    }
    }

    问题 1:编译后执行得到的结果始终是 testdata_200,缓存 30 秒过期,第一次执行得到 testdata_200 正常,为什么第二次执行得到的不是缓存数据 testdata_20 呢?莫非 go-cache 在程序运行结束,缓存也消失了?

    问题 2://return "testdata_" + string(id) //始终输出 testdata_,而不是输出 testdata_20 ?在 windows 命令行窗口 20 是个乱码,其他终端下运行直接没有

    问题 3:我这样写符合大众习惯吗?毕竟这个写法跟我以前用 PHP/Python 差别挺大的。。

    PHP 为例:
    $data = $this->_getCache(20);

    protected _getCache(cateId)
    {
    $data = Cache->get('cacheName');
    if( !$data ){
    $data = '一些数据'
    Cache->set('cacheName', $data);
    }
    return $data;
    }

    先谢谢大家!
    第 1 条附言  ·  2020-09-21 16:06:50 +08:00
    func main() {
    for {
    rand.Seed(time.Now().Unix())
    var id cateId = cateId(rand.Intn(9) + 1) // 随机得到 1-10 的数字
    data := id.getCache()
    fmt.Println(id, data)
    time.Sleep(2 * time.Second)
    }

    }

    type cateId int

    func (id cateId) getCache() string {
    cacheName := "cache_" + string(id) // 缓存名字与数字关联
    c := cache.New(30*time.Second, 10*time.Second)
    value, found := c.Get(cacheName)
    if found {
    return value.(string)
    } else {
    c.Set(cacheName, "testdata_"+string(id), cache.DefaultExpiration)
    return "testdata_" + string(id) //始终输出 testdata_乱码
    }
    }
    第 2 条附言  ·  2020-09-21 17:25:54 +08:00
    func main() {
    	for {
    		rand.Seed(time.Now().Unix())
    		id := strconv.Itoa(rand.Intn(9) + 1) // 随机得到1-10的数字字符
    		result := getCache(id)               // 得到结果
    		fmt.Println(id, result)
    		time.Sleep(2 * time.Second)
    	}
    
    }
    
    func getCache(id string) string {
    	cacheName := "cache_" + id // 缓存名字与ID关联
    	c := cache.New(30*time.Second, 10*time.Second)
    	value, found := c.Get(cacheName)
    	if found {
    		return value.(string)
    	} else {
    		c.Set(cacheName, "testdata_"+id, cache.DefaultExpiration)
    		return "new:testdata_" + id
    	}
    }
    

    还是换成这种熟悉的写法,但是输出的始终是

    1 new:testdata_1
    3 new:testdata_3
    1 new:testdata_1
    5 new:testdata_5
    

    像第3行应该输出 1 testdata_1 才对呀

    7 条回复    2020-09-21 17:28:15 +08:00
    rimutuyuan
        1
    rimutuyuan  
       2020-09-21 12:28:02 +08:00
    1. cache 存储在内存,程序退出后内存都释放掉了,key 也不存在了
    添加以下代码可以实现你期望的结果
    data2 := id.getCache()
    fmt.Println(data2)
    hbolive
        2
    hbolive  
    OP
       2020-09-21 16:05:41 +08:00
    @rimutuyuan 谢谢,已经搞清楚了了,缓存随着运行结束,也会释放掉。
    现在加了个 for 循环,让程序一直运行,但是还是一直输出 testdata_200,而不是 testdata_20,就像主贴里一样,很奇怪。
    现在 append 了一下,想缓存名字与 id 关联起来,结果 CMD 下执行,输出的就是 testdata_乱码。
    xkeyideal
        3
    xkeyideal  
       2020-09-21 16:17:02 +08:00
    既然是萌新就不说了,这个沙雕 cache 库别用,流量大了,你就可以收拾包裹滚蛋了
    CoolSpring
        4
    CoolSpring  
       2020-09-21 16:18:54 +08:00
    乱码是因为 string() 并不是把数字转换成对应字符的形式,它的作用是把这个数字当作 UTF-8 值转换到对应的字符
    可以用 strconv.Itoa() 或者 fmt.Sprintf()
    scukmh
        5
    scukmh  
       2020-09-21 16:33:40 +08:00
    @xkeyideal 有先例吗?已经用了,有点害怕。
    xkeyideal
        6
    xkeyideal  
       2020-09-21 16:51:31 +08:00   ❤️ 1
    @scukmh 要啥先例,一个 cache 都去找开源的,找就找了,也看看源码呀,这个裤子的过期操作简直是智障,抓紧换了吧
    hbolive
        7
    hbolive  
    OP
       2020-09-21 17:28:15 +08:00
    @CoolSpring 抱歉,犯了个低级错误,看来还得过几遍语法
    再次 append 了,就是无法获取缓存数据
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5360 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 08:35 · PVG 16:35 · LAX 01:35 · JFK 04:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.