Documentation ¶
Overview ¶
Package groupcache 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 ¶
- func GetTime() int64
- func RegisterNewGroupHook(fn func(*Group))
- func RegisterPeerPicker(fn func() PeerPicker)
- func RegisterServerStart(fn func())
- func UnpackTimestamp(b []byte) (result []byte, timestamp int64, err error)
- type AtomicInt
- type ByteView
- func (v ByteView) At(i int) byte
- func (v ByteView) ByteSlice() []byte
- func (v ByteView) Copy(dest []byte) int
- func (v ByteView) Equal(b2 ByteView) bool
- func (v ByteView) EqualBytes(b2 []byte) bool
- func (v ByteView) EqualString(s string) bool
- func (v ByteView) Len() int
- func (v ByteView) ReadAt(p []byte, off int64) (n int, err error)
- func (v ByteView) Reader() io.ReadSeeker
- func (v ByteView) Slice(from, to int) ByteView
- func (v ByteView) SliceFrom(from int) ByteView
- func (v ByteView) String() string
- type CacheStats
- type CacheType
- type Context
- type Getter
- type GetterFunc
- type Group
- func (g *Group) CacheStats(which CacheType) CacheStats
- func (g *Group) Get(ctx Context, key string, dest Sink) error
- func (g *Group) Name() string
- func (g *Group) SetDisableHotCache(disable bool) *Group
- func (g *Group) SetExpiration(d time.Duration) *Group
- func (g *Group) SetStaleDeadline(d time.Duration) *Group
- func (g *Group) SetStalePeriod(d time.Duration) *Group
- type HTTPPool
- type NoPeers
- type PeerPicker
- type ProtoGetter
- type Sink
- type Stats
- type TimeProvider
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetTime ¶
func GetTime() int64
GetTime should be called to produce a new timestamp to supply for Group.SetTimeStampBytes()
func RegisterNewGroupHook ¶
func RegisterNewGroupHook(fn func(*Group))
RegisterNewGroupHook registers a hook that is run each time a group is created.
func RegisterPeerPicker ¶
func RegisterPeerPicker(fn func() PeerPicker)
RegisterPeerPicker registers the peer initialization function. It is called once, when the first group is created.
func RegisterServerStart ¶
func RegisterServerStart(fn func())
RegisterServerStart registers a hook that is run when the first group is created.
Types ¶
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) EqualBytes ¶
EqualBytes returns whether the bytes in b are the same as the bytes in b2.
func (ByteView) EqualString ¶
EqualString returns whether the bytes in b are the same as the bytes in s.
func (ByteView) Reader ¶
func (v ByteView) Reader() io.ReadSeeker
Reader returns an io.ReadSeeker for the bytes in v.
type CacheStats ¶
CacheStats are returned by stats accessors on Group.
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 ¶
A GetterFunc implements Getter with a function.
type Group ¶
type Group struct { // Stats are statistics on the group. Stats Stats // 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 GetGroup ¶
GetGroup returns the named group previously created with NewGroup, or nil if there's no such group.
func NewGroup ¶
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.
func (*Group) CacheStats ¶
func (g *Group) CacheStats(which CacheType) CacheStats
CacheStats returns stats about the provided cache within the group.
func (*Group) SetDisableHotCache ¶
SetDisableHotCache controls disabling of writes to the hotCache. May be used with expiration functionality to prevent clients from getting inconsistent data freshness due to unsynchronized clocks in a multi-peer setup. With the hotCache disabled, there is only ever one server in a system where an item may be cached (the authorative server for a given key). With the hotCache disabled, if the authoritative server for a given key goes down, its peers will generate the value locally, but will not cache it (otherwise the value would be generated locally and cached locally). Writes to the hotCache are enabled by default.
func (*Group) SetExpiration ¶
SetExpiration sets the cache expiration time. Internally the duration is truncated to seconds, so it's not useful to set with greater precision. If unset or set to 0, will cache items forever. To effectively disable caching, set to 1ns.
In getter, set timestamp using SetTimestampBytes():
func(gctx groupcache.Context, key string, dest groupcache.Sink) error { content = MakeMyContent(key) return dest.SetTimestampBytes(content, groupcache.GetTime()) }
In call to Get() use AllocatingByteSliceSink, StringSink, or ByteViewSink, then unpack returned result. Using an unsupported sink implementation will produce an error when calling Get().
var packedContent []byte var timestamp int64
if err := gcache.Get(nil, key, groupcache.AllocatingByteSliceSink(&packedContent)); err != nil { handleErr() }
content, timestamp, err := groupcache.UnpackTimestamp(packedContent) if err != nil { handleErr() }
func (*Group) SetStaleDeadline ¶
SetStaleDeadline sets the deadline during the stale period for a background reload after which stale data will be returned if fresh data is not ready.
type HTTPPool ¶
type HTTPPool struct { // Context optionally specifies a context for the server to use when it // receives a request. // If nil, the server uses a nil Context. Context func(*http.Request) Context // Transport optionally specifies an http.RoundTripper for the client // to use when it makes a request. // If nil, the client uses http.DefaultTransport. Transport func(Context) http.RoundTripper // contains filtered or unexported fields }
HTTPPool implements PeerPicker for a pool of HTTP peers.
func NewHTTPPool ¶
NewHTTPPool initializes an HTTP pool of peers. It registers itself as a PeerPicker and as an HTTP handler with the http.DefaultServeMux. The self argument be a valid base URL that points to the current server, for example "http://example.net:8000".
func (*HTTPPool) Set ¶
Set updates the pool's list of peers. Each peer value should be a valid base URL, for example "http://example.net:8000".
type NoPeers ¶
type NoPeers struct{}
NoPeers is an implementation of PeerPicker that never finds a peer.
type PeerPicker ¶
type PeerPicker interface { // PickPeer returns the peer that owns the specific key // and true to indicate that a remote peer was nominated. // It returns nil, false if the key owner is the current peer. PickPeer(key string) (peer ProtoGetter, ok bool) }
PeerPicker is the interface that must be implemented to locate the peer that owns a specific key.
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 // SetTimestampBytes sets the value to the contents of v, appending the // supplied Unix epoch timestamp in seconds. SetTimestampBytes(v []byte, timestamp int64) 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 ¶
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 ¶
ByteViewSink returns a Sink that populates a ByteView.
func StringSink ¶
StringSink returns a Sink that populates the provided string pointer.
func TruncatingByteSliceSink ¶
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 PeerLoads AtomicInt // either remote load or remote cache hit (not an error) PeerErrors AtomicInt Loads AtomicInt // (gets - cacheHits) LoadsDeduped AtomicInt // after singleflight LocalLoads AtomicInt // total good local loads LocalLoadErrs AtomicInt // total bad local loads ServerRequests AtomicInt // gets that came over the network from peers }
Stats are per-group statistics.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package lru implements an LRU cache.
|
Package lru implements an LRU cache. |
Package singleflight provides a duplicate function call suppression mechanism.
|
Package singleflight provides a duplicate function call suppression mechanism. |