cache

package
v0.0.0-...-6469b06 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2018 License: BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Overview

Package cache implements a cache which collaborates on concurrent calculations of the same hash key to reduce time spent calculating expensive values.

The Cache.Get() method ensures that at most one goroutine is calculating a specific key value at any time. It also ensures that frequently accessed values are recalculated before their expiration time. It does this by creating a cacheItem which contains a wait condition on the value completion only if one does not already exist.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fuzz

func Fuzz(data []byte) int

Fuzz tests flowcache with a binary input test plan

Types

type Cache

type Cache struct {
	MaxSize    int
	MaxStorage uint64

	Refresh     bool
	ExtendOnUse bool
	GetTimeout  time.Duration
	Recover     bool
	// contains filtered or unexported fields
}

Cache implements a cache

func (*Cache) Clear

func (c *Cache) Clear()

Clear removes all items from the cache.

func (*Cache) Get

func (c *Cache) Get(key interface{}, ttl time.Duration, generate func(interface{}) (interface{}, error)) func() (interface{}, error)

Get begins the process of fetching a specific cache item.

When Get is called a new goroutine is spawned to run the provided generate method if no result is available in the cache. A function is return which, when called, will wait for the goroutine to finish execution and return the value/error returned. In the case that the item has expired before the retrieve method is called the goroutine may be called again.

If the cache entry is half way through its TTL a new goroutine will be spawned at the time of retrieval, though the current cached values will be returned immediately.

Expiration/Refresh conditions are evaluated immediately upon calling Get(), the retrieval function returns the cache query as it was evaluated during the Get operation.

func (*Cache) Purge

func (c *Cache) Purge()

Purge finds and removes all expired cache entires from the cache, allowing the data to be freed by the garbage collector.

func (*Cache) PurgeCount

func (c *Cache) PurgeCount(count int)

PurgeCount finds and removes all expired cache entires from the cache, checking at moust count items.

func (*Cache) Size

func (c *Cache) Size() int

Size returns the number of cache entires (including unpurged expired entries) in the cache.

Jump to

Keyboard shortcuts

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