expiremap

package module
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2020 License: MIT Imports: 2 Imported by: 4

README

expiremap

synchronization map with expiration date (extended sync.Map)

Description

expiremap.Map provides a func that is compatible with sync.Map and an extended func.

Installation

This package can be installed with the go get command:

$ go get github.com/vvatanabe/expiremap

Usage

package main

import (
	"fmt"
	"time"

	"github.com/vvatanabe/expiremap"
)

func main() {
	var m expiremap.Map

	// SetDefaultExpire sets default expiration for value in Map.
	m.SetDefaultExpire(time.Second / 2)

	// Store sets the value for a key with default expiration.
	m.Store("key1", "foo")

	// Load returns the value in expiration stored in the map for a key,
	// or nil if no value is present.
	v, ok := m.Load("key1")
	if !ok {
		return
	}
	fmt.Println("key1:", v)

	// Store with expire sets the value for a key with expiration.
	m.Store("key2", "bar", expiremap.Expire(time.Second / 2))
	v, ok = m.Load("key2")
	if !ok {
		return
	}
	fmt.Println("key2:", v)

	// Wait for expiration
	time.Sleep(time.Second)

	_, ok = m.Load("key1")
	if !ok {
		fmt.Println("key1 expired")
	}
	_, ok = m.Load("key2")
	if !ok {
		fmt.Println("key2 expired")
	}
}

Bugs and Feedback

For bugs, questions and discussions please use the Github Issues.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Map

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

Map is synchronization map with expiration date (extended sync.Map).

func (*Map) Delete

func (m *Map) Delete(key interface{})

Delete deletes the value for a key.

func (*Map) Load

func (m *Map) Load(key interface{}) (value interface{}, ok bool)

Load returns the value in expiration stored in the map for a key, or nil if no value is present.

func (*Map) LoadOrStore

func (m *Map) LoadOrStore(key, value interface{}, opts ...Option) (actual interface{}, loaded bool)

LoadOrStore returns the existing value in expiration for the key if present. Otherwise, it stores with expiration and returns the given value.

func (*Map) Range

func (m *Map) Range(f func(key, value interface{}) bool)

Range calls f sequentially for each key and value in expiration present in the map.

func (*Map) SetDefaultExpire

func (m *Map) SetDefaultExpire(expire time.Duration)

SetDefaultExpire sets default expiration for value in Map.

func (*Map) Store

func (m *Map) Store(key, value interface{}, opts ...Option)

Store sets the value for a key with default expiration and some options.

type Option added in v0.10.0

type Option func(*Options)

func Expire added in v0.10.0

func Expire(expire time.Duration) Option

func ExpiredFunc added in v0.10.0

func ExpiredFunc(f func()) Option

type Options added in v0.10.0

type Options struct {
	Expire      time.Duration
	ExpiredFunc func()
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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