cache

package
v0.3.11 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2020 License: GPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MIN_TIMER_INTERVAL = 1 * time.Millisecond
)

Variables

This section is empty.

Functions

func StartTicks

func StartTicks(tickInterval time.Duration)

Start the self-ticking routine, which ticks per tickInterval

func Tick

func Tick()

Tick once for timers

Types

type Cache

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

Cache is a thread-safe fixed size LRU cache.

func NewThreadSafeCache

func NewThreadSafeCache(size int) (*Cache, error)

New creates an LRU of the given size.

func NewThreadSafeWithEvict

func NewThreadSafeWithEvict(size int, onEvicted func(key interface{}, value interface{})) (*Cache, error)

NewWithEvict constructs a fixed size cache with the given eviction callback.

func (*Cache) Add

func (c *Cache) Add(key, value interface{}) (evicted bool)

Add adds a value to the cache. Returns true if an eviction occurred.

func (*Cache) Clear

func (c *Cache) Clear()

Purge is used to completely clear the cache.

func (*Cache) Contains

func (c *Cache) Contains(key interface{}) bool

Contains checks if a key is in the cache, without updating the recent-ness or deleting it for being stale.

func (*Cache) ContainsOrAdd

func (c *Cache) ContainsOrAdd(key, value interface{}) (ok, evicted bool)

ContainsOrAdd checks if a key is in the cache without updating the recent-ness or deleting it for being stale, and if not, adds the value. Returns whether found and whether an eviction occurred.

func (*Cache) Get

func (c *Cache) Get(key interface{}) (value interface{}, ok bool)

Get looks up a key's value from the cache.

func (*Cache) GetOldest

func (c *Cache) GetOldest() (key interface{}, value interface{}, ok bool)

GetOldest returns the oldest entry

func (*Cache) Keys

func (c *Cache) Keys() []interface{}

Keys returns a slice of the keys in the cache, from oldest to newest.

func (*Cache) Len

func (c *Cache) Len() int

Len returns the number of items in the cache.

func (*Cache) Peek

func (c *Cache) Peek(key interface{}) (value interface{}, ok bool)

Peek returns the key value (or undefined if not found) without updating the "recently used"-ness of the key.

func (*Cache) PeekOrAdd

func (c *Cache) PeekOrAdd(key, value interface{}) (previous interface{}, ok, evicted bool)

PeekOrAdd checks if a key is in the cache without updating the recent-ness or deleting it for being stale, and if not, adds the value. Returns whether found and whether an eviction occurred.

func (*Cache) Remove

func (c *Cache) Remove(key interface{}) (present bool)

Remove removes the provided key from the cache.

func (*Cache) RemoveOldest

func (c *Cache) RemoveOldest() (key interface{}, value interface{}, ok bool)

RemoveOldest removes the oldest item from the cache.

func (*Cache) Resize

func (c *Cache) Resize(size int) (evicted int)

Resize changes the cache size.

type CacheInfo

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

type CacheWithTTL

type CacheWithTTL struct {
	*LRU

	TTL time.Duration
	// contains filtered or unexported fields
}

func (*CacheWithTTL) Add

func (t *CacheWithTTL) Add(key, value interface{}) bool

Add adds the item to the cache. It also includes the `lastAccessTime` to the value. Life of an item can be increased by calling `Add` multiple times on the same key.

func (*CacheWithTTL) Clear

func (t *CacheWithTTL) Clear()

Purge is used to completely clear the cache.

func (*CacheWithTTL) Contains

func (t *CacheWithTTL) Contains(key interface{}) bool

Contains checks if a key is in the cache, without updating the recent-ness or deleting it for being stale.

func (*CacheWithTTL) Dispose

func (t *CacheWithTTL) Dispose()

func (*CacheWithTTL) Get

func (t *CacheWithTTL) Get(key interface{}) (value interface{}, ok bool)

