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)