sdpcache

package module
v1.6.4 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2024 License: MIT Imports: 11 Imported by: 6

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

View Source
var ErrCacheNotFound = errors.New("not found in cache")

Functions

This section is empty.

Types

type Cache

type Cache struct {
	// Minimum amount of time to wait between cache purges
	MinWaitTime time.Duration
	// contains filtered or unexported fields
}

func NewCache added in v0.5.1

func NewCache() *Cache

func (*Cache) Clear added in v0.2.0

func (c *Cache) Clear()

Clear Delete all data in cache

func (*Cache) Delete

func (c *Cache) Delete(ck CacheKey)

Delete Deletes anything that matches the given cache query

func (*Cache) GetMinWaitTime

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

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

func (*Cache) Lookup added in v0.5.3

func (c *Cache) Lookup(ctx context.Context, srcName string, method sdp.QueryMethod, scope string, typ string, query string, ignoreCache bool) (bool, CacheKey, []*sdp.Item, *sdp.QueryError)

Lookup returns true/false whether or not the cache has a result for the given query. If there are results, they will be returned as slice of `sdp.Item`s or an `*sdp.QueryError`. The CacheKey is always returned, even if the lookup otherwise fails or errors

func (*Cache) Purge

func (c *Cache) Purge(before time.Time) PurgeStats

Purge Purges all expired items from the cache. The user must pass in the `before` time. All items that expired before this will be purged. Usually this would be just `time.Now()` however it could be overridden for testing

func (*Cache) Search

func (c *Cache) Search(ck CacheKey) ([]*sdp.Item, error)

Search Runs a given query against the cache. If a cached error is found it will be returned immediately, if nothing is found a ErrCacheNotFound will be returned. Otherwise this will return items that match ALL of the given query parameters

func (*Cache) StartPurger

func (c *Cache) StartPurger(ctx context.Context) error

StartPurger Starts the purge process in the background, it will be cancelled when the context is cancelled. The cache will be purged initially, at which point the process will sleep until the next time an item expires

func (*Cache) StoreError

func (c *Cache) StoreError(err error, duration time.Duration, cacheQuery CacheKey)

StoreError Stores an error for the given duration.

func (*Cache) StoreItem

func (c *Cache) StoreItem(item *sdp.Item, duration time.Duration, ck CacheKey)

StoreItem Stores an item in the cache. Note that this item must be fully populated (including metadata) for indexing to work correctly

type CacheKey added in v0.5.3

type CacheKey struct {
	SST                  SST // *required
	UniqueAttributeValue *string
	Method               *sdp.QueryMethod
	Query                *string
}

func CacheKeyFromParts added in v0.5.3

func CacheKeyFromParts(srcName string, method sdp.QueryMethod, scope string, typ string, query string) CacheKey

func CacheKeyFromQuery added in v0.5.3

func CacheKeyFromQuery(q *sdp.Query, srcName string) CacheKey

func (CacheKey) Matches added in v0.5.3

func (ck CacheKey) Matches(i IndexValues) bool

Matches Returns whether or not the supplied index values match the CacheQuery, excluding the SST since this will have already been validated. Note that this only checks values that ave actually been set in the CacheQuery

func (CacheKey) String added in v0.5.3

func (ck CacheKey) String() string

func (CacheKey) ToIndexValues added in v0.5.3

func (ck CacheKey) ToIndexValues() IndexValues

ToIndexValues Converts a cache query to a set of index values

type CachedResult

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

	// Error is the error that we want
	Error error

	// The time at which this item expires
	Expiry time.Time

	// Values that we use for calculating indexes
	IndexValues IndexValues
}

CachedResult An item including cache metadata

type IndexValues added in v0.5.1

type IndexValues struct {
	SSTHash              SSTHash
	UniqueAttributeValue string
	Method               sdp.QueryMethod
	Query                string
}

type PurgeStats

type PurgeStats struct {
	// How many items were timed out of the cache
	NumPurged int
	// How long purging took overall
	TimeTaken time.Duration
	// The expiry time of the next item to expire. If there are no more items in
	// the cache, this will be nil
	NextExpiry *time.Time
}

PurgeStats Stats about the Purge

type SST added in v0.5.1

type SST struct {
	SourceName string
	Scope      string
	Type       string
}

SST A combination of SourceName, Scope and Type, all of which must be provided

func (SST) Hash added in v0.5.1

func (s SST) Hash() SSTHash

Hash Creates a new SST hash from a given SST

type SSTHash added in v0.5.1

type SSTHash string

SSTHash Represents the hash of `SourceName`, `Scope` and `Type`

Jump to

Keyboard shortcuts

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