cachedmap

package module
v0.0.0-...-358d5cd Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2021 License: MIT Imports: 4 Imported by: 0

README

cachedmap

A Go string keyed object cache with entry timeout and periodic flushing.

Cachedmap provides the equivalent of a cached map[string]interface{} store with per record timeouts.

Overall cache growth is managed by a periodic global flush of entire cache. For this reason it may be more suitable for applications where you expect a high hit rate but when a miss is not really so expensive. Or if you know that growth is not going to be a problem. In that case you could set the global flush period to some high value.

Usage example

cm := cachedmap.NewCachedMap(5*time.Second, 5*time.Minute, nil)

cm.Set("cake", &monkey)

...

m, ok := cm.Get("cake")
if ok {
    freshMonkey = (m).(*monkeyType)
}

See the examples directory for more.

Alternatives

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheEntry

type CacheEntry struct {
	Data       interface{}
	RemoveTime time.Time
}

type CachedMap

type CachedMap struct {
	Name      string
	Hits      int64
	Misses    int64
	Writes    int64
	Flushes   int64
	MaxLength int
	// contains filtered or unexported fields
}

func NewCachedMap

func NewCachedMap(name string, keyTimeout, flushCycle time.Duration, log *logrus.Entry) *CachedMap

func (*CachedMap) Get

func (c *CachedMap) Get(key string) (interface{}, bool)

Get returns a non-expired entry and true. If no valid entry is found, it returns (nil, false).

func (*CachedMap) GetStats

func (c *CachedMap) GetStats() Stats

GetStats returns current stats in a convenient, loggable, struct.

func (*CachedMap) Len

func (c *CachedMap) Len() int

Len returns the number of items in the map.

func (*CachedMap) Set

func (c *CachedMap) Set(key string, data interface{}) time.Time

Set adds an item to the map with a computed remove time. It returns the remove time in case you want to use it in a nearby SetUntil() call.

func (*CachedMap) SetLog

func (c *CachedMap) SetLog(log *logrus.Entry)

SetLog sets the logger used by this component and adds a component identifier. The instance name will be logged via the GetStats() call.

func (*CachedMap) SetUntil

func (c *CachedMap) SetUntil(key string, data interface{}, removeTime time.Time)

SetUntil adds an item to the map with the given remove time. This allows you to override the default key timeout.

type Stats

type Stats struct {
	Name       string `json:"name"`
	Hits       int64  `json:"hits"`
	Misses     int64  `json:"misses"`
	Writes     int64  `json:"writes"`
	Flushes    int64  `json:"flushes"`
	MaxLength  int    `json:"max_length"`
	Length     int    `json:"length"`
	KeyTTL     int64  `json:"key_ttl"`
	FlushCycle int64  `json:"flush_cycle"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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