Coder Social home page Coder Social logo

concurrent-expiring-map's Introduction

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.