1

Relatively new to Go and I can't figure out if I'm doing something stupid (quite likely) or if there's a bug (unlikely given I'm probably doing something stupid). I have 2 filesystem cache's using Beego's filesystem cache. They are two separate folders. When I try writing to each cache then try to retrieve values the results get mixed up. From what I can tell the creation of MyCache gets overridden by OtherCache in the Init() function:

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)
}

Result:

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

The first line of this output should point to "mycache", not "othercache". Also, the third line of this output is wrong...value should be "myvalue", not "othervalue".

Am I doing something wrong here? (My other option is to have 1 filesystem cache & change the key but it is much easier to manage 2 different folders & delete an entire cache easily if need be....I'd rather know what I'm doing wrong here, however)

4

1 回答 1

0

看一下NewCache函数的实现:

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

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

于 2014-12-05T21:23:57.630 回答