Help us shape the future of Go! Take the 2026 Go Developer Survey

slicecachettl

package module
v0.0.0-...-bba468d Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2019 License: MIT Imports: 3 Imported by: 0

README

slicecachettl

Simple performant ttl cache, w/ constant-time insertions and expirations.

It's for a vary narrow use case to be a part of an online data joiner. It can be used for online joining labels (click/no click) to training inputs.

For context, you can read about joiner in e.g.

X. He, J. Pan, O. Jin, T. Xu, B. Liu, T. Xu, Y. Shi, A. Atallah, R. Herbrich, S. Bowers, and J. Q. n. Candela. Practical lessons from predicting clicks on ads at facebook. In Proceedings of the Eighth International Workshop on Data Mining for Online Advertising, ADKDD?14, 2014

How to use

The keys are of type interface{} and need to be compliant with requirements for map type.

The values need to satisfy interface Timeable:

type Timeable interface {
	GetTS() time.Time
}

Example (Simple Append/Get):


	cache := Simple(time.Millisecond*100, time.Millisecond*10)
	start := time.Now()
	const KEYS = 10
	for key := 0; key <= KEYS; key++ {
		cache.Append(key, &TimeableTest{K: int(key), V: key, Ts: start})
	}

	v, ok := cache.Get(1)

Example (with expiration handler)

	cache := WithExpirationHandler(func(key interface{}, arr []Timeable) {
	    fmt.Println("hello")
	}, time.Millisecond*100, time.Millisecond*10)

	cache.Append(key, &TimeableTest{K: int(key), V: key, Ts: start})

Documentation

Index

Constants

View Source
const DEFAULT_EXPIRATION_CHAN_SIZE = 10000
View Source
const DEFAULT_SLICE_SIZE = 20

Variables

View Source
var Factory = &Cache{}

Functions

func MakeMaxResolutionChan

func MakeMaxResolutionChan(maxresolution time.Duration) chan Timeable

Types

type Cache

type Cache struct{}

func (*Cache) Custom

func (self *Cache) Custom(onExpiration ExpirationHandler, ttl time.Duration,
	expirationResolution chan Timeable, defaultSliceSize int, expirationChanSize int) CacheTtl

func (*Cache) Simple

func (self *Cache) Simple(ttl time.Duration,
	maxresolution time.Duration) CacheTtl

func (*Cache) WithExpirationHandler

func (self *Cache) WithExpirationHandler(onExpiration ExpirationHandler,
	ttl time.Duration,
	maxresolution time.Duration) CacheTtl

type CacheTtl

type CacheTtl interface {
	Append(key interface{}, value Timeable) error
	Get(key interface{}) (ret []Timeable, ok bool)
	ExpireAll() int // Returns number of expired items
}

func Custom

func Custom(onExpiration ExpirationHandler,
	ttl time.Duration,
	expirationResolution chan Timeable,
	defaultSliceSize int,
	expirationChanSize int) CacheTtl

func Simple

func Simple(ttl time.Duration, maxresolution time.Duration) CacheTtl

func WithExpirationHandler

func WithExpirationHandler(onExpiration ExpirationHandler, ttl time.Duration, maxresolution time.Duration) CacheTtl

type CacheTtlFactory

type CacheTtlFactory interface {
	WithExpirationHandler(onExpiration ExpirationHandler, ttl time.Duration, maxresolution time.Duration) CacheTtl
	Simple(ttl time.Duration, maxresolution time.Duration) CacheTtl
	Custom(onExpiration ExpirationHandler,
		ttl time.Duration,
		expirationResolution chan Timeable,
		defaultSliceSize int,
		expirationChanSize int) CacheTtl
}

type ExpirationHandler

type ExpirationHandler func(key interface{}, arr []Timeable)

type Item

type Item struct {
	Key   interface{}
	Value Timeable
}

type MP

type MP map[interface{}][]Timeable

type SimpleTime

type SimpleTime struct {
	Ts time.Time
}

func (*SimpleTime) GetTS

func (self *SimpleTime) GetTS() time.Time

type SliceCacheTTL

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

func (*SliceCacheTTL) Append

func (self *SliceCacheTTL) Append(key interface{}, value Timeable) error

func (*SliceCacheTTL) ExpireAll

func (self *SliceCacheTTL) ExpireAll() int

func (*SliceCacheTTL) Get

func (self *SliceCacheTTL) Get(key interface{}) (ret []Timeable, ok bool)

type Timeable

type Timeable interface {
	GetTS() time.Time
}

Jump to

Keyboard shortcuts

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