Last active
September 27, 2021 15:07
-
-
Save Israel-Miles/55cae3935ccbf5b26bcdfd80f70e5451 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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