expirables

package module
v0.0.0-...-7f9869f Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2023 License: Apache-2.0 Imports: 2 Imported by: 0

README

Go Expirables

Expirables variables in Golang.

Installation

To install this as a dependency, simply call : go get github.com/steve-nzr/expirables

Usage

How to use of this package :

package main

import (
	"fmt"
	"time"

	"github.com/google/uuid"
	"github.com/steve-nzr/expirables"
)

var token = expirables.NewExpirable(func() interface{} {
	return uuid.New().String()
}, time.Second*4)

func main() {
	val := token.Get().(string)

	if uuid.MustParse(token.Get().(string)).String() == val {
		fmt.Printf("'%s' is an UUID !\n", val)
	}

	time.Sleep(time.Second * 4)

	val = token.Get().(string)

	if uuid.MustParse(token.Get().(string)).String() == val {
		fmt.Printf("'%s' stills an UUID (but is another) !\n", val)
	}
}

Which outputs (UUID are examples) :

user@deb:~$ go run main.go 
'c1b4b6da-fdcf-4644-a207-8a95e751b9d8' is an UUID !
'd1c208a2-c5e4-4c64-b553-1cd5afafd00c' stills an UUID !

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Expirable

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

Expirable in-memory variable Its value is an interface{} that is refreshed every {ttl} with the given refresher

func NewExpirable

func NewExpirable(refresher ExpirableRefresher, TTL time.Duration) *Expirable

NewExpirable creates a new Expirable variable with the given Refresher & TTL

func (*Expirable) Get

func (v *Expirable) Get() interface{}

Get the value of the stored variable. Calling this function could trigger a refresh on the value and potentially slow the function execution

func (*Expirable) GetSync

func (v *Expirable) GetSync() interface{}

Get the value of the stored variable. Calling this function could trigger a synchronous refresh on the value (blocking the current calling context) and potentially slow the function execution

type ExpirableRefresher

type ExpirableRefresher func() interface{}

ExpirableRefresher is the function that is called each refresh of the Expirable Variable

Jump to

Keyboard shortcuts

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