this post was submitted on 27 Sep 2023
11 points (100.0% liked)
Golang
2197 readers
1 users here now
This is a community dedicated to the go programming language.
Useful Links:
Rules:
- Posts must be relevant to Go
- No NSFW content
- No hate speech, bigotry, etc
- Try to keep discussions on topic
- No spam of tools/companies/advertisements
- It’s OK to post your own stuff part of the time, but the primary use of the community should not be self-promotion.
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
In applications where I’ve needed this, I’ve taken a manual approach. Structure the function to return a single Result struct (including an error field), develop a convention for mapping function inputs to a string, then add reads & writes to a
map[string]*Result
which allows me to return cached values as a shortcut. No idea if it’s the most efficient way, I’ve not actually considered finding a package to handle the process.Edit: For more advanced behavior, what are your thoughts on the official memoize package?
I suppose I should be clearer on the features I want. I'd want to be able to store my cache in memcached or redis and I want the cached data to expire. So for one call, I might want to keep it for five minutes, but another one can stick around for 24 hours.
The memorize package falls down there.
Pet peeve: don't use string keys. It invites key collision errors. Use the fact Go supports structs as keys in maps. Safer and more efficient.