A Go Library to create distributed in-memory cache inside your app.
redis
or memcached
is not acceptable for youSet
, Get
, and Delete
operationOnly need to specify one or few nodes as bootstrap nodes, and all nodes will find each other using gossip protocol
Set
and Delete
, the event will be propagated to all of the nodes.So, all of the nodes will eventually have synced data.
Cache filling mechanism is provided in GetWithFiller func.
When the cache for the given key is not exists:
Filler
Filler
Even there are many goroutines which call GetWithFiller
, the given Filler
func
will only called once for each of the key.
Cache stampede could be avoided this way.
In server 1
bc, err := New(Config{
// PeerID: 1, // leave it, will be set automatically based on mac addr
ListenAddr: "192.168.0.1:12345",
Peers: nil, // it nil because we will use this node as bootstrap node
MaxKeys: 1000,
Logger: logrus.New(),
})
if err != nil {
log.Fatalf("failed to create cache: %v", err)
}
bc.Set("my_key", "my_val",86400)
In server 2
bc, err := New(Config{
// PeerID: 2, // leave it, will be set automatically based on mac addr
ListenAddr: "192.168.0.2:12345",
Peers: []string{"192.168.0.1:12345"},
MaxKeys: 1000,
Logger: logrus.New(),
})
if err != nil {
log.Fatalf("failed to create cache: %v", err)
}
bc.Set("my_key2", "my_val2", 86400)
In server 3
bc, err := New(Config{
// PeerID: 3,// will be set automatically based on mac addr
ListenAddr: "192.168.0.3:12345",
Peers: []string{"192.168.0.1:12345"},
MaxKeys: 1000,
Logger: logrus.New(),
})
if err != nil {
log.Fatalf("failed to create cache: %v", err)
}
val, exists := bc.Get("my_key2")
c, err := New(Config{
PeerID: 3,
ListenAddr: "192.168.0.3:12345",
Peers: []string{"192.168.0.1:12345"},
MaxKeys: 1000,
})
if err != nil {
log.Fatalf("failed to create cache: %v", err)
}
val, exp,err := bc.GetWithFiller("my_key2",func(key string) (string, error) {
// get value from database
.....
//
return value, 0, nil
}, 86400)