A sharded, concurrent key-value store (cache) library for Go, suitable for single-machine applications.
$ go get github.com/korzepadawid/cacher
package main
import (
"fmt"
"log"
"time"
"github.com/korzepadawid/cacher"
)
type vertex struct {
x, y float64
}
func main() {
c, err := cacher.New(&cacher.Config{
// You can disable this feature, just use cacher.NoExpiration instead.
// It's possible to override this setting whenever you want with PutWithExpiration().
DefaultExpiration: time.Hour,
// I used the concept of “sharding” and split a large hash table
// into multiple partitions to localise the effects of read/write locks.
// NumberOfShards must be greater than or equal two.
NumberOfShards: 20,
// It's possible to disable this feature, use cacher.NoCleanup.
CleanupInterval: time.Second * 5,
})
if err != nil {
log.Fatal(err)
}
// It's not mandatory, but it's recommended.
// Store pointers, and performance will be improved.
c.Put("p1", &vertex{x: 0.1, y: 0.2})
item, err := c.Get("p1")
if err != nil {
log.Fatal(err)
}
v := item.(*vertex)
fmt.Println(v) // &{0.1 0.2}
}
The library uses the dbj2 algorithm for generating string hashes.
Cacher uses the concept of "sharding" and splits a large hash table into multiple partitions to localise the effects of "read/write" locks. With many read and write requests, there's no need to block the whole data structure unnecessarily.
shard_id = item_hash % total_number_of_shards