Skip to content

Instantly share code, notes, and snippets.

@Israel-Miles
Last active September 27, 2021 15:07
Show Gist options
  • Save Israel-Miles/55cae3935ccbf5b26bcdfd80f70e5451 to your computer and use it in GitHub Desktop.
Save Israel-Miles/55cae3935ccbf5b26bcdfd80f70e5451 to your computer and use it in GitHub Desktop.
func GetValue(id int, wg *sync.WaitGroup, mtx *sync.RWMutex) (int, bool) {
defer wg.Done()
// Check the cache
mtx.RLock()
val, ok := cache[id]
mtx.RUnlock()
if ok {
return val, ok
}
// Query the mock database
time.Sleep(300 * time.Millisecond)
mtx.Lock()
val, ok = database[id]
cache[id] = database[id]
mtx.Unlock()
return val, ok
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment