Skip to content

Commit

Permalink
reforge bats cache
Browse files Browse the repository at this point in the history
(cherry picked from commit 9129c6e)
  • Loading branch information
AZ-X committed Jul 6, 2022
1 parent 00eb706 commit 023a774
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions repique/conceptions/bats_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,21 @@ func NewCache(size int) *Cache {
return nil
},
}

for {
select {
case kv := <-cache.set:
func() {
value, _ := kv.V.load()
if fresh == nil {
fresh = make(map[interface{}]*entry)
snapshot := cache.snapshot.Load().(map[interface{}]*entry)

if e, ok := snapshot[kv.K]; ok && e.tryStore(&value) {
return
}
if len(snapshot) == len(r.entries) {
fresh = make(map[interface{}]*entry, len(r.entries))
} else {
fresh = make(map[interface{}]*entry)
}
for k, v := range snapshot {
fresh[k] = v
}
Expand All @@ -82,16 +84,20 @@ func NewCache(size int) *Cache {
fresh = nil
}
}()
if e, ok := fresh[kv.K]; ok && e.tryStore(&value) {
found := false
if e, found := fresh[kv.K]; found && e.tryStore(&value) {
return
}
fresh[kv.K] = kv.V
if k := r.reload(kv.K); k != nil {
if e, ok := fresh[k]; ok {
e.delete()
delete(fresh, k)
}
}
if !found && len(fresh) == len(r.entries) {
panic("key not found in cache")
}
fresh[kv.K] = kv.V
}()
}
}
Expand Down

0 comments on commit 023a774

Please sign in to comment.