Get looks up a key's value from the cache. Also, it unmarshals `lastAccessTime` from `Get` response

func (*CacheWithTTL) Keys

func (t *CacheWithTTL) Keys() []interface{}

Keys returns a slice of the keys in the cache, from oldest to newest.

func (*CacheWithTTL) Len

func (t *CacheWithTTL) Len() int

Len returns the number of items in the cache.

func (*CacheWithTTL) Peek

func (t *CacheWithTTL) Peek(key interface{}) (value interface{}, ok bool)

Peek returns the key value (or undefined if not found) without updating the "recently used"-ness of the key. Also, it unmarshals the `lastAccessTime` from the result

func (*CacheWithTTL) Remove

func (t *CacheWithTTL) Remove(key interface{}) bool

Remove removes the provided key from the cache.

func (*CacheWithTTL) RemoveOldest

func (t *CacheWithTTL) RemoveOldest() (key interface{}, value interface{}, ok bool)

RemoveOldest removes the oldest item from the cache.

type CallbackFunc

type CallbackFunc func()

Type of callback function

type ConnectivityCache

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

ConnectivityCache is a invalidatedCache for URL connectivity.

func NewConnectivityCache

func NewConnectivityCache() (*ConnectivityCache, error)

func (*ConnectivityCache) ClearBrowser

func (c *ConnectivityCache) ClearBrowser()

func (*ConnectivityCache) Invalidate added in v0.3.10

func (c *ConnectivityCache) Invalidate(url string)

Invalidate a invalidatedCache entry by removing it from the invalidatedCache.

func (*ConnectivityCache) IsOk

func (c *ConnectivityCache) IsOk(url string) bool

IsOk returns whether the invalidatedCache contains a successful response for the url

func (*ConnectivityCache) IsOkAndSet

func (c *ConnectivityCache) IsOkAndSet(u string, f func() bool) bool

IsOkAndSet checks if the `u` value is contained, if it's not it checks it. This operation is thread safe, you can use it to modify the invalidatedCache state in the function.

func (*ConnectivityCache) SetBrowser

func (c *ConnectivityCache) SetBrowser(bow browser.Browsable)

func (*ConnectivityCache) Test

func (c *ConnectivityCache) Test(u string) error

Test the connectivity for an url.

type ConnectivityTester added in v0.2.2

type ConnectivityTester interface {
	//IsOkAndSet checks if the `u` value is contained, if it's not it checks it.
	//This operation should be thread safe, you can use it to modify the invalidatedCache state in the function.
	IsOkAndSet(u string, f func() bool) bool
	IsOk(url string) bool
	//Test if the operation can be completed with success. If so, invalidatedCache that.
	Test(u string) error
	SetBrowser(bow browser.Browsable)
	ClearBrowser()
	Invalidate(url string)
}

type EvictionCallback

type EvictionCallback func(key interface{}, value interface{})

type LRU

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

LRU is a non-thread safe fixed size LRU cache

func NewLRU

func NewLRU(size int, onEviction EvictionCallback) (*LRU, error)

NewLRU creates a new LRU of the given size

func (*LRU) Add

func (c *LRU) Add(key, value interface{}) bool

Add a key with a given value returns true if an element was evicted so this one could be added

func (*LRU) Clear

func (c *LRU) Clear()

Clear the cahce

func (*LRU) Contains

func (c *LRU) Contains(key interface{}) (ok bool)

Contains checks if a key is in the cache, without updating the recent-ness or deleting it for being stale.

func (*LRU) Get

func (c *LRU) Get(key interface{}) (value interface{}, ok bool)

Get looks up a key's value from the cache. this updates the recent-ness of the cache

func (*LRU) GetOldest

func (c *LRU) GetOldest() (key interface{}, value interface{}, ok bool)

GetOldest returns the oldest item in the cache.

func (*LRU) Keys

func (c *LRU) Keys() []interface{}

Keys returns the cache keys.

func (*LRU) Len

