diff options
| author | Junegunn Choi <junegunn.c@gmail.com> | 2015-01-02 04:49:30 +0900 |
|---|---|---|
| committer | Junegunn Choi <junegunn.c@gmail.com> | 2015-01-04 00:37:29 +0900 |
| commit | f3177305d5572b26f135fc045481358b4eb1bf69 (patch) | |
| tree | d59fd9587e44e998581a131875bf45e243df6c6e /src/cache.go | |
| parent | 7ba93d9f8351be64b37c65ae04d594ee261d5d26 (diff) | |
| download | fzf-f3177305d5572b26f135fc045481358b4eb1bf69.tar.gz | |
Rewrite fzf in Go
Diffstat (limited to 'src/cache.go')
| -rw-r--r-- | src/cache.go | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/cache.go b/src/cache.go new file mode 100644 index 00000000..340f3258 --- /dev/null +++ b/src/cache.go @@ -0,0 +1,47 @@ +package fzf + +import "sync" + +type QueryCache map[string][]*Item +type ChunkCache struct { + mutex sync.Mutex + cache map[*Chunk]*QueryCache +} + +func NewChunkCache() ChunkCache { + return ChunkCache{sync.Mutex{}, make(map[*Chunk]*QueryCache)} +} + +func (cc *ChunkCache) Add(chunk *Chunk, key string, list []*Item) { + if len(key) == 0 || !chunk.IsFull() { + return + } + + cc.mutex.Lock() + defer cc.mutex.Unlock() + + qc, ok := cc.cache[chunk] + if !ok { + cc.cache[chunk] = &QueryCache{} + qc = cc.cache[chunk] + } + (*qc)[key] = list +} + +func (cc *ChunkCache) Find(chunk *Chunk, key string) ([]*Item, bool) { + 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 +} |
