This library was implemented as part of a recruitment task. It was intended to demonstrate the problem that occurs when sharing shared cache memory between multiple instances of an application.
- Local cache operations: Set, Get, and Delete
- Setting TTL - the maximum lifespan of a data in a cache before it is discarded
- Lazy loading - loads data into the cache only when necessary using auto fetching mechanism
- Distribution across cache pool peers using consistent hashing
package main
import "github.com/cygkam/dcache/pkg/cache"
func main() {
c := cache.NewCache()
key := "9663d0d0-eb9d-429a-8c87-6c8b6645bee7"
data := []byte("Ut fringilla lectus nec")
ttl := time.Second * 30
c.Set(key, data, ttl)
cachedData, hit := c.Get(key)
if hit {
fmt.Println(string(cachedData))
// Ut fringilla lectus nec
}
c.Delete(key)
}package main
import "github.com/cygkam/dcache/pkg/cachepool"
func main() {
cfg := &cachepool.CachePoolCfg{
Port: "30345",
Ttl: time.Second * 30
}
cp := cachepool.NewCachePool(cfg)
cp.SetPeers("10.11.0.1:9909","10.11.0.2:9909")
go cp.StartHTTPPoolServer()
cachedData, hit := c.Get(key)
if hit {
fmt.Println(string(cachedData))
// Who knows what peers have in their local cache
}
}