In a project I want to use a cache to store things like a hash. However, it happens from time to time that the stored value in the cache changes to the key. Usually around 4 characters from the key are taken over:
<- Set hash::helloworldtest = abcdef0123456789
-> Get hash::helloworldtest = testef0123456789
This is roughly how my cache is structured:
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)
}
I was using a sync.RWMutex
before, but replaced it with a sync.Mutex
, thinking that might be where the problem was. But same with sync.Mutex
.
The Get and Set methods are called in a goroutine by go-fiber to then return these values.
Does anyone have any idea how something like this can happen?
EDIT 1: Saving []byte
instead of string
works fine.
Thanks to @majodev the problem was finally fixed properly.
The problem is described in the documentation under Zero Allocation. An excerpt:
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.
So the context values must be copied, or the "Immutable" flag must be passed in the fiber config.
1st Solution:
New buffer from read values and copy its contents
buf := bytes.NewBufferString(ctx.Params("hash"))
hash := string(buf.Bytes())
2nd Solution:
Use builtin function utils#CopyString(string)
described here.
hash := utils.CopyString(ctx.Params("hash"))
3rd Solution:
Immutable config flag
cfg := &fiber.Config{Immutable: true}
then everything works.