https://github.com/oastuff/timedmap
A go implementation of timed map with automatic items expiration (golang)
https://github.com/oastuff/timedmap
Last synced: 9 months ago
JSON representation
A go implementation of timed map with automatic items expiration (golang)
- Host: GitHub
- URL: https://github.com/oastuff/timedmap
- Owner: oaStuff
- Created: 2017-11-16T11:00:57.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-11-26T20:14:32.000Z (over 8 years ago)
- Last Synced: 2024-06-21T04:41:49.807Z (about 2 years ago)
- Language: Go
- Size: 3.91 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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:
```go
import (
"github.com/oaStuff/timedMap"
)
```
```bash
go get "github.com/oaStuff/timedMap"
```
## example
```go
//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
```go
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.
```go
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](https://github.com/orcaman/concurrent-map/blob/master/LICENSE) file)