cache

package module
v0.0.0-...-27dd6ef Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2017 License: Apache-2.0 Imports: 11 Imported by: 0

README

go-lru-cache-stats

LRU cache with/without stats for golang

wercker status

Documentation

Overview

Package cache provides a data loading mechanism with caching and de-duplication that works across a set of peer processes.

Each data Get first consults its local cache, otherwise delegates to the requested key's canonical owner, which then checks its cache or finally gets the data. In the common case, many concurrent cache misses across a set of peers for the same key result in just one cache fill.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterNewGroupHook

func RegisterNewGroupHook(fn func(*GroupInterface))

RegisterNewGroupHook registers a hook that is run each time a group is created.

Types

type AtomicInt

type AtomicInt int64

An AtomicInt is an int64 to be accessed atomically.

func (*AtomicInt) Add

func (i *AtomicInt) Add(n int64)

Add atomically adds n to i.

func (*AtomicInt) Get

func (i *AtomicInt) Get() int64

Get atomically gets the value of i.

func (*AtomicInt) String

func (i *AtomicInt) String() string

type ByteView

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

A ByteView holds an immutable view of bytes. Internally it wraps either a []byte or a string, but that detail is invisible to callers.

A ByteView is meant to be used as a value type, not a pointer (like a time.Time).

func (ByteView) At

func (v ByteView) At(i int) byte

At returns the byte at index i.

func (ByteView) ByteSlice

func (v ByteView) ByteSlice() []byte

ByteSlice returns a copy of the data as a byte slice.

func (ByteView) Copy

func (v ByteView) Copy(dest []byte) int

Copy copies b into dest and returns the number of bytes copied.

func (ByteView) Equal

func (v ByteView) Equal(b2 ByteView) bool

Equal returns whether the bytes in b are the same as the bytes in b2.

func (ByteView) EqualBytes

func (v ByteView) EqualBytes(b2 []byte) bool

EqualBytes returns whether the bytes in b are the same as the bytes in b2.

func (ByteView) EqualString

func (v ByteView) EqualString(s string) bool

EqualString returns whether the bytes in b are the same as the bytes in s.

func (ByteView) Len

func (v ByteView) Len() int

Len returns the view's length.

func (ByteView) ReadAt

func (v ByteView) ReadAt(p []byte, off int64) (n int, err error)

ReadAt implements io.ReaderAt on the bytes in v.

func (ByteView) Reader

func (v ByteView) Reader() io.ReadSeeker

Reader returns an io.ReadSeeker for the bytes in v.

func (ByteView) Slice

func (v ByteView) Slice(from, to int) ByteView

Slice slices the view between the provided from and to indices.

func (ByteView) SliceFrom

func (v ByteView) SliceFrom(from int) ByteView

SliceFrom slices the view from the provided index until the end.

func (ByteView) String

func (v ByteView) String() string

String returns the data as a string, making a copy if necessary.

type Context

type Context interface{}

Context is an opaque value passed through calls to the ProtoGetter. It may be nil if your ProtoGetter implementation does not require a context.

type Getter

type Getter interface {
	// Get returns the value identified by key, populating dest.
	//
	// The returned data must be unversioned. That is, key must
	// uniquely describe the loaded data, without an implicit
	// current time, and without relying on cache expiration
	// mechanisms.
	Get(ctx Context, key string, dest Sink) error
}

A Getter loads data for a key.

type GetterFunc

type GetterFunc func(ctx Context, key string, dest Sink) error

A GetterFunc implements Getter with a function.

func (GetterFunc) Get

func (f GetterFunc) Get(ctx Context, key string, dest Sink) error

Get item from cache.

type Group

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

A Group is a cache namespace and associated data loaded spread over a group of 1 or more machines.

func (*Group) Get

func (g *Group) Get(ctx Context, key string, dest Sink) error

Get item from cache.

func (*Group) Name

func (g *Group) Name() string

Name returns the name of the group.

func (*Group) Remove

func (g *Group) Remove(key string)

Remove item from cache.

type GroupInterface

type GroupInterface interface {
	Name() string
	Get(ctx Context, key string, dest Sink) error
	// contains filtered or unexported methods
}

GroupInterface implements Group.

func GetGroup

func GetGroup(name string) *GroupInterface

GetGroup returns the named group previously created with NewGroup, or nil if there's no such group.

func NewGroup

func NewGroup(name string, cacheBytes int64, getter Getter, stats bool) *GroupInterface

NewGroup creates a coordinated group-aware Getter from a Getter.

The returned Getter tries (but does not guarantee) to run only one Get call at once for a given key across an entire set of peer processes. Concurrent callers both in the local process and in other processes receive copies of the answer once the original Get completes.

The group name must be unique for each getter.

type GroupStats

type GroupStats struct {
	Bytes     int64
	Items     int64
	Gets      int64
	Hits      int64
	Evictions int64
}

GroupStats are returned by stats accessors on Group.

type GroupWithStats

type GroupWithStats struct {
	*Group

	// Stats are statistics on the group.
	Stats Stats
	// contains filtered or unexported fields
}

A GroupWithStats is a cache namespace and associated data loaded spread over a group of 1 or more machines.

func (*GroupWithStats) Get

func (g *GroupWithStats) Get(ctx Context, key string, dest Sink) error

Get item from cache.

func (*GroupWithStats) GroupStats

func (g *GroupWithStats) GroupStats() GroupStats

GroupStats returns stats about the provided cache within the group.

type ProtoGetter

type ProtoGetter interface {
	Get(context Context, in *pb.GetRequest, out *pb.GetResponse) error
}

ProtoGetter is the interface that must be implemented by a peer.

type Sink

type Sink interface {
	// SetString sets the value to s.
	SetString(s string) error

	// SetBytes sets the value to the contents of v.
	// The caller retains ownership of v.
	SetBytes(v []byte) error

	// SetProto sets the value to the encoded version of m.
	// The caller retains ownership of m.
	SetProto(m proto.Message) error
	// contains filtered or unexported methods
}

A Sink receives data from a Get call.

Implementation of Getter must call exactly one of the Set methods on success.

func AllocatingByteSliceSink

func AllocatingByteSliceSink(dst *[]byte) Sink

AllocatingByteSliceSink returns a Sink that allocates a byte slice to hold the received value and assigns it to *dst. The memory is not retained by groupcache.

func ByteViewSink

func ByteViewSink(dst *ByteView) Sink

ByteViewSink returns a Sink that populates a ByteView.

func ProtoSink

func ProtoSink(m proto.Message) Sink

ProtoSink returns a sink that unmarshals binary proto values into m.

func StringSink

func StringSink(sp *string) Sink

StringSink returns a Sink that populates the provided string pointer.

func TruncatingByteSliceSink

func TruncatingByteSliceSink(dst *[]byte) Sink

TruncatingByteSliceSink returns a Sink that writes up to len(*dst) bytes to *dst. If more bytes are available, they're silently truncated. If fewer bytes are available than len(*dst), *dst is shrunk to fit the number of bytes available.

type Stats

type Stats struct {
	Gets          AtomicInt // any Get request, including from peers
	CacheHits     AtomicInt // either cache was good
	Loads         AtomicInt // (gets - cacheHits)
	LoadsDeduped  AtomicInt // after singleflight
	LocalLoads    AtomicInt // total good local loads
	LocalLoadErrs AtomicInt // total bad local loads
}

Stats are per-group statistics.

Jump to

Keyboard shortcuts

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