cmap

package module
v0.0.0-...-25cb195 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2015 License: MIT Imports: 3 Imported by: 0

README

concurrent map Circle CI

Forked from original project: https://github.com/streamrail/concurrent-map. I've modified the functionality to add item expiry, and the values are now just byte slices - it's getting to be a little bit more like an in-process, thread-safe memcache.

Golang map doesn't support concurrent reads and writes, Please see (http://golang.org/doc/faq#atomic_maps and http://blog.golang.org/go-maps-in-action), in case you're using multiple Go routines to read and write concurrently from a map some form of guard mechanism should be in-place.

Concurrent map is a wrapper around Go's map, more specifically around a String -> interface{} kinda map, which enforces concurrency.

usage

Import the package:

import (
	"github.com/growse/concurrent-expiring-map"
)

and go get it using the goapp gae command:

goapp get "github.com/growse/concurrent-expiring-map"

The package is now imported under the "cmap" namespace.

example


	// Create a new map.
	map := cmap.New()
	
	// Sets item within map, sets "bar" under key "foo", expiring 5 hours from now
	map.Set("foo", []byte("bar"), time.Now().Add(time.Hour*5)

	// Retrieve item from map.
	tmp, ok := map.Get("foo")

	// Checks if item exists
	if ok {
		// Map stores items as ConcurrentMapItem, which contains the expiry and the byte slice
		bar := string(tmp.Value)
	}

	// Removes item under key "foo"
	map.Remove("foo")

For more examples have a look at concurrent_map_test.go.

Running tests:

go test "github.com/growse/concurrent-expiring-map"

license

MIT (see LICENSE file)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var SHARD_COUNT = 32

Functions

func ExpireAll

func ExpireAll()

Types

type ConcurrentMap

type ConcurrentMap []*ConcurrentMapShared

A "thread" safe map of type string:Anything. To avoid lock bottlenecks this map is dived to several (SHARD_COUNT) map shards.

func New

func New() ConcurrentMap

Creates a new concurrent map.

func (ConcurrentMap) Count

func (m ConcurrentMap) Count() int

Returns the number of elements within the map.

func (ConcurrentMap) Flush

func (m ConcurrentMap) Flush()

func (ConcurrentMap) Get

func (m ConcurrentMap) Get(key string) ([]byte, bool)

Retrieves an element from map under given key.

func (ConcurrentMap) GetShard

func (m ConcurrentMap) GetShard(key string) *ConcurrentMapShared

Returns shard under given key

func (*ConcurrentMap) Has

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

Looks up an item under specified key

func (*ConcurrentMap) IsEmpty

func (m *ConcurrentMap) IsEmpty() bool

Checks if map is empty.

func (ConcurrentMap) Iter

func (m ConcurrentMap) Iter() <-chan Tuple

Returns an iterator which could be used in a for range loop.

func (ConcurrentMap) IterBuffered

func (m ConcurrentMap) IterBuffered() <-chan Tuple

Returns a buffered iterator which could be used in a for range loop.

func (*ConcurrentMap) Remove

func (m *ConcurrentMap) Remove(key string)

Removes an element from the map.

func (*ConcurrentMap) Set

func (m *ConcurrentMap) Set(key string, value []byte, expiresAt time.Time)

Sets the given value under the specified key.

type ConcurrentMapItem

type ConcurrentMapItem struct {
	Expires time.Time
	Value   []byte
}

type ConcurrentMapShared

type ConcurrentMapShared struct {
	sync.RWMutex // Read Write mutex, guards access to internal map.
	// contains filtered or unexported fields
}

A "thread" safe string to anything map.

type Tuple

type Tuple struct {
	Key string
	Val ConcurrentMapItem
}

Used by the Iter & IterBuffered functions to wrap two variables together over a channel,

Jump to

Keyboard shortcuts

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