A simple groupcache-like distributed cache implemented by Golang.
- Cache Eviction Algorithm (LRU)
- Stand-alone Cache
- Distributed Cache
- HTTP Server and Client
- Consistent Hash (prevent cache avalanche)
- Singleflight (prevent cache breakdown)
func startCacheServer(addr string, addrs []string, group *ginglecache.Group) {
picker := ginglecache.NewHTTPPool(addr)
picker.ConfPeers(addrs...)
group.RegisterPicker(picker)
log.Println("ginglecache server is running at", addr)
log.Fatal(http.ListenAndServe(addr[7:], picker))
}
func startCacheClient(addr string, group *ginglecache.Group) {
http.Handle("/api", http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
key := req.URL.Query().Get("key")
view, err := group.Get(key)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
rw.WriteHeader(http.StatusOK)
rw.Header().Set("Content-Type", "application/octet-stream")
rw.Write(view.Bytes())
}))
log.Println("ginglecache client is running at", addr)
log.Fatal(http.ListenAndServe(addr[7:], nil))
}
- HTTP2 Support
- Protobuf Support
- More Replacement Strategy
- Distributed Unique ID Generation
- Distributed Lock Manager
- Goroutine and Connect Pool