memtable

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2021 License: MIT Imports: 5 Imported by: 0

README

Library for Consistent Cache

Go Report Card Coverage Status

Based on the freecache library and the 'leasing' mechanism of the paper Scaling Memcache at Facebook.

Usage

cacheSize := 100 * 1024 * 1024 // 100MB
cache := memtable.New(cacheSize)

for {
    key := []byte("some-key")
    result := cache.Get(key)
    if result.Status == memtable.GetStatusLeaseRejected {
    	time.Sleep(100 * time.Millisecond)
    	continue
    }
    
    if result.Status == memtable.GetStatusFound {
    	// cache hit
    	fmt.Println("Got value:", result.Value)
    	return
    }
    
    // cache miss but lease is granted
    // get data from database
    value := []byte("some-value")
    
    affected := cache.Set(key, result.LeaseID, value)
    fmt.Println("Affected:", affected)
    return
}

License

The MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GetResult

type GetResult struct {
	Value   []byte
	LeaseID uint32
	Status  GetStatus
}

GetResult for result when calling Get

type GetStatus

type GetStatus int

GetStatus for cache Get

const (
	// GetStatusFound for normal cache hit case
	GetStatusFound GetStatus = iota
	// GetStatusLeaseGranted when cache miss and lease is granted
	GetStatusLeaseGranted
	// GetStatusLeaseRejected when cache miss and lease is not granted
	GetStatusLeaseRejected
)

type Memtable

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

Memtable ...

func New

func New(memsize int, options ...Option) *Memtable

New ...

func (*Memtable) Get

func (m *Memtable) Get(key []byte) GetResult

Get value from the cache

func (*Memtable) GetUnsafeInnerCache added in v0.1.2

func (m *Memtable) GetUnsafeInnerCache() *freecache.Cache

GetUnsafeInnerCache returns the freecache

func (*Memtable) Invalidate

func (m *Memtable) Invalidate(key []byte) (affected bool)

Invalidate an entry from the cache

func (*Memtable) Set

func (m *Memtable) Set(key []byte, leaseID uint32, value []byte) (affected bool)

Set value to the cache

type Option

type Option func(opts *memtableOptions)

Option ...

func WithLeaseListSize

func WithLeaseListSize(n uint32) Option

WithLeaseListSize configures the number of entries in a lease list

func WithLeaseTimeout

func WithLeaseTimeout(d uint32) Option

WithLeaseTimeout for duration of lease timeout, in second

func WithNumBuckets

func WithNumBuckets(n uint32) Option

WithNumBuckets configures number of lease buckets

Jump to

Keyboard shortcuts

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