cache.go
This file
Path
src/cache.go
53 lines · 1.1 KiB · Go
Last changed by
Micro-optimizationsJunegunn Choibefore adoption37dc273
History of this fileTake it
The walk stopped after 200 commits: lines older than 971ea22 point at it.
Merge pull request #433 from pokey/masterJunegunn Choibefore adoption971ea22
lines 1–5why, one step back1package fzf23import "sync"45// queryCache associates strings to lists of itemsMicro-optimizationsJunegunn Choibefore adoption37dc273
line 6why, one step back6type queryCache map[string][]*ResultMerge pull request #433 from pokey/masterJunegunn Choibefore adoption971ea22
lines 7–19why, one step back78// ChunkCache associates Chunk and query string to lists of items9type ChunkCache struct {10 mutex sync.Mutex11 cache map[*Chunk]*queryCache12}1314// NewChunkCache returns a new ChunkCache15func NewChunkCache() ChunkCache {16 return ChunkCache{sync.Mutex{}, make(map[*Chunk]*queryCache)}17}1819// Add adds the list to the cacheMicro-optimizationsJunegunn Choibefore adoption37dc273
line 20why, one step back20func (cc *ChunkCache) Add(chunk *Chunk, key string, list []*Result) {Merge pull request #433 from pokey/masterJunegunn Choibefore adoption971ea22
lines 21–36why, one step back21 if len(key) == 0 || !chunk.IsFull() || len(list) > queryCacheMax {22 return23 }2425 cc.mutex.Lock()26 defer cc.mutex.Unlock()2728 qc, ok := cc.cache[chunk]29 if !ok {30 cc.cache[chunk] = &queryCache{}31 qc = cc.cache[chunk]32 }33 (*qc)[key] = list34}3536// Find is called to lookup ChunkCacheMicro-optimizationsJunegunn Choibefore adoption37dc273
line 37why, one step back37func (cc *ChunkCache) Find(chunk *Chunk, key string) ([]*Result, bool) {Merge pull request #433 from pokey/masterJunegunn Choibefore adoption971ea22
lines 38–53why, one step back38 if len(key) == 0 || !chunk.IsFull() {39 return nil, false40 }4142 cc.mutex.Lock()43 defer cc.mutex.Unlock()4445 qc, ok := cc.cache[chunk]46 if ok {47 list, ok := (*qc)[key]48 if ok {49 return list, true50 }51 }52 return nil, false53}