cmap

package module
v0.0.0-...-d099edd Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2018 License: MIT Imports: 2 Imported by: 0

README

Concurrent Map

A thread-safe map for the Go programming language.

License ReportCard GoDoc

Install

$ go get -u github.com/LyricTian/cmap

Usage

package main

import (
	"fmt"

	"github.com/LyricTian/cmap"
)

func main() {
	m := cmap.NewShardMap()
	// or
	// m := cmap.NewMap()
	m.Set("foo", "bar")
	if v, ok := m.Get("foo"); ok {
		fmt.Println("foo=", v.(string))
	}
	m.Remove("foo")
}

Benchmark

BenchmarkMapSet-8                	 1000000	      1032 ns/op
BenchmarkParallelMapSet-8        	 1000000	      1217 ns/op
BenchmarkShardMapSet-8           	 2000000	       854 ns/op
BenchmarkParallelShardMapSet-8   	 5000000	       395 ns/op

MIT License

Copyright (c) 2016 Lyric

Documentation

Index

Constants

View Source
const (
	// DefaultShartCount The default number of shard
	DefaultShartCount = 32
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Map

type Map struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Map A thread-safe anything to anything map

func NewMap

func NewMap() *Map

NewMap create a new map

func (*Map) Clear

func (m *Map) Clear()

Clear removes all elements from the map

func (*Map) Count

func (m *Map) Count() (count int)

Count the number of elements within the map

func (*Map) Get

func (m *Map) Get(key interface{}) (value interface{}, ok bool)

Get retrieves an element from map under given key

func (*Map) Items

func (m *Map) Items() (items map[interface{}]interface{})

Items returns all items as map[interface{}]interface{}

func (*Map) Remove

func (m *Map) Remove(key interface{})

Remove removes an element from the map

func (*Map) Set

func (m *Map) Set(key, value interface{})

Set the given value under the specified key

type ShardMap

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

ShardMap A thread-safe string to anything shard map, the distribution of keys used FNV-1a hash algorithm

func NewShardMap

func NewShardMap(shardcount ...uint32) *ShardMap

NewShardMap create a new shard map

func (*ShardMap) Clear

func (sm *ShardMap) Clear()

Clear removes all elements from the map

func (*ShardMap) Count

func (sm *ShardMap) Count() (count int)

Count the number of elements within the map

func (*ShardMap) Get

func (sm *ShardMap) Get(key string) (value interface{}, ok bool)

Get retrieves an element from map under given key

func (*ShardMap) Items

func (sm *ShardMap) Items() (items map[string]interface{})

Items returns all items as map[string]interface{}

func (*ShardMap) Remove

func (sm *ShardMap) Remove(key string)

Remove removes an element from the map

func (*ShardMap) Set

func (sm *ShardMap) Set(key string, value interface{})

Set the given value under the specified key

Jump to

Keyboard shortcuts

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