temp

package module
v2.0.0+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 7, 2016 License: MIT Imports: 4 Imported by: 4

README

Temp

Temporary structs and maps with expiring elements in Golang

Table of Contents

Install

go get gopkg.in/ammario/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

Documentation

Overview

Package temp provides the ability to have temporary structs and simple cleaning of slices and maps containing temporary data

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clean

func Clean(m interface{}, mutex *sync.RWMutex, scanInterval time.Duration, checkInterval time.Duration)

Clean blocks forever and removes expires keys from a map with values that adhere to Temporary. Clean panics if m is not a map or slice. Clean makes heavy use of reflection. scanInterval is slept between whole scans of a map, if zero it will block the thread. checkInterval is slept between checks of individual elements.

func ExpireAfter

func ExpireAfter(t Temporary, dur time.Duration)

ExpireAfter sets a T to expire after a duration from now

func Expired

func Expired(t Temporary) bool

Expired checks if a Temporary has expired

Types

type T

type T struct {
	// contains filtered or unexported fields
}

T is meant to be inherited by instances intended to expire

func (*T) Expires

func (t *T) Expires() time.Time

Expires returns when T expires

func (*T) SetExpires

func (t *T) SetExpires(ti time.Time)

SetExpires sets the expire time for a T

type Temporary

type Temporary interface {
	Expires() time.Time
	SetExpires(ti time.Time)
}

Temporary defines the necessary methods used by an expiring object

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL