| type Store interface { | |
| Get(interface{}) interface{} | |
| Set(interface{}) | |
| } | |
| type DatabaseStore struct { | |
| // wrap sql connection | |
| // or any other driver that your database needs | |
| } | |
| func (s *DatabaseStore) GetFollowers(userID string) ([]models.User, error) { | |
| // get users from db | |
| // can be any db, e.g. PostgreSQL, CouchDB, MongoDB | |
| return users, nil | |
| } | |
| type CacheStore struct { | |
| // embed any other store | |
| // this also makes unimplemented store methods just pass through | |
| Store | |
| // cache internals | |
| } | |
| func (s *CacheStore) GetFollowers(userID string) ([]models.User, error) { | |
| // return user from cache if it's there | |
| // if not, get from underlying store | |
| user, err = s.Store.GetFollowers(userID) | |
| // error check | |
| // add user to cache | |
| return users, nil | |
| } | |
| // easily instantiate any configuration you need | |
| store := CacheStore{DatabaseStore{DB: db}} | |
| // In package main, the store implementation should just return an object that can be passed around and queried |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment