ttlmap

package module
v0.0.0-...-91fd36b Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2017 License: Apache-2.0 Imports: 4 Imported by: 0

README

Build Status Build Status

TtlMap

Redis-like Map with expiry times and maximum capacity


import "github.com/mailgun/ttlmap"

mh, _ := ttlmap.NewMap(20)
mh.Set("key1", "value", 20)
valI, exists := mh.Get("key2")
if exists {
   val := valI.(string)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Callback

type Callback func(key string, el interface{})

Callback can be executed on the element once TTLMap performs some action on the dictionary

type Option

type Option func(m *TTLMap) error

Option is a functional option argument to TTLMap

func CallOnExpire

func CallOnExpire(cb Callback) Option

CallOnExpire will call this callback on expiration of elements

func Clock

func Clock(clock clockwork.Clock) Option

Clock sets the time provider clock, handy for testing

type TTLMap

type TTLMap struct {
	Clock clockwork.Clock
	// contains filtered or unexported fields
}

TTLMap is a map with expiring elements

func New

func New(capacity int, opts ...Option) (*TTLMap, error)

New returns new instance of TTLMap

func (*TTLMap) Get

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

Get returns element value, does not update TTL

func (*TTLMap) GetInt

func (m *TTLMap) GetInt(key string) (int, bool, error)

GetInt returns integer element value stored in map, does not update TTL

func (*TTLMap) Increment

func (m *TTLMap) Increment(key string, value int, ttl time.Duration) (int, error)

Increment increment element's value and updates it's TTL

func (*TTLMap) Len

func (m *TTLMap) Len() int

Len returns amount of elements in the map

func (*TTLMap) Pop

func (m *TTLMap) Pop() (string, interface{}, bool)

Pop removes and returns the key and the value of the oldest element from the TTL Map if there are no elements in the map, returns empty string and false

func (*TTLMap) Remove

func (m *TTLMap) Remove(key string) (interface{}, bool)

Remove removes and returns element if it's found, returns element, true if it's found returns nil, false if element is not found

func (*TTLMap) RemoveExpired

func (m *TTLMap) RemoveExpired(iterations int) int

RemoveExpired makes a pass through map and removes expired elements

func (*TTLMap) Set

func (m *TTLMap) Set(key string, value interface{}, ttl time.Duration) error

Set sets the value of the element by key to value and sets time to expire to TTL. Resolution is 1 second, min value is one second

Jump to

Keyboard shortcuts

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