syncmap

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

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

Go to latest
Published: Jun 1, 2020 License: MIT Imports: 3 Imported by: 2

README

DEPRECATED syncmap

GoDoc Build Status

THIS PACKAGE IS DEPRECATED, PLEASE USE THE sync.Map PROVIDED BY GO STANDARD LIBRARY.

A thread safe map implementation for Golang

Usage

Install with:

go get github.com/DeanThompson/syncmap

Example:

import (
    "fmt"

    "github.com/DeanThompson/syncmap"
)

func main() {
    m := syncmap.New()
    m.Set("one", 1)
    v, ok := m.Get("one")
    fmt.Println(v, ok)  // 1, true

    v, ok = m.Get("not_exist")
    fmt.Println(v, ok)  // nil, false

    m.Set("two", 2)
    m.Set("three", "three")

    for item := range m.IterItems() {
        fmt.Println("key:", item.Key, "value:", item.Value)
    }
}

Documentation

Overview

A thread safe map implementation for Golang

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Item

type Item struct {
	Key   string
	Value interface{}
}

Item is a pair of key and value

type IterItemFunc

type IterItemFunc func(item *Item)

IterItemFunc is the type of the function called for every item.

Don't modify the SyncMap in this function, or maybe leads to deadlock.

type IterItemWithBreakFunc

type IterItemWithBreakFunc func(item *Item) bool

IterItemWithBreakFunc is the type of the function called for each item.

If false is returned,each item stops. Don't modify the SyncMap in this function, or maybe leads to deadlock.

type IterKeyFunc

type IterKeyFunc func(key string)

IterKeyFunc is the type of the function called for every key.

Don't modify the SyncMap in this function, or maybe leads to deadlock.

type IterKeyWithBreakFunc

type IterKeyWithBreakFunc func(key string) bool

IterKeyWithBreakFunc is the type of the function called for each key.

If false is returned,each key stops. Don't modify the SyncMap in this function, or maybe leads to deadlock.

type SyncMap

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

SyncMap keeps a slice of *syncMap with length of `shardCount`. Using a slice of syncMap instead of a large one is to avoid lock bottlenecks.

func New

func New() *SyncMap

Create a new SyncMap with default shard count.

func NewWithShard

func NewWithShard(shardCount uint8) *SyncMap

Create a new SyncMap with given shard count. NOTE: shard count must be power of 2, default shard count will be used otherwise.

func (*SyncMap) Delete

func (m *SyncMap) Delete(key string)

Removes an item

func (*SyncMap) EachItem

func (m *SyncMap) EachItem(iter IterItemFunc)

func (*SyncMap) EachItemWithBreak

func (m *SyncMap) EachItemWithBreak(iter IterItemWithBreakFunc)

func (*SyncMap) EachKey

func (m *SyncMap) EachKey(iter IterKeyFunc)

func (*SyncMap) EachKeyWithBreak

func (m *SyncMap) EachKeyWithBreak(iter IterKeyWithBreakFunc)

func (*SyncMap) Flush

func (m *SyncMap) Flush() int

Wipes all items from the map

func (*SyncMap) Get

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

Retrieves a value

func (*SyncMap) Has

func (m *SyncMap) Has(key string) bool

Whether SyncMap has the given key

func (*SyncMap) IterItems

func (m *SyncMap) IterItems() <-chan Item

Return a channel from which each item (key:value pair) in the map can be read

func (*SyncMap) IterKeys

func (m *SyncMap) IterKeys() <-chan string

Returns a channel from which each key in the map can be read

func (*SyncMap) Pop

func (m *SyncMap) Pop() (string, interface{})

Pop delete and return a random item in the cache

func (*SyncMap) Set

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

Sets value with the given key

func (*SyncMap) Size

func (m *SyncMap) Size() int

Returns the number of items

Jump to

Keyboard shortcuts

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