Last active
February 15, 2018 05:42
-
-
Save sapiens-sapide/6b9c94fc6d0066bc3896ea9313d54ee5 to your computer and use it in GitHub Desktop.
shard index implementation
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
ElasticSearchBackend struct { | |
ElasticSearchConfig | |
Client *elastic.Client | |
Cache APICache // put a ShardsCache struct at ElasticSearchBackend initialization | |
} |
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 (es *ElasticSearchBackend) CreateContact(contact *Contact) error { //unchanged signature | |
//… | |
shard, err := es.Cache.GetUserShard(userId) | |
if err != nil { | |
//~ | |
} | |
// make use of shard to PUT doc into ES | |
//… | |
} |
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
type ShardsCache struct { | |
redis *RedisBackend | |
cassa *CassandraBackend | |
} | |
// implements APICache | |
func (sc *ShardsCache) GetUserShard(userId UUID) (shard string, err error) { | |
shard, err := redis.client.Get(userId.String()).String() | |
if err != nil { | |
//shard not in redis, get it from cassa | |
shard, err := cassa.GetUserShard(userId) // TODO | |
if err != nil { | |
//handle error | |
} | |
// return shard and put it into cache for nex time | |
go func(user, shard string) { | |
redis.client.Set(user, shard, time.Minute*10) | |
}(userId.String(), shard) | |
} | |
return shard, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment