caching - 多个文件系统缓存互相破坏

标签 caching go beego

Go 相对较新,我无法弄清楚我是否在做一些愚蠢的事情(很有可能)或者是否存在错误(不太可能因为我可能在做一些愚蠢的事情)。我有 2 个文件系统缓存使用 Beego's filesystem cache .它们是两个独立的文件夹。当我尝试写入每个缓存然后尝试检索值时,结果会混淆。据我所知,MyCache 的创建在 Init() 函数中被 OtherCache 覆盖:

package main

import (
    "github.com/astaxie/beego/cache"
    "log"
)

var (
    MyCache    cache.Cache
    OtherCache cache.Cache
    err        error
)

func Init() {
    if MyCache, err = cache.NewCache("file", `{"CachePath":".cache/mycache","FileSuffix":".cache","DirectoryLevel":1,"EmbedExpiry":10}`); err != nil {
        log.Println(err)
    }
    if OtherCache, err = cache.NewCache("file", `{"CachePath":".cache/othercache","FileSuffix":".cache","DirectoryLevel":1,"EmbedExpiry":10}`); err != nil {
        log.Println(err)
    }
}
func checkCache(c cache.Cache, k string) string {
    b := c.Get(k)
    return b.(string)
}
func writeCache(c cache.Cache, k string, v string) {
    if err := c.Put(k, v, 10); err != nil {
        log.Println(err)
    }
}
func main() {
    log.SetFlags(log.Lshortfile)
    Init()
    log.Println(MyCache)
    log.Println(OtherCache)
    // write to MyCache
    mykey := "mykey"
    myvalue := "myvalue"
    writeCache(MyCache, mykey, myvalue)

    // write to OtherCache
    othervalue := "othervalue"
    writeCache(OtherCache, mykey, othervalue)

    // Get MyCache value
    myv := checkCache(MyCache, mykey)
    log.Println("MyCache:", myv, myv == myvalue)

    // Get OtherCache value
    otherv := checkCache(OtherCache, mykey)
    log.Println("OtherCache:", otherv, otherv == othervalue)
}

结果:

tester.go:34: &{/home/desktop-admin/Desktop/eclipse/go/src/testfolder/tester/.cache/othercache .cache 2 0}
tester.go:35: &{/home/desktop-admin/Desktop/eclipse/go/src/testfolder/tester/.cache/othercache .cache 2 0}
tester.go:47: MyCache: othervalue false
tester.go:51: OtherCache: othervalue true

此输出的第一行应指向“mycache”,而不是“othercache”。此外,此输出的第三行是错误的......值应该是“myvalue”,而不是“othervalue”。

我是不是做错了什么? (我的另一个选择是拥有 1 个文件系统缓存并更改 key ,但管理 2 个不同的文件夹并在需要时轻松删除整个缓存要容易得多......我宁愿知道我在这里做错了什么,然而)

最佳答案

看一下 NewCache 函数的实现:

https://github.com/astaxie/beego/blob/master/cache/cache.go#L84

这个包只允许每个适配器一个缓存。第二次调用同一适配器的 NewCache 将重新配置并返回同一对象。

关于caching - 多个文件系统缓存互相破坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27324350/

相关文章:

go - 没有这样的工具 "asm"

python - 类似于本地文件的 HTTP Headers 的属性系统

java - Guava CacheBuilder 最大尺寸

postgresql - 如何使 Buffalo 事务中间件提交?

android - 如何在android中实现缓存数据而不是从服务器加载数据

go - 如何在beego中获取controller之外的cookie和session

go - Beego - 端点测试

json - 如何从 HTTP 响应正文中读取错误

android - 如何缓存图像?

JavaFx WebView 缓存