Skip to content

oaStuff/timedMap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

timedMap

timedMap is simple go 1.9 sync.map with timing added. This implies that timedMap just stores items in a map and expires them automatically based on the time duration set.

usage

Import the package:

import (
	"github.com/oaStuff/timedMap"
)
go get "github.com/oaStuff/timedMap"

example

        //create the map
	    tm := timedMap.NewTimeMap(time.Second * 3, nil) // we want items to live in the map for only 3 seconds
    	tm.Add("one","1")
    	tm.Add("two","2")
    	tm.Add("three", "3")

    	val := tm.Get("one")
    	fmt.Println("value is " + val)

another example

    type user struct {
        name string
        age int
    }
    
    tm := timedMap.NewTimeMap(time.Second * 3, nil)
	tm.Add("john", &user{"john mark", 30})
	tm.Add("mary", &user{"mary jane", 26})
	tm.Add("paul", &user{"paul frank", 19})

	val = tm.Get("mary")
	if val != nil {
	    fmt.Println(val.(*user).name)
	    fmt.Println(val.(*user).age)
	}

The library also supports eviction callback when an item in the map expires.

    type user struct {
        name string
        age int
    }
    tm := timedMap.NewTimeMap(time.Second * 3, func(key, value interface{}) {
        fmt.Println("expried callback:")
        fmt.Printf("%+v\n", key)
        fmt.Printf("%+v\n", value)
    })
    
	tm.Add("john", &user{"john mark", 30})
	tm.Add("mary", &user{"mary jane", 26})
	tm.Add("paul", &user{"paul frank", 19})

    time.Sleep(time.Second * 5)
	val = tm.Get("mary")
	if val != nil {
	    fmt.Println(val.(*user).name)
	    fmt.Println(val.(*user).age)
	}

license

MIT (see LICENSE file)

About

A go implementation of timed map with automatic items expiration (golang)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages