Skip to content
Myr

cache.go

This file

Path

src/cache.go

81 lines · 1.6 KiB · Go

Last changed by

Consolidate Result and rank structsJunegunn Choibefore adoptionbbe10f4

History of this file

Take it

The walk stopped after 200 commits: lines older than e1291aa point at it.

Fix make deps to see the right git dirJunegunn Choibefore adoptione1291aa

lines 1–5why, one step back
1package fzf
23import "sync"45// queryCache associates strings to lists of items

Consolidate Result and rank structsJunegunn Choibefore adoptionbbe10f4

line 6why, one step back
6type queryCache map[string][]Result

Fix make deps to see the right git dirJunegunn Choibefore adoptione1291aa

lines 7–19why, one step back
7
8// 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 cache

Consolidate Result and rank structsJunegunn Choibefore adoptionbbe10f4

line 20why, one step back
20func (cc *ChunkCache) Add(chunk *Chunk, key string, list []Result) {

Fix make deps to see the right git dirJunegunn Choibefore adoptione1291aa

lines 21–35why, one step back
21 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}35

Reduce memory footprint of Item structJunegunn Choibefore adoption9e85cba

line 36why, one step back
36// Lookup is called to lookup ChunkCache

Consolidate Result and rank structsJunegunn Choibefore adoptionbbe10f4

line 37why, one step back
37func (cc *ChunkCache) Lookup(chunk *Chunk, key string) []Result {

Fix make deps to see the right git dirJunegunn Choibefore adoptione1291aa

line 38why, one step back
38 if len(key) == 0 || !chunk.IsFull() {

Refactor cache lookupJunegunn Choibefore adoption8dbdd55

line 39why, one step back
39 return nil

Fix make deps to see the right git dirJunegunn Choibefore adoptione1291aa

lines 40–48why, one step back
40 }
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 {

Refactor cache lookupJunegunn Choibefore adoption8dbdd55

line 49why, one step back
49 return list

Fix make deps to see the right git dirJunegunn Choibefore adoptione1291aa

lines 50–51why, one step back
50 }
51 }

Refactor cache lookupJunegunn Choibefore adoption8dbdd55

lines 52–54why, one step back
52 return nil
53}54

Consolidate Result and rank structsJunegunn Choibefore adoptionbbe10f4

line 55why, one step back
55func (cc *ChunkCache) Search(chunk *Chunk, key string) []Result {

Refactor cache lookupJunegunn Choibefore adoption8dbdd55

lines 56–80why, one step back
56 if len(key) == 0 || !chunk.IsFull() {
57 return nil58 }5960 cc.mutex.Lock()61 defer cc.mutex.Unlock()6263 qc, ok := cc.cache[chunk]64 if !ok {65 return nil66 }6768 for idx := 1; idx < len(key); idx++ {69 // [---------| ] | [ |---------]70 // [--------| ] | [ |--------]71 // [-------| ] | [ |-------]72 prefix := key[:len(key)-idx]73 suffix := key[idx:]74 for _, substr := range [2]string{prefix, suffix} {75 if cached, found := (*qc)[substr]; found {76 return cached77 }78 }79 }80 return nil

Fix make deps to see the right git dirJunegunn Choibefore adoptione1291aa

line 81why, one step back
81}