cache

package
v0.0.0-...-2841839 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2023 License: MIT Imports: 8 Imported by: 0

README

cache

import "github.com/cognusion/go-jar/cache"

Overview

Index

Package files

group.go misc.go

Constants

const (
    // NilBackfillError is returned by the Getter if there there is no backfill func, in lieu of panicing
    NilBackfillError = Error("item not in cache and backfill func is nil")
    // ItemNotFoundError is a generic error returned by a BackFillFunc if the item is not found or findable
    ItemNotFoundError = Error("item not found")
    // CacheNotFoundError is an error returned if the cache requested is not found
    CacheNotFoundError = Error("cache not found")
    // NameRequiredError is returned when creating or adding a cache, and the Config.Name field is empty
    NameRequiredError = Error("name is required")
)

type BackFillFunc

type BackFillFunc func(key string) ([]byte, error)

BackFillFunc is a function that can retrieve an uncached item to go into the cache

type Config

type Config struct {
    Name           string        // For New and Add. Pass as ``cacheName`` to differentiate caches
    ListenAddress  string        // Only for New to set the listener
    PeerList       []string      // Only for New to establish the initial PeerList. May be reset with GroupCache.SetPeers()
    CacheSize      int64         // For New and Add to set the size in bytes of the cache
    ItemExpiration time.Duration // For New and Add to set the default expiration duration. Leave as empty for infinite.
}

Config is used to store configuration information to pass to a GroupCache.

type Error

type Error string

Error is an error type

func (Error) Error
func (e Error) Error() string

Error returns the stringified version of Error

type GroupCache

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

GroupCache is a distributed LRU cache where consistent hashing on keynames is used to cut out "who's on first" nonsense, and backfills are linearly distributed to mitigate multiple-member requests.

func NewGroupCache
func NewGroupCache(config Config, fillfunc BackFillFunc) (*GroupCache, error)

NewGroupCache creates a GroupCache from the Config. Only call this once. If you need more caches use the .Add() function. fillfunc may be nil if caches will be added later using .Add().

func (*GroupCache) Add
func (gc *GroupCache) Add(config Config, fillfunc BackFillFunc) error

Add creates new caches in the cluster. Config.ListenAddress and Config.PeerList are ignored.

func (*GroupCache) Close
func (gc *GroupCache) Close() error

Close calls the listener close function

func (*GroupCache) Get
func (gc *GroupCache) Get(cacheName, key string) (value interface{}, ok bool)

Get will return the value of the cacheName'd key, asking other cache members or backfilling as necessary.

func (*GroupCache) GetContext
func (gc *GroupCache) GetContext(ctx context.Context, cacheName, key string) (value interface{}, ok bool)

GetContext will return the value of the cacheName'd key, asking other cache members or backfilling as necessary, honoring the provided context.

func (*GroupCache) Names
func (gc *GroupCache) Names() []string

Names returns the names of the current caches

func (*GroupCache) Remove
func (gc *GroupCache) Remove(cacheName, key string) error

Remove makes a best effort to remove an item from the cache

func (*GroupCache) RemoveContext
func (gc *GroupCache) RemoveContext(ctx context.Context, cacheName, key string) error

RemoveContext makes a best effort to remove an item from the cache, honoring the provided context.

func (*GroupCache) Set
func (gc *GroupCache) Set(cacheName, key string, value []byte) error

Set forces an item into the cache, following the configured expiration policy

func (*GroupCache) SetContext
func (gc *GroupCache) SetContext(ctx context.Context, cacheName, key string, value []byte, expiration time.Time) error

SetContext forces an item into the cache, following the specified expiration (unless a zero Time is provided then falling back to the configured expiration policy) honoring the provided context.

func (*GroupCache) SetDebugOut
func (gc *GroupCache) SetDebugOut(logger *log.Logger)

SetDebugOut wires in the debug logger to the specified logger

func (*GroupCache) SetPeers
func (gc *GroupCache) SetPeers(peers ...string)

SetPeers allows the dynamic [re]setting of the peerlist

func (*GroupCache) SetToExpireAt
func (gc *GroupCache) SetToExpireAt(cacheName, key string, expireAt time.Time, value []byte) error

SetToExpireAt forces an item into the cache, to expire at a specific time regardless of the cache configuration. Use SetContext if you need to set the expiration and a context.

func (*GroupCache) Stats
func (gc *GroupCache) Stats(w http.ResponseWriter, req *http.Request)

Stats is a request finisher that outputs the GroupCache stats as JSON


Generated by godoc2md

Documentation

Index

Constants

View Source
const (
	// NilBackfillError is returned by the Getter if there there is no backfill func, in lieu of panicing
	NilBackfillError = Error("item not in cache and backfill func is nil")
	// ItemNotFoundError is a generic error returned by a BackFillFunc if the item is not found or findable
	ItemNotFoundError = Error("item not found")
	// CacheNotFoundError is an error returned if the cache requested is not found
	CacheNotFoundError = Error("cache not found")
	// NameRequiredError is returned when creating or adding a cache, and the Config.Name field is empty
	NameRequiredError = Error("name is required")
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BackFillFunc

type BackFillFunc func(key string) ([]byte, error)

BackFillFunc is a function that can retrieve an uncached item to go into the cache

type Config

type Config struct {
	Name           string        // For New and Add. Pass as “cacheName“ to differentiate caches
	ListenAddress  string        // Only for New to set the listener
	PeerList       []string      // Only for New to establish the initial PeerList. May be reset with GroupCache.SetPeers()
	CacheSize      int64         // For New and Add to set the size in bytes of the cache
	ItemExpiration time.Duration // For New and Add to set the default expiration duration. Leave as empty for infinite.
}

Config is used to store configuration information to pass to a GroupCache.

type Error

type Error string

Error is an error type

func (Error) Error

func (e Error) Error() string

Error returns the stringified version of Error

type GroupCache

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

GroupCache is a distributed LRU cache where consistent hashing on keynames is used to cut out "who's on first" nonsense, and backfills are linearly distributed to mitigate multiple-member requests.

func NewGroupCache

func NewGroupCache(config Config, fillfunc BackFillFunc) (*GroupCache, error)

NewGroupCache creates a GroupCache from the Config. Only call this once. If you need more caches use the .Add() function. fillfunc may be nil if caches will be added later using .Add().

func (*GroupCache) Add

func (gc *GroupCache) Add(config Config, fillfunc BackFillFunc) error

Add creates new caches in the cluster. Config.ListenAddress and Config.PeerList are ignored.

func (*GroupCache) Close

func (gc *GroupCache) Close() error

Close calls the listener close function

func (*GroupCache) Get

func (gc *GroupCache) Get(cacheName, key string) (value interface{}, ok bool)

Get will return the value of the cacheName'd key, asking other cache members or backfilling as necessary.

func (*GroupCache) GetContext

func (gc *GroupCache) GetContext(ctx context.Context, cacheName, key string) (value interface{}, ok bool)

GetContext will return the value of the cacheName'd key, asking other cache members or backfilling as necessary, honoring the provided context.

func (*GroupCache) Names

func (gc *GroupCache) Names() []string

Names returns the names of the current caches

func (*GroupCache) Remove

func (gc *GroupCache) Remove(cacheName, key string) error

Remove makes a best effort to remove an item from the cache

func (*GroupCache) RemoveContext

func (gc *GroupCache) RemoveContext(ctx context.Context, cacheName, key string) error

RemoveContext makes a best effort to remove an item from the cache, honoring the provided context.

func (*GroupCache) Set

func (gc *GroupCache) Set(cacheName, key string, value []byte) error

Set forces an item into the cache, following the configured expiration policy

func (*GroupCache) SetContext

func (gc *GroupCache) SetContext(ctx context.Context, cacheName, key string, value []byte, expiration time.Time) error

SetContext forces an item into the cache, following the specified expiration (unless a zero Time is provided then falling back to the configured expiration policy) honoring the provided context.

func (*GroupCache) SetDebugOut

func (gc *GroupCache) SetDebugOut(logger *log.Logger)

SetDebugOut wires in the debug logger to the specified logger

func (*GroupCache) SetPeers

func (gc *GroupCache) SetPeers(peers ...string)

SetPeers allows the dynamic [re]setting of the peerlist

func (*GroupCache) SetToExpireAt

func (gc *GroupCache) SetToExpireAt(cacheName, key string, expireAt time.Time, value []byte) error

SetToExpireAt forces an item into the cache, to expire at a specific time regardless of the cache configuration. Use SetContext if you need to set the expiration and a context.

func (*GroupCache) Stats

func (gc *GroupCache) Stats(w http.ResponseWriter, req *http.Request)

Stats is a request finisher that outputs the GroupCache stats as JSON

Jump to

Keyboard shortcuts

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