Skip to content
A map which has expiring key-value pairs
Branch: master
Clone or download
Latest commit 8fefa6c Feb 20, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples updated example and readme Feb 14, 2019
scripts updated example and readme Feb 14, 2019
.travis.yml init Jan 30, 2019
LICENCE init Jan 30, 2019
README.md Update README.md Feb 20, 2019
timedmap.go optimized concurrency safety Feb 18, 2019
timedmap_test.go updated tests Feb 14, 2019

README.md

~ timedmap ~

A map which has expiring key-value pairs.

       

go get github.com/zekroTJA/timedmap

Intro

This package allows to set values to a map which will expire and disappear after a specified time.

Here you can read the docs of this package, generated by godoc.org.


Usage Example

package main

import (
	"log"
	"time"

	"github.com/zekroTJA/timedmap"
)

func main() {
	// Create a timed map with a cleanup timer interval of 1 second
	tm := timedmap.New(1 * time.Second)
	// Set value of key "hey" to 213, which will expire after 3 seconds
	tm.Set("hey", 213, 3*time.Second)
	// Print the value of "hey"
	printKeyVal(tm, "hey")
	// Block the main thread for 5 seconds
	// After this time, the key-value pair "hey": 213 has expired
	time.Sleep(5 * time.Second)
	// Now, this function should show that there is no key "hey"
	// in the map, because it has been expired
	printKeyVal(tm, "hey")
}

func printKeyVal(tm *timedmap.TimedMap, key interface{}) {
	d := tm.GetValue(key)
	if d == nil {
		log.Println("data expired")
		return
	}

	dInt := d.(int)
	log.Printf("%v = %d\n", key, dInt)
}

Further examples, you can find in the example directory.


Copyright (c) 2019 zekro Development (Ringo Hoffmann).
Covered by MIT licence.

You can’t perform that action at this time.