Skip to content
stirbyPublic

About

Temporary structs and maps with expiring elements in Golang

Resources

Stars

38 stars

Watchers

1 watching

Forks

Latest commit

 

History

10 Commits

Folders and files

Repository files navigation

Temp

Temporary structs and maps with expiring elements in Golang

Table of Contents

Install

go get gopkg.in/s-kirby/temp.v2

Basic Usage

Temporary struct

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

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

About

Temporary structs and maps with expiring elements in Golang

Resources

Stars

38 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages