refactoring
This commit is contained in:
parent
9f66851529
commit
718c39de00
|
@ -20,22 +20,31 @@ type EtcdGroupCacheHandler struct {
|
|||
}
|
||||
|
||||
func NewEtcdGroupCacheHandler(cfg *viper.Viper) (*EtcdGroupCacheHandler, error) {
|
||||
var endpoint = cfg.GetString("storage.kv.groupcache.endpoint")
|
||||
var (
|
||||
endpoint = cfg.GetString("storage.kv.groupcache.endpoint")
|
||||
ports = cfg.GetStringSlice("storage.kv.groupcache.ports")
|
||||
cacheTTL = cfg.GetInt64("storage.kv.groupcache.cacheTTL")
|
||||
cacheServers []*http.Server
|
||||
)
|
||||
etcdHandler, err := NewEtcdHandler(cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pool := groupcache.NewHTTPPoolOpts("http://"+endpoint, &groupcache.HTTPPoolOptions{})
|
||||
server := http.Server{
|
||||
Addr: endpoint,
|
||||
Handler: pool,
|
||||
}
|
||||
go func() {
|
||||
log.Printf("Serving....\n")
|
||||
if err := server.ListenAndServe(); err != nil {
|
||||
log.Fatal(err)
|
||||
pool := groupcache.NewHTTPPoolOpts("", &groupcache.HTTPPoolOptions{})
|
||||
for _, port := range ports {
|
||||
pool.Set("http://" + endpoint + ":" + port)
|
||||
server := &http.Server{
|
||||
Addr: endpoint + ":" + port,
|
||||
Handler: pool,
|
||||
}
|
||||
}()
|
||||
cacheServers = append(cacheServers, server)
|
||||
go func(srv *http.Server) {
|
||||
log.Printf("Serving cache server %s \n", srv.Addr)
|
||||
if err := srv.ListenAndServe(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}(server)
|
||||
}
|
||||
group := groupcache.NewGroup("data", 3000000, groupcache.GetterFunc(
|
||||
func(ctx context.Context, key string, dest groupcache.Sink) error {
|
||||
|
||||
|
@ -50,7 +59,7 @@ func NewEtcdGroupCacheHandler(cfg *viper.Viper) (*EtcdGroupCacheHandler, error)
|
|||
if err != nil {
|
||||
return fmt.Errorf("failed to decode value for key %s: %v", key, err)
|
||||
}
|
||||
dest.SetString(string(decoded), time.Now().Add(time.Minute*5))
|
||||
dest.SetString(string(decoded), time.Now().Add(time.Duration(cacheTTL)*time.Minute))
|
||||
return nil
|
||||
},
|
||||
))
|
|
@ -70,6 +70,8 @@ func createConfig() *viper.Viper {
|
|||
cfg.Set("storage.kv.etcd.username", "")
|
||||
cfg.Set("storage.kv.etcd.prefix", "parcoursmob/cache/")
|
||||
cfg.Set("storage.kv.etcd.password", "")
|
||||
cfg.Set("storage.kv.groupcache.endpoint", "localhost:8080")
|
||||
cfg.Set("storage.kv.groupcache.endpoint", "localhost")
|
||||
cfg.Set("storage.kv.groupcache.ports", []string{"36000", "36500"})
|
||||
cfg.Set("storage.kv.groupcache.cacheTTL", 10)
|
||||
return cfg
|
||||
}
|
Loading…
Reference in New Issue