Trying to solve a simple duplicate check using an l1 cache using either of these 2 libraries https://github.com/patrickmn/go-cache or https://github.com/karlseguin/ccache
Several goroutines are executed which fetch a large amount of data from an upstream API. Inside of each, there is a for range
loop which checks if the key is a hit or a miss. In case of a hit, the loop should skip and continue onto the next iteration. As you may have guessed, the cache should be concurrency safe. I also instantiate only 1 instance of this cache (x.cacheInstance
).
for _, record := range data {
...
exists := x.cacheInstance.Get(record.DocumentID)
if exists == nil {
x.cacheInstance.Set(record.DocumentID, true, time.Minute * 10)
} else {
c.logger.Warnw("DocumentID duplicate located", "documentID", record.DocumentID)
continue
}
...
}
However, the application seems to hang, and not continue iterating over records. Commenting the lru cache check fixes the problem, allowing the program to run as expected.