eko / gocache

☔️ A complete Go cache library that brings you multiple ways of managing your caches
https://vincent.composieux.fr/article/i-wrote-gocache-a-complete-and-extensible-go-cache-library/
MIT License
2.36k stars 193 forks source link

Redis Get silently returns an empty []byte for stored []byte values #222

Open butaca opened 10 months ago

butaca commented 10 months ago

The issue is similar to https://github.com/eko/gocache/issues/166 but for Redis with []byte values. When getting a []byte value the cache returns an empty []byte without an error. This is because the Redis implementation always returns strings and fails when trying to convert the value to string. Since it silently fails, it was difficult to debug.

Steps for Reproduction

  1. Set up a cache manager with a Redis store:
redisStore := redis_store.NewRedis(redis.NewClient(&redis.Options{
    Addr: "127.0.0.1:6379",
}))
cacheManager := cache.New[[]byte](redisStore)
  1. Set a []byte value
value := []byte{1, 2, 3, 4}
err := cacheManager.Set(ctx, "key", value, store.WithExpiration(15*time.Second))
if err != nil {
    panic(err)
}
  1. Get the value

    cachedValue, err := cacheManager.Get(ctx, "key")
    if err != nil {
    panic(err)
    }
  2. Check how the returned value has len = 0

fmt.Printf("cached value len: %v\n", len(cachedValue))

Expected behavior: The returned value is the stored []byte not and empty value

Actual behavior: It silently returns an empty []byte instead of the stored value

Platforms: macOS and dockerized Linux from scratch

Versions: gocache v4.1.3 go 1.21 Redis store v4.2.0 Redis client v9.0.5 Redis server 7.0.12

luoxiaohei commented 10 months ago

@eko Maybe we can add a Mashaler and Unmashaler to the cache and allow users to choose whether to use them or not? This will solve the problem of this type mismatch.

update: The Marshaler package has implemented encapsulation logic for cached data.

claudiunicolaa commented 1 month ago

related https://github.com/eko/gocache/issues/197