You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
fzf/src/cache.go

54 lines
1.1 KiB
Go

10 years ago
package fzf
import "sync"
// queryCache associates strings to lists of items
type queryCache map[string][]*Result
10 years ago
// ChunkCache associates Chunk and query string to lists of items
10 years ago
type ChunkCache struct {
mutex sync.Mutex
cache map[*Chunk]*queryCache
10 years ago
}
10 years ago
// NewChunkCache returns a new ChunkCache
10 years ago
func NewChunkCache() ChunkCache {
return ChunkCache{sync.Mutex{}, make(map[*Chunk]*queryCache)}
10 years ago
}
10 years ago
// Add adds the list to the cache
func (cc *ChunkCache) Add(chunk *Chunk, key string, list []*Result) {
if len(key) == 0 || !chunk.IsFull() || len(list) > queryCacheMax {
10 years ago
return
}
cc.mutex.Lock()
defer cc.mutex.Unlock()
qc, ok := cc.cache[chunk]
if !ok {
cc.cache[chunk] = &queryCache{}
10 years ago
qc = cc.cache[chunk]
}
(*qc)[key] = list
}
10 years ago
// Find is called to lookup ChunkCache
func (cc *ChunkCache) Find(chunk *Chunk, key string) ([]*Result, bool) {
10 years ago
if len(key) == 0 || !chunk.IsFull() {
return nil, false
}
cc.mutex.Lock()
defer cc.mutex.Unlock()
qc, ok := cc.cache[chunk]
if ok {
list, ok := (*qc)[key]
if ok {
return list, true
}
}
return nil, false
}