gocache icon indicating copy to clipboard operation
gocache copied to clipboard

BigCache implementation does not return an error of type *store.NotFound when the key is not found

Open butaca opened this issue 1 year ago • 2 comments

I'm trying to check if a key exists in the cache. Since there is not a CacheInterface.Exists, I have to rely on CacheInterface.Get to perform the check. The Redis implementation correctly returns *store.NotFound when the key is not found, but the BigCache one does not.

Steps for Reproduction

  1. Set up BigCache:
bigcacheClient, _ := bigcache.New(ctx, bigcache.DefaultConfig(5 * time.Minute))
bigcacheStore := bigcache_store.NewBigcache(bigcacheClient)
cacheManager := cache.New[[]byte](bigcacheStore)
  1. Check the error type:
_, err := bigCacheCache.Get(ctx, "key")
_, ok := err.(*store.NotFound)
fmt.Printf("BigCache Type is *store.NotFound: %v\n", ok)
  1. See that is not of type *store.NotFound
  2. Perform the previous steps with Redis and see how the error is of type *store.NotFound

Expected behavior:

Returned error is of type *store.NotFound

Actual behavior:

Returned error is of not of type *store.NotFound

Platforms:

macOS and dockerized Linux from scratch

Versions:

gocache v4.1.3 and v4.1.4 go 1.21 BigCache store v4.1.2 and v4.2.0 BigCache v3.1.0 Redis store v4.2.0 Redis client v9.0.5 Redis server 7.0.12

butaca avatar Sep 07 '23 23:09 butaca

This seems like a trivial fix, just need to handle bigcache.ErrEntryNotFound in Get method. However, many existing library usages may be dependent on current behavior which returns error directly. So fixing this may cause issues for some users.

ShivamKumar2002 avatar Nov 29 '23 18:11 ShivamKumar2002