sdpcache

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2022 License: MIT Imports: 7 Imported by: 5

README

sdpcache

Go Reference

The library provides a flexible and configurable cache for SDP items. It has the following features:

  • Caching of both items and errors
  • Configurable cache duration per-item
  • Intelligent purging logic adapts purge frequency to cache content
  • Useful statistics

See the reference docs for more info.

Documentation

Index

Constants

View Source
const MinWaitDefault = (5 * time.Second)

MinWaitDefault The default minimum wait time

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	// Name used for logging since there are many caches
	Name    string
	Storage []CachedResult

	// The minimum amount of time to wait between purges, defaults to
	// MinWaitDefault
	MinWaitTime time.Duration
	// contains filtered or unexported fields
}

Cache is responsible for caching data between calls

func (*Cache) BulkStoreItem

func (c *Cache) BulkStoreItem(items []*sdp.Item, duration time.Duration, tags Tags)

BulkStoreItem stores many items at once instead of just one

func (*Cache) Clear added in v0.2.0

func (c *Cache) Clear()

Clear Clears all items from the cache

func (*Cache) Delete

func (c *Cache) Delete(searchTags Tags) error

Delete Removes items form the cache that have matching tags

func (*Cache) GetMinWaitTime

func (c *Cache) GetMinWaitTime() time.Duration

GetMinWaitTime Returns the minimum wait time or the default if not set

func (*Cache) Purge

func (c *Cache) Purge() PurgeStats

Purge Prunes old items from the cache

func (*Cache) Search

func (c *Cache) Search(searchTags Tags) ([]*sdp.Item, error)

Search will find all items with a given set of tags. If multiple tags are supplied an AND is used. If a cached error is found with matching tags it will be returned immediately

func (*Cache) StartPurger

func (c *Cache) StartPurger()

StartPurger Starts the automatic purging of the cache

func (*Cache) StoreError

func (c *Cache) StoreError(err error, duration time.Duration, tags Tags)

StoreError Store an item in the cache.

Note that all errors will get the following tags by default:

isError: true

func (*Cache) StoreItem

func (c *Cache) StoreItem(item *sdp.Item, duration time.Duration, tags Tags)

StoreItem Store an item in the cache.

Note that all stored items will have the following default tags applied:

type: (item.Type)
context: (item.Context)
uniqueAttributeValue: (item.Attributes[item.UniqueAttribute])

These default tags mean that each full tag set will be unique to an item

type CacheNotFoundError

type CacheNotFoundError Tags

CacheNotFoundError is returned when an item is not found in the cache. Usually this would be handled very gracefully

func (CacheNotFoundError) Error

func (c CacheNotFoundError) Error() string

type CachedResult

type CachedResult struct {
	// Item is the actual cached item
	Item *sdp.Item

	// Error is the error that we want
	Error error

	// CacheDuration How long to cache the item for
	CacheDuration time.Duration

	// InterstTime When the item was inserted into the cache
	InsertTime time.Time

	// Tags are arbitrary strings maps that can be searched on
	Tags Tags
}

CachedResult An itemincluding cache metadata

func (*CachedResult) Expired

func (cr *CachedResult) Expired() bool

Expired Tells us if the item's cache has expired

func (*CachedResult) HasTag

func (cr *CachedResult) HasTag(key string, value string) bool

HasTag returns a boolean if the CachedResult has a given tag with a given value

func (*CachedResult) HasTags

func (cr *CachedResult) HasTags(tags Tags) bool

HasTags returns whether a given result has all of the tags supplied. This is a logical AND query

type PurgeStats

type PurgeStats struct {
	// How many items were timed out of the cache
	NumPurged int
	// How long the Purgeing took overall
	TimeTaken time.Duration
	// Shortest cache duration of the remaining items
	ShortestCacheRemaining time.Duration
}

PurgeStats Stats about the Purge

type Tags

type Tags map[string]string

Tags A map of key-value pairs that can be searched on

func DefaultErrorTags

func DefaultErrorTags(e error) Tags

DefaultErrorTags Returns default tags for any error

func DefaultItemTags

func DefaultItemTags(item *sdp.Item) Tags

DefaultItemTags Returns default tags for a given item

Jump to

Keyboard shortcuts

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