func (c *LRU) Len() int

Len returns the number of items in the cache

func (*LRU) Peek

func (c *LRU) Peek(key interface{}) (value interface{}, ok bool)

Peek returns the key value (or nil if not found) without updating the "recently used"-ness of the key.

func (*LRU) Remove

func (c *LRU) Remove(key interface{}) (present bool)

Remove removes the provided key from the cache, returning if the key was contained.

func (*LRU) RemoveOldest

func (c *LRU) RemoveOldest() (key, value interface{}, ok bool)

RemoveOldest removes the oldest item in the cache

func (*LRU) Resize

func (c *LRU) Resize(newSize int) int

Resize changes the cache size returns the number of items removed when the cache shrinks

type LRUCache

type LRUCache interface {
	// Adds a value to the cache, returns true if an eviction occurred and
	// updates the "recently used"-ness of the key.
	Add(key, value interface{}) bool

	// Returns key's value from the cache and
	// updates the "recently used"-ness of the key. #value, isFound
	Get(key interface{}) (value interface{}, ok bool)

	// Checks if a key exists in cache without updating the recent-ness.
	Contains(key interface{}) (ok bool)

	// Returns key's value without updating the "recently used"-ness of the key.
	Peek(key interface{}) (value interface{}, ok bool)

	// Removes a key from the cache.
	Remove(key interface{}) bool

	// Removes the oldest entry from cache.
	RemoveOldest() (interface{}, interface{}, bool)

	// Returns the oldest entry from the cache. #key, value, isFound
	GetOldest() (interface{}, interface{}, bool)

	// Returns a slice of the keys in the cache, from oldest to newest.
	Keys() []interface{}

	// Returns the number of items in the cache.
	Len() int

	// Clears all cache entries.
	Clear()

	// Resizes cache, returning number evicted
	Resize(int) int
}

LRUCache is the interface for simple LRU cache.

func NewTTL

func NewTTL(size int, ttl time.Duration) (LRUCache, error)

func NewTTLWithEvict

func NewTTLWithEvict(size int, ttl time.Duration, onEvict EvictionCallback) (LRUCache, error)

type OptimisticConnectivityCache added in v0.3.10

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

* This invalidatedCache should return true from the start, and only start working if the items have been non-present.

func NewOptimisticConnectivityCache added in v0.3.10

func NewOptimisticConnectivityCache() (*OptimisticConnectivityCache, error)

func (*OptimisticConnectivityCache) ClearBrowser added in v0.3.10

func (c *OptimisticConnectivityCache) ClearBrowser()

func (*OptimisticConnectivityCache) Invalidate added in v0.3.10

func (c *OptimisticConnectivityCache) Invalidate(url string)

Invalidate a invalidatedCache entry by removing it from the invalidatedCache.

func (*OptimisticConnectivityCache) IsOk added in v0.3.10

IsOk returns whether the invalidatedCache contains a successful response for the url

func (*OptimisticConnectivityCache) IsOkAndSet added in v0.3.10

func (c *OptimisticConnectivityCache) IsOkAndSet(u string, f func() bool) bool

IsOkAndSet checks if the `u` value is contained, if it's not it checks it. This operation is thread safe, you can use it to modify the invalidatedCache state in the function.

func (*OptimisticConnectivityCache) SetBrowser added in v0.3.10

func (c *OptimisticConnectivityCache) SetBrowser(bow browser.Browsable)

func (*OptimisticConnectivityCache) Test added in v0.3.10

Test the connectivity for an url.

type Timer

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

func AddCallback

func AddCallback(d time.Duration, callback CallbackFunc) *Timer

Add a callback which will be called after specified duration

func AddTimer

func AddTimer(d time.Duration, callback CallbackFunc) *Timer

Add a timer which calls callback periodly

func (*Timer) Cancel

func (t *Timer) Cancel()

func (*Timer) IsActive

func (t *Timer) IsActive() bool

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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