sweep

package module
v0.0.0-...-9e4e931 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2020 License: MIT Imports: 6 Imported by: 0

README

Sweep

Sweep is an in-memory cache. It uses sharding techinque to store millions of entries inside it. Even with millions of entries inside it the GC pause takes only a few milliseconds.

Note: It is a toy project, not intended to be used in production. I was practicing for an LLD interview and I ended up writing this.

Usage

package main

import (
	"fmt"
	"github.com/ataul443/sweep"
)

func main() {
	cache := sweep.Default()
	defer cache.Close()
	
	err := cache.Put("pikachu", []byte("value of pikachu"))
	if err != nil {
		panic(err)
	}
	
	val, err := cache.Get("pikachu")
	if err != nil {
		panic(err)
	}
	
	fmt.Println(string(val))
}

How GC Pause minimized ?

Starting GC Pause benchmark....
Going to put 20000000 entries in sweep.
Time took in inserting 20000000 entries in sweep: 18.93 seconds
GC Pause took: 85 milliseconds
Total entries in cache: 20000000

Instead of storing (key, val) pair directly in map[string]*entry, Sweep stores the val in a queue and stores the index in a map[uint64]uint64 as an (key, index) pair. Large map containing pointers causes huge GC pause time, however this is not same with map containing integers. You can read up more about this at issue-9477.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrClosed = errors.New("sweep closed")

ErrClosed is the error returned when sweep is closed already.

View Source
var ErrEntryNotFound = errors.New("entry not found")

ErrEntryNotFound is the error returned when an entry doesn't exist in sweep.

View Source
var ErrEntryTooLarge = errors.New("entry is too large in size")

ErrEntryTooLarge is the error returned when an entry is too large going to be put in sweep.

Functions

This section is empty.

Types

type Configuration

type Configuration struct {
	// ShardsCount represents a fixed number shards sweep will have.
	// This should be power of two. If it is not, then it will be set
	// to next power of two greater than current value.
	ShardsCount int

	// MaxShardSize represents the upper bound limit of a shard size in bytes.
	// This can be either 0 or should be power of two. If it is not,
	// then it will be set to next power of two greater than current
	// value. A zero value means no restriction on shard size.
	MaxShardSize int

	// EntryLifetime represents lifetime of an Entry in the sweep.
	EntryLifetime time.Duration

	// MaxEntrySize represents maximum size of Entry in bytes
	// in sweep can be stored
	MaxEntrySize int

	// CleanupInterval represents the waiting period between cleanup
	// cycles in sweep.
	CleanupInterval time.Duration
}

type Sweep

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

func Default

func Default() *Sweep

Default return's sweep with default Entry lifetime of 10 minutes and 1000 shards.

func New

func New(cfg Configuration) *Sweep

New return a sweep instance configured to given configuration.

func (*Sweep) Close

func (s *Sweep) Close() error

Close closes the sweep and removes all entries.

func (*Sweep) EntriesCount

func (s *Sweep) EntriesCount() int

EntriesCount returns number of current entries stored. This count includes those entries too which are expired but not cleaned up yet.

func (*Sweep) Get

func (s *Sweep) Get(key string) (value []byte, err error)

Get retrieves value associated with the key from the sweep.

func (*Sweep) Put

func (s *Sweep) Put(key string, value []byte) error

Put inserts the value associated with the key into the sweep.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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