Last active
July 25, 2017 22:26
-
-
Save cep21/e705d2303cd2e5d41e9b50b817b6e59c to your computer and use it in GitHub Desktop.
example cache library
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
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