Created
April 2, 2022 11:28
-
-
Save kben19/952397de4855d793e31a80bc9ef45b8d to your computer and use it in GitHub Desktop.
Wrapper Caching
This file contains 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 (wrapper *ServiceWrapper) GetProductStock(id int) (stock int, err error) { | |
key := fmt.Sprintf("key-%d", id) | |
value, err := wrapper.cache.Get(key) | |
if err == nil && value != "" { | |
stock, _ = strconv.Atoi(value) | |
return | |
} | |
defer func () { | |
_ = wrapper.cache.Set(key, strconv.Itoa(stock)) | |
} () | |
stock, err = wrapper.service.GetProductStock(id) | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment