ttlmap

package module
Version: v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2021 License: Apache-2.0 Imports: 4 Imported by: 0

README

go-ttlmap

Build Status codecov Go Report Card GoDoc GitHub

Go TTL Map is a concurent map with entries expiring after a specified interval. This package requires go1.14 or newer.

Overview

This implementation of TTL Map creates a separate goroutine for each map entry that takes care of entry expiry.

PutXXX() methods take parent context as one of the parameters and entries are safely removed from the map when associated context is canceled.

This design can potentially create a race conditions but measures had been taken to address this issue. In particular, random delay in 0-100,000us range is added to every TTL to reduce probability of race condition when large number of entries is added to the map in a rapid succession.

A similar condition may occur on context cancellation but impact of it is rather negligeable.

ToDo

  • Provide code examples
  • Improve documentation
  • Develop better tests for concurency and race conditions

License

See LICENSE.

Documentation

Overview

Package ttlmap is a map with entries that expire after a specified interval

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TTLMap

type TTLMap struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

TTLMap is obscure context

func New

func New(ttl time.Duration) *TTLMap

New creates a new TTLMap with a specified default TTL

func (*TTLMap) Clear

func (t *TTLMap) Clear()

Clear cancels any pending operations and clears the content of the map

func (*TTLMap) Get

func (t *TTLMap) Get(key string) (interface{}, bool)

Get returns a value from the map with a specified key or nil if one not present.

func (*TTLMap) Len

func (t *TTLMap) Len() int

Len returns number of items in the map

func (*TTLMap) Put

func (t *TTLMap) Put(ctx context.Context, key string, val interface{})

Put inserts a new value with a specified key into the map or replaces an existing one. It uses the default TTL specified during the initialization.

func (*TTLMap) PutWithTTL

func (t *TTLMap) PutWithTTL(ctx context.Context, key string, val interface{}, ttl time.Duration)

PutWithTTL inserts a new value with a specified key and TTL into the map or replaces an existing one.

func (*TTLMap) TestAndPut

func (t *TTLMap) TestAndPut(ctx context.Context, key string, val interface{}) bool

TestAndPut inserts a new value with a specified key into the map only if none exists. Otherwise, it does nothing and returns `false`. It uses the default TTL specified during the initialization.

func (*TTLMap) TestAndPutWithTTL

func (t *TTLMap) TestAndPutWithTTL(ctx context.Context, key string, val interface{}, ttl time.Duration) bool

TestAndPutWithTTL inserts a new value with a specified key and TTL into the map only if none exists. Otherwise, it does nothing and returns `false`.

Jump to

Keyboard shortcuts

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