// Map constains all hashed keystypeMapstruct {
hashHash// Hash functionreplicasint// Virtual node multiplekeys []int// SortedhashMapmap[int]string// Mapping Table of Virtual Node and Real Node
}
// New creates a Map instancefuncNew(replicasint, fnHash) *Map// Add adds some keys to the hash.func (m*Map) Add(keys...string)
// Get gets the closest item in the hash to the provided key.func (m*Map) Get(keystring) string
// Cache is a LRU/FIFO cache. It is not safe for concurrent access.typeCachestruct {
eliminationstrategyintmaxBytesint64nbytesint64ll*list.Listcachemap[string]*list.Element// optional and executed when an entry is purged.OnEvictedfunc(keystring, valueValue)
}
// New is the Constructor of CachefuncNew(maxBytesint64, eliminationstrategyint, onEvictedfunc(string, Value)) *Cache// Get look ups a key's valuefunc (c*Cache) Get(keystring) (valueValue, okbool)
// Add adds a value to the cache.func (c*Cache) Add(keystring, valueValue)
typeGroupstruct {
mu sync.Mutex// protects mmmap[string]*call
}
// Do only initiate one request to the same keyfunc (g*Group) Do(keystring, fnfunc() (interface{}, error)) (interface{}, error)
byteview.go实现了 ByteView 用来表示缓存值,对外暴露的数据结构及方法包括:
// A ByteView holds an immutable view of bytes.typeByteViewstruct {
b []byte
}
// Len returns the view's lengthfunc (vByteView) Len() int// ByteSlice returns a copy of the data as a byte slice.func (vByteView) ByteSlice() []byte// String returns the data as a string, making a copy if necessary.func (vByteView) String() string
// A Getter loads data for a key.typeGetterinterface {
Get(keystring) ([]byte, error)
}
// A GetterFunc implements Getter with a function.typeGetterFuncfunc(keystring) ([]byte, error)
// Get implements Getter interface functionfunc (fGetterFunc) Get(keystring) ([]byte, error) {
returnf(key)
}
// A Group is a cache namespace and associated data loaded spread overtypeGroupstruct {
namestringgetterGettermainCachecachepeersPeerPicker// use singleflight.Group to make sure that each key is only fetched onceloader*singleflight.Group
}
// NewGroup create a new instance of GroupfuncNewGroup(namestring, esint, cacheBytesint64, getterGetter) *Group// GetGroup returns the named group previously created with NewGroup, or// nil if there's no such group.funcGetGroup(namestring) *Group// Get value for a key from cachefunc (g*Group) Get(keystring) (ByteView, error)
// RegisterPeers registers a PeerPicker for choosing remote peerfunc (g*Group) RegisterPeers(peersPeerPicker)