syncmap

package
v0.0.0-...-1e780ff Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2015 License: MIT, MIT Imports: 3 Imported by: 2

README

syncmap

GoDoc Build Status

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 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) 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