Skip to content

A sharded, concurrent key-value store (cache) library for Go, suitable for single-machine applications.

License

Notifications You must be signed in to change notification settings

korzepadawid/cacher

Repository files navigation

Go Reference tests

cacher

A sharded, concurrent key-value store (cache) library for Go, suitable for single-machine applications.

Installation

$ go get github.com/korzepadawid/cacher 

Usage

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}
}

How does it work?

Hashing

The library uses the dbj2 algorithm for generating string hashes.

Sharding

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.

Choosing a shard

shard_id = item_hash % total_number_of_shards

About

A sharded, concurrent key-value store (cache) library for Go, suitable for single-machine applications.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages