ttlmap

package module
v0.0.0-...-595051d Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2017 License: MIT Imports: 3 Imported by: 0

README

ttlmap

Simple Golang package to handle maps which has items with expiration

Build Status Code Climate Coverage Status

Documentation

Index

Constants

View Source
const (
	// Never is a helper const for easy to add item to map which never expires.
	Never = time.Duration(-11223344)
)

Variables

View Source
var (
	// ErrKeyAlreadyExists happens, when try to add an item with a key which is
	// already exists in the map.
	ErrKeyAlreadyExists = errors.New("key is already exists")

	// ErrNilKeyIsNotAcceptable happens, when someone try to operate with nil
	// valued key.
	ErrNilKeyIsNotAcceptable = errors.New("nil key is not acceptable")

	// ErrItemNotFound happens, when try to update an item, but it not
	// found by key.
	ErrItemNotFound = errors.New("key is not exists")

	// ErrItemIsExpired happens, when the item is exists in the map, but already
	// expired (gc is not removed yet)
	ErrItemIsExpired = errors.New("item is expired")
)

Functions

This section is empty.

Types

type TTLMap

type TTLMap interface {
	// Insert a new element to the map. If the key is exists, return with an
	// "ErrKeyExists" error, and when key is nil then return with
	// "ErrNilKeyIsNotAcceptable" error.
	// If the expiration is "ttlmap.Never" the item is never expired.
	Insert(key, value interface{}, expiration time.Duration) error

	// Update is an existing item's value and expiration. If the item is not
	// found by key, then return with an "ErrItemNotFound" error.
	// If the expiration is "ttlmap.Never" remove the expiration from the element.
	Update(key, value interface{}, expiration time.Duration) error

	// Has is just checking the given key is exists in the current map or not.
	Has(key interface{}) bool

	// Get an item from the map by key. If the item is not found, then return
	// with an "ErrItemNotFound" error, otherwise the item is exists, but
	// already expired, then return an "ErrItemIsExpired" error.
	// But when everythig is ok, return with the requested item of course.
	Get(key interface{}) (interface{}, error)

	// Remove an item from the map by key. If the item is not found, then return
	// with an "ErrItemNotFound" error.
	Remove(key interface{}) error
}

TTLMap is the main public interface type of package. A TTLMap contains a map with elements which has expiration time.

func New

func New() TTLMap

New is instantiate a TTLMap. Every new TTLMap is fully empty, so not contains items.

Jump to

Keyboard shortcuts

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