多个文件系统缓存相互干扰引发混乱。

huangapple go评论89阅读模式
英文:

Multiple file system caches causing havoc for each other

问题

相对于Go语言来说,我还是比较新手,我无法确定我是在做一些愚蠢的事情(很可能是这样),还是存在一个bug(鉴于我可能在做一些愚蠢的事情,这种可能性不大)。我有两个使用Beego的文件系统缓存的缓存,它们是两个独立的文件夹。当我尝试向每个缓存写入数据,然后尝试检索值时,结果会混乱。据我所知,在Init()函数中,MyCache的创建被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"。

我在这里做错了什么吗?(我的另一个选择是只有一个文件系统缓存,并更改键名,但是如果需要的话,管理两个不同的文件夹并轻松删除整个缓存会更容易...然而,我更想知道我在这里做错了什么)

英文:

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)

答案1

得分: 0

请看一下NewCache函数的实现:

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

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

英文:

Have a look at the implementation of the NewCache function:

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

This package only allows one cache per adapter. The second call to NewCache for the same adapter will reconfigure and return the same object.

huangapple
  • 本文由 发表于 2014年12月6日 04:42:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/27324350.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定