Documentation
¶
Overview ¶
Package groupcache 提供了一个缓存的数据加载机制, and de-duplication that works across a set of peer processes.
每一个数据都会首先向本地缓存获取数据,否则会向数据的拥有者获取 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 RegisterNewGroupHook(fn func(*Group))
- func RegisterPeerPicker(fn func() PeerPicker)
- func RegisterPerGroupPeerPicker(fn func(groupName string) PeerPicker)
- func RegisterServerStart(fn func())
- 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
- func (v ByteView) WriteTo(w io.Writer) (n int64, err error)
- type CacheStats
- type CacheType
- type Context
- type Getter
- type GetterFunc
- type Group
- type HTTPPool
- type HTTPPoolOptions
- type NoPeers
- type PeerPicker
- type ProtoGetter
- type Sink
- type Stats
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 注册一个peer初始化的方法 当第一个group被创建的时候,它就会被调用一次 RegisterPeerPicker和RegisterPerGroupPeerPicker都只能被调用一次
func RegisterPerGroupPeerPicker ¶
func RegisterPerGroupPeerPicker(fn func(groupName string) PeerPicker)
RegisterPerGroupPeerPicker registers the peer initialization function, which takes the groupName, to be used in choosing a PeerPicker. It is called once, when the first group is created. Either RegisterPeerPicker or RegisterPerGroupPeerPicker should be called exactly once, but not both.
func RegisterServerStart ¶
func RegisterServerStart(fn func())
RegisterServerStart注册了一个钩子,他会在第一个group被创建的时候运行
Types ¶
type ByteView ¶
type ByteView struct {
// contains filtered or unexported fields
}
A ByteView 是被用来作为一个value类型的,而不是一个指针类型 (例如 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 Getter ¶
type Getter interface { // Get returns the value identified by key, populating dest. // 返回的数据必须是未版本化的。 也就是说,密钥必须唯一地描述加载的数据,没有隐式当前时间,并且不依赖于缓存过期机制。 Get(ctx context.Context, key string, dest Sink) error }
获取key的数据
type GetterFunc ¶
A GetterFunc 实现了Getter接口
type Group ¶
type Group struct { // Stats 是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. Group 是一个缓存的命名空间,同时将相关联的数据加载后同步到一组机器(1个或多个机器组成)中
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.
type HTTPPool ¶
type HTTPPool struct { // Context是一个可选的配置,他是当收到request的时候,供server使用的,如果没有设置,就使用request的中的context Context func(*http.Request) context.Context // 当创建一个request的时候,可以指定一个http.RoundTripper供client使用。如果没有设置,就使用默认的http.DefaultTransport Transport func(context.Context) http.RoundTripper // contains filtered or unexported fields }
HTTPPool implements PeerPicker for a pool of HTTP peers. HTTPPool是实现了PeerPicker接口的一个HTTP连接池
func NewHTTPPool ¶
NewHTTPPool initializes an HTTP pool of peers, and registers itself as a PeerPicker. For convenience, it also registers itself as an http.Handler with http.DefaultServeMux. The self argument should be a valid base URL that points to the current server, for example "http://example.net:8000".
func NewHTTPPoolOpts ¶
func NewHTTPPoolOpts(self string, o *HTTPPoolOptions) *HTTPPool
NewHTTPPoolOpts initializes an HTTP pool of peers with the given options. Unlike NewHTTPPool, this function does not register the created pool as an HTTP handler. The returned *HTTPPool implements http.Handler and must be registered using http.Handle.
func (*HTTPPool) Set ¶
更新pool中peers列表,每一个peer必须是一个合法的URL for example "http://example.net:8000".
type HTTPPoolOptions ¶
type HTTPPoolOptions struct { // BasePath 指定了处理groupcache的请求的 HTTP 地址 // 如果没有设置,就使用默认值: "/_groupcache/". BasePath string // 指定了在一致性hash中key的副本数量,如果没有设置,就使用默认值50 Replicas int // 用于生成一致性hash值的方法,如果没有设置,就使用crc32.ChecksumIEEE. HashFn consistenthash.Hash }
HTTPPoolOptions 是HTTPPool的配置项.
type PeerPicker ¶
type PeerPicker interface { // PickPeer 返回通过key找到的peer,returns the peer that owns the specific key // and返回true的话表明有一个远端的peer被找到, true to indicate that a remote peer was nominated. // 如果通过key找到的peer是本地peer的话,就返回nil和false PickPeer(key string) (peer ProtoGetter, ok bool) }
PeerPicker 是一个接口,它必须被本地的拥有指定key的peer实现 is the interface that must be implemented to locate the peer that owns a specific key.
type ProtoGetter ¶
type ProtoGetter interface {
Get(ctx context.Context, in *pb.GetRequest, out *pb.GetResponse) error
}
ProtoGetter 是一个接口,peer必须实现这个接口
type Sink ¶
type Sink interface { // SetString 将值设置为一个string SetString(s string) error // SetBytes sets the value to the contents of v. // SetBytes将值设置为[]byte // The caller retains ownership of v. // 调用者保留对v的所有权 SetBytes(v []byte) error // SetProto sets the value to the encoded version of m. // The caller retains ownership of m. // SetProto 将值设置为已经编码后的m SetProto(m proto.Message) error // contains filtered or unexported methods }
stringSink、byteViewSink、protoSink、allocBytesSink都实现了Sink接口
func AllocatingByteSliceSink ¶
AllocatingByteSliceSink returns a Sink that allocates a byte slice to hold the received value and assigns it to *dst. 这个内存是不被groupcache保留的
func ByteViewSink ¶
ByteViewSink returns a Sink that populates a ByteView.
func StringSink ¶
StringSink returns a Sink that populates the provided string pointer. 返回的是已提供的string的指针
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 // 任何的Get请求,包括来自peers的Get 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 通过网络从peers获得请求的数量 }
Stats 是每一个group的统计信息
Directories
¶
Path | Synopsis |
---|---|
hash 环的一个实现
|
hash 环的一个实现 |
Package lru 实现了一个lru缓存
|
Package lru 实现了一个lru缓存 |
Package singleflight 提供了重复调用函数时的抑制机制
|
Package singleflight 提供了重复调用函数时的抑制机制 |