Documentation ¶
Overview ¶
Package groupcache provides a data loading mechanism with caching and de-duplication that works across a set of peer processes.
Each Get() call 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 ¶
- type AtomicInt
- type Cache
- type CacheStats
- type CacheType
- type Daemon
- func (d *Daemon) GetGroup(name string) transport.Group
- func (d *Daemon) GetInstance() *Instance
- func (d *Daemon) ListenAddress() string
- func (d *Daemon) MustClient() peer.Client
- func (d *Daemon) NewGroup(name string, cacheBytes int64, getter Getter) (transport.Group, error)
- func (d *Daemon) RemoveGroup(name string)
- func (d *Daemon) SetPeers(ctx context.Context, src []peer.Info) error
- func (d *Daemon) Shutdown(ctx context.Context) error
- func (d *Daemon) Start(ctx context.Context) error
- type ErrRemoteCall
- type Getter
- type GetterFunc
- type Group
- type GroupStats
- type Instance
- func (i *Instance) GetGroup(name string) transport.Group
- func (i *Instance) NewGroup(name string, cacheBytes int64, getter Getter) (Group, error)
- func (i *Instance) PickPeer(key string) (peer.Client, bool)
- func (i *Instance) RemoveGroup(name string)
- func (i *Instance) SetPeers(ctx context.Context, peers []peer.Info) error
- type Logger
- type MultiError
- type Options
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache interface { // Get returns a ByteView from the cache Get(string) (transport.ByteView, bool) // Add adds a ByteView to the cache Add(string, transport.ByteView) // Stats returns stats about the current cache Stats() CacheStats // Remove removes a ByteView from the cache Remove(string) // Bytes returns the total number of bytes in the cache Bytes() int64 // Close closes the cache. The implementation should shut down any // background operations. Close() }
Cache is the interface a cache should implement
type CacheStats ¶
type CacheStats struct { // Rejected is a counter of the total number of items that were not added to // the cache due to some consideration of the underlying cache implementation. Rejected int64 // Bytes is a gauge of how many bytes are in the cache Bytes int64 // Items is a gauge of how many items are in the cache Items int64 // Gets reports the total get requests Gets int64 // Hits reports the total successful cache hits Hits int64 // Evictions reports the total number of evictions Evictions int64 }
CacheStats are returned by stats accessors on Group.
type Daemon ¶
type Daemon struct {
// contains filtered or unexported fields
}
Daemon is an instance of groupcache bound to a port listening for requests
func ListenAndServe ¶
ListenAndServe creates a new instance of groupcache listening on the address provided
func (*Daemon) GetGroup ¶
GetGroup is a convenience method which calls GetGroup on the instance associated with this daemon
func (*Daemon) GetInstance ¶
GetInstance returns the current groupcache instance associated with this daemon
func (*Daemon) ListenAddress ¶
ListenAddress returns the address this instance is listening on
func (*Daemon) MustClient ¶
MustClient is a convenience method which creates a new client for this instance. This method will panic if transport.NewClient() returns an error.
func (*Daemon) NewGroup ¶
NewGroup is a convenience method which calls NewGroup on the instance associated with this daemon.
func (*Daemon) RemoveGroup ¶
RemoveGroup is a convenience method which calls RemoveGroup on the instance associated with this daemon
func (*Daemon) SetPeers ¶
SetPeers is a convenience method which calls SetPeers on the instance associated with this daemon. In addition, it finds and marks this instance as self by asking the transport for it's listening address before calling SetPeers() on the instance. If this is not desirable, call Daemon.GetInstance().SetPeers() instead.
type ErrRemoteCall ¶
type ErrRemoteCall struct {
Msg string
}
ErrRemoteCall is returned from `group.Get()` when a remote GetterFunc returns an error. When this happens `group.Get()` does not attempt to retrieve the value via our local GetterFunc.
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.Context, key string, dest transport.Sink) error }
A Getter loads data for a key.
type GetterFunc ¶
A GetterFunc implements Getter with a function.
type Group ¶
type Group interface { Set(context.Context, string, []byte, time.Time, bool) error Get(context.Context, string, transport.Sink) error Remove(context.Context, string) error UsedBytes() (int64, int64) Name() string }
Group is the user facing interface for a group
type GroupStats ¶
type GroupStats struct { Gets AtomicInt // any Get request, including from peers CacheHits AtomicInt // either cache was good GetFromPeersLatencyLower AtomicInt // slowest duration to request value from peers 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 }
GroupStats are per-group statistics.
type Instance ¶
type Instance struct {
// contains filtered or unexported fields
}
Instance of groupcache
func New ¶
New instantiates a new Instance of groupcache with the provided options
Example ¶
ExampleNew demonstrates starting a groupcache http instance with its own listener.
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) // Starts an instance of groupcache with the provided transport d, err := groupcache.ListenAndServe(ctx, "192.168.1.1:8080", groupcache.Options{ // If transport is nil, defaults to HttpTransport Transport: nil, // The following are all optional HashFn: fnv1.HashBytes64, Logger: slog.Default(), Replicas: 50, }) cancel() if err != nil { log.Fatal("while starting server on 192.168.1.1:8080") } // Create a new group cache with a max cache size of 3MB group, err := d.NewGroup("users", 3000000, groupcache.GetterFunc( func(ctx context.Context, id string, dest transport.Sink) error { // Set the user in the groupcache to expire after 5 minutes if err := dest.SetString("hello", time.Now().Add(time.Minute*5)); err != nil { return err } return nil }, )) if err != nil { log.Fatal(err) } ctx, cancel = context.WithTimeout(context.Background(), time.Second) defer cancel() var value string if err := group.Get(ctx, "12345", transport.StringSink(&value)); err != nil { log.Fatal(err) } fmt.Printf("Value: %s\n", value) // Remove the key from the groupcache if err := group.Remove(ctx, "12345"); err != nil { fmt.Printf("Remove Err: %s\n", err) log.Fatal(err) } // Shutdown the daemon _ = d.Shutdown(context.Background())
Output:
func (*Instance) GetGroup ¶
GetGroup returns the named group previously created with NewGroup, or nil if there's no such group.
func (*Instance) 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 (*Instance) RemoveGroup ¶
RemoveGroup removes group from group pool
type MultiError ¶
type MultiError struct {
// contains filtered or unexported fields
}
func (*MultiError) Add ¶
func (m *MultiError) Add(err error)
func (*MultiError) Error ¶
func (m *MultiError) Error() string
func (*MultiError) NilOrError ¶
func (m *MultiError) NilOrError() error
type Options ¶
type Options struct { // HashFn is a function type that is used to calculate a hash used in the hash ring // Default is fnv1.HashBytes64 HashFn peer.HashFn // Replicas is the number of replicas that will be used in the hash ring // Default is 50 Replicas int // Logger is the logger that will be used by groupcache // Default is slog.Default() Logger Logger // Transport is the transport groupcache will use to communicate with peers in the cluster // Default is transport.HttpTransport Transport transport.Transport // CacheFactory returns a new instance of Cache which will be used by groupcache for both // the main and hot cache. CacheFactory func(maxBytes int64) (Cache, error) }
Options is the configuration for this instance of groupcache
Directories ¶
Path | Synopsis |
---|---|
Package cluster contains convince functions which make managing the creation of multiple groupcache instances simple.
|
Package cluster contains convince functions which make managing the creation of multiple groupcache instances simple. |
cmd
|
|
server
main package is intended for testing the interaction between instances.
|
main package is intended for testing the interaction between instances. |
internal
|
|
lru
Package lru implements an LRU cache.
|
Package lru implements an LRU cache. |
singleflight
Package singleflight provides a duplicate function call suppression mechanism.
|
Package singleflight provides a duplicate function call suppression mechanism. |