在一个项目中,我想使用缓存来存储哈希之类的东西。但是,缓存中存储的值有时会更改为键。通常, key 中的 4 个字符左右会被接管:
<- Set hash::helloworldtest = abcdef0123456789
-> Get hash::helloworldtest = testef0123456789
这大致就是我的缓存的结构:
type node struct {
expires nodeExpiration
value interface{}
}
// ...
func (c *Cache) Set(key string, value interface{}, expiration time.Duration) {
c.mu.Lock()
c.val[key] = &node{
expires: c.expiration(expiration),
value: value,
}
// fmt.Println( ... )
c.mu.Unlock()
}
func (c *Cache) Get(key string) (interface{}, bool) {
c.mu.Lock()
if v, o := c.val[key]; o && v != nil {
if !v.expires.IsExpired() {
// fmt.Println( ... )
c.mu.Unlock()
return v.value, true
}
}
c.mu.Unlock()
return nil, false
}
// Cache Backend
func (b *CacheBackend) GetHash(key string) (res string, err error) {
return b.get("hash::" + key)
}
func (b *CacheBackend) get(key string) (res string, err error) {
if v, ok := b.cache.Get(key); ok {
if s, ok := v.(string); ok {
return s, nil
}
return "", b.errCast
}
return "", nil
}
// go-fiber Route
func (s *WebServer) handleGetHashAnywhere(ctx *fiber.Ctx) (err error) {
path := ctx.Params("anywhere")
var res string
if res, err = s.Backend.GetHash(path); err != nil {
return
}
if res == "" {
ctx.Status(404)
} else {
ctx.Status(200)
}
return ctx.SendString(res)
}
我之前使用的是 sync.RWMutex
,但将其替换为 sync.Mutex
,认为这可能就是问题所在。但与 sync.Mutex
相同。
go-fiber 在 goroutine 中调用 Get 和 Set 方法,然后返回这些值。
有谁知道这样的事情怎么会发生吗?
编辑 1:保存 []byte
而不是 string
效果很好。
最佳答案
感谢@majodev,问题终于得到正确解决。
Zero Allocation 下的文档中描述了该问题。摘录:
Because fiber is optimized for high-performance, values returned from fiber.Ctx are not immutable by default and will be re-used across requests. [...] As soon as you return from the handler, any values you have obtained from the context will be re-used in future requests and will change below your feet.
因此必须复制上下文值,或者必须在 Fiber 配置中传递“不可变”标志。
第一个解决方案:
从读取值创建新缓冲区并复制其内容
buf := bytes.NewBufferString(ctx.Params("hash"))
hash := string(buf.Bytes())
第二个解决方案:
使用描述的内置函数 utils#CopyString(string)
here .
hash := utils.CopyString(ctx.Params("hash"))
第三种解决方案:
不可变 config标志
cfg := &fiber.Config{Immutable: true}
然后一切正常。
关于go - Race(?) with Mutex - map 中的数据损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66930097/