Skip to content

Instantly share code, notes, and snippets.

@sapiens-sapide
Last active February 15, 2018 05:42
Show Gist options
  • Save sapiens-sapide/6b9c94fc6d0066bc3896ea9313d54ee5 to your computer and use it in GitHub Desktop.
Save sapiens-sapide/6b9c94fc6d0066bc3896ea9313d54ee5 to your computer and use it in GitHub Desktop.
shard index implementation
ElasticSearchBackend struct {
ElasticSearchConfig
Client *elastic.Client
Cache APICache // put a ShardsCache struct at ElasticSearchBackend initialization
}
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
//…
}
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