shardcache

package module
v0.0.0-...-7a20aab Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 8, 2021 License: MIT Imports: 1 Imported by: 0

README

Shard Cache

Warning: Do not use this since it is still evolving.

Shard Cache is meant to remove having to lock the entire cache to write a single item to it. Instead the cache is set up into shared based on the key so there can be an unlimited amount of read locks and a write lock per shard. This lets reads be unblocked for all N-1 shards while a single item in a shard is being written.

// set up a cache with 64 shards, Shards MUST be a power of 2
sc := shardcache.New(64)

sc.Set(512, "my-512-value")
sc.Set(101, "my-101-value")

val, ok := sc.Get(101).(string)
if !ok {
    // derp
}

fmt.Println(val) // prints "my-101-value"

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ShardCache

type ShardCache struct {
	// contains filtered or unexported fields
}

ShardCache will keep track and shard keys based on the number of shards it holds around mutexes so this is safe for concurrent use

func New

func New(shards uint64) *ShardCache

New returns a ShardCache with the number of shards which must be a power of 2

func (*ShardCache) Delete

func (sc *ShardCache) Delete(key uint64)

Delete a key in the cache and use a write lock only for the shard that has the value leaving all other shards available for reads

func (*ShardCache) Get

func (sc *ShardCache) Get(key uint64) interface{}

Get a key from the cache and only use a read lock to access it

func (*ShardCache) Set

func (sc *ShardCache) Set(key uint64, value interface{})

Set a key in the cache and use a write lock only for the shard that will hold the value leaving all other shards available for reads

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL