Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/stirby/temp
Temporary structs and maps with expiring elements in Golang
https://github.com/stirby/temp
Last synced: about 11 hours ago
JSON representation
Temporary structs and maps with expiring elements in Golang
- Host: GitHub
- URL: https://github.com/stirby/temp
- Owner: stirby
- License: mit
- Created: 2016-07-04T03:39:18.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-07-19T23:10:20.000Z (about 1 year ago)
- Last Synced: 2024-06-22T04:53:13.294Z (3 months ago)
- Language: Go
- Homepage: http://godoc.org/github.com/ammario/temp
- Size: 7.81 KB
- Stars: 38
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Temp
Temporary structs and maps with expiring elements in Golang**Table of Contents**
- [Install](#install)
- [Basic Usage](#basic-usage)
- [Temporary struct](#temporary-struct)
- [Expiring map](#expiring-map)## Install
```go get gopkg.in/s-kirby/temp.v2```
# Basic Usage
## Temporary struct```go
type session struct {
ID string
temp.T
}func main() {
sess := session{}
temp.ExpireAfter(&sess, time.Second)
fmt.Printf("Session expired: %v\n", temp.Expired(&sess)) // false
time.Sleep(time.Second)
fmt.Printf("Session expired: %v\n", temp.Expired(&sess)) // true
}```
## Expiring map
```go
m := map[string]*session{
"123": &session{
ID: "123",
temp.T: temp.T{
expires: time.Now().Add(time.Second),
},
},
"124": &session{
ID: "124",
temp.T: temp.T{
expires: time.Now().Add(time.Second),
},
},
"125": &session{
ID: "125",
temp.T: temp.T{
expires: time.Now().Add(time.Second),
},
},
}
mutex := &sync.RWMutex{}
go temp.Clean(m, mutex, time.Millisecond*50, 0) //Clean blocks forever
time.Sleep(time.Second * 2)
//Map should be empty here
```