simplettl

package module
v0.0.0-...-5a96d84 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2017 License: MIT Imports: 2 Imported by: 0

README

SimpleTTL

Coverage Status Build Status Go Report Card GitHub license GoDoc

Simple TTL on golang for map[string]interface{}, so keys is string and values is something.

See more detail: https://en.wikipedia.org/wiki/Time_to_live

Minimal example of using:

func main(){
	cache := simplettl.NewCache(2 * time.Second)
	key := "foo"
	value := "bar"
	cache.Add(key, value, time.Second)
	if r, ok := cache.Get(key); ok {
		fmt.Printf("Value for key %v is %v",key,r)
	}
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache - simple implementation of cache More information: https://en.wikipedia.org/wiki/Time_to_live

func NewCache

func NewCache(interval time.Duration) *Cache

NewCache - initialization of new cache. For avoid mistake - minimal time to live is 1 minute. For simplification, - key is string and cache haven`t stop method

Example
cache := simplettl.NewCache(2 * time.Second)
key := "foo"
value := "bar"
cache.Add(key, value, time.Second)
if r, ok := cache.Get(key); ok {
	fmt.Printf("Value for key %v is %v", key, r)
}
Output:

Value for key foo is bar

func (*Cache) Add

func (cache *Cache) Add(key string, value interface{}, ttl time.Duration)

Add - add key/value in cache

func (*Cache) Count

func (cache *Cache) Count() int

Count - return amount element of TTL map.

func (*Cache) Get

func (cache *Cache) Get(key string) (interface{}, bool)

Get - return value from cache

func (*Cache) GetKeys

func (cache *Cache) GetKeys() []interface{}

GetKeys - return all keys of cache map

Jump to

Keyboard shortcuts

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