Skip to content

Instantly share code, notes, and snippets.

@cep21
Last active July 25, 2017 22:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cep21/e705d2303cd2e5d41e9b50b817b6e59c to your computer and use it in GitHub Desktop.
Save cep21/e705d2303cd2e5d41e9b50b817b6e59c to your computer and use it in GitHub Desktop.
example cache library
type ObjectCache {}
func (r *ObjectCache) Cached(ctx context.Context, key string, callback func() (interface{}, error), storeIntoPtr interface{}) error {
// Check cache for object, if it exists, get it from cache, otherwise call callback, store to cache, and put result
// into storeIntoPtr pointer
}
func (app * Application) UserCode(ctx context.Context) (*UserProfile, error) {
var ret *UserProfile
err := app.Cache.Cached(ctx, "user:" + userID, func() (interface{}, error) {
// This is only called if the object isn't in the cache
return app.BackingStore.GetUser(ctx, userID)
}, &ret)
return ret, err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment