cmap

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2023 License: MIT Imports: 5 Imported by: 2

README

cmap

MIT License GoDoc Go Report Card Releases

cmap is inspired by orcaman/concurrent-map, with performance improvements and some usable methods, while keeping same use cases.

Installation

$ go get github.com/octu0/cmap

Example

import "github.com/octu0/cmap

var (
  m = cmap.New()
)

func main() {
  m.Set("foo", "bar")

  if v, ok := m.Get("foo"); ok {
    bar := v.(string)
  }

  m.Remove("foo")
}

Benchmarks

goos: darwin
goarch: amd64
pkg: github.com/octu0/cmap
cpu: Intel(R) Core(TM) i7-8569U CPU @ 2.80GHz
BenchmarkCompare/ConcurrentMap-8         	     496	   2806827 ns/op	  735840 B/op	    5153 allocs/op
BenchmarkCompare/sync.Map-8              	     252	   4720666 ns/op	  852128 B/op	   25158 allocs/op
BenchmarkCompare/cmap-8                  	     799	   1506810 ns/op	  468819 B/op	    5011 allocs/op
PASS

License

MIT, see LICENSE file for details.

Documentation

Index

Constants

View Source
const (
	AppName string = "cmap"
	Version string = "1.0.3"
)

Variables

This section is empty.

Functions

func WithCacheCapacity

func WithCacheCapacity(size int) cmapOptionFunc

func WithHashFunc

func WithHashFunc(hashFunc CMapHashFunc) cmapOptionFunc

func WithSlabSize

func WithSlabSize(size int) cmapOptionFunc

Types

type CMap

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

func New

func New(funcs ...cmapOptionFunc) *CMap

func (*CMap) Get

func (c *CMap) Get(key string) (interface{}, bool)

func (*CMap) GetRLocked added in v1.0.2

func (c *CMap) GetRLocked(key string, fn GetFunc) interface{}

func (*CMap) Keys

func (c *CMap) Keys() []string

func (*CMap) Len

func (c *CMap) Len() int

func (*CMap) Remove

func (c *CMap) Remove(key string) (interface{}, bool)

func (*CMap) RemoveIf

func (c *CMap) RemoveIf(key string, fn RemoveIfFunc) (removed bool)

func (*CMap) Set

func (c *CMap) Set(key string, value interface{})

func (*CMap) SetIf added in v1.0.3

func (c *CMap) SetIf(key string, fn SetIfFunc)

func (*CMap) SetIfAbsent

func (c *CMap) SetIfAbsent(key string, value interface{}) (updated bool)

func (*CMap) Upsert

func (c *CMap) Upsert(key string, fn UpsertFunc) (newValue interface{})

type CMapHashFunc

type CMapHashFunc interface {
	Hash64(string) uint64
}

func NewFNV64HashFun

func NewFNV64HashFun() CMapHashFunc

func NewXXHashFunc

func NewXXHashFunc() CMapHashFunc

type Cache

type Cache interface {
	Lock()
	Unlock()

	RLock()
	RUnlock()

	Set(string, interface{})
	Get(string) (interface{}, bool)
	Remove(string) (interface{}, bool)

	Len() int
	Keys() []string
}

type GetFunc added in v1.0.2

type GetFunc func(exists bool, value interface{}) (processedValue interface{})

type RemoveIfFunc

type RemoveIfFunc func(exists bool, value interface{}) bool

type SetIfFunc added in v1.0.3

type SetIfFunc func(exists bool, value interface{}) (newValue interface{}, isSetValue bool)

type UpsertFunc

type UpsertFunc func(exists bool, oldValue interface{}) (newValue interface{})

Jump to

Keyboard shortcuts

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