Ai
2 Star 0 Fork 0

mirrors_sourcegraph/multicache

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
fallback.go 1.29 KB
一键复制 编辑 原始数据 按行查看 历史
// Package multicache provides a "fallback" cache implementation that
// short-circuits gets and writes/deletes to all underlying caches.
package multicache // import "sourcegraph.com/sourcegraph/multicache"
// Fallback is a cache that wraps a list of caches. Gets read from the caches in
// sequence until a cache entry is found. Sets write to all caches, returning
// after the first WaitNSets Set operations complete. Deletes delete from all
// caches, returning after the first WaitNDeletes Delete operations complete.
type Fallback struct {
caches []Underlying
WaitNSets int
WaitNDeletes int
}
func (f *Fallback) Get(key string) (resp []byte, ok bool) {
for _, c := range f.caches {
resp, ok = c.Get(key)
if ok {
return
}
}
return
}
func (f *Fallback) Set(key string, resp []byte) {
for i, c := range f.caches {
if i < f.WaitNSets {
c.Set(key, resp)
} else {
go c.Set(key, resp)
}
}
}
func (f *Fallback) Delete(key string) {
for i, c := range f.caches {
if i < f.WaitNDeletes {
c.Delete(key)
} else {
go c.Delete(key)
}
}
}
// NewFallback returns a new Fallback cache with WaitNSets == WaitNDeletes ==
// len(caches).
func NewFallback(caches ...Underlying) *Fallback {
return &Fallback{caches: caches, WaitNSets: len(caches), WaitNDeletes: len(caches)}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors_sourcegraph/multicache.git
git@gitee.com:mirrors_sourcegraph/multicache.git
mirrors_sourcegraph
multicache
multicache
master

搜索帮助