golang.org/x/tools/gopls@v0.15.3/internal/cache/cache.go (about) 1 // Copyright 2019 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package cache 6 7 import ( 8 "reflect" 9 "strconv" 10 "sync/atomic" 11 12 "golang.org/x/tools/gopls/internal/protocol/command" 13 "golang.org/x/tools/internal/imports" 14 "golang.org/x/tools/internal/memoize" 15 ) 16 17 // New Creates a new cache for gopls operation results, using the given file 18 // set, shared store, and session options. 19 // 20 // Both the fset and store may be nil, but if store is non-nil so must be fset 21 // (and they must always be used together), otherwise it may be possible to get 22 // cached data referencing token.Pos values not mapped by the FileSet. 23 func New(store *memoize.Store) *Cache { 24 index := atomic.AddInt64(&cacheIndex, 1) 25 26 if store == nil { 27 store = &memoize.Store{} 28 } 29 30 c := &Cache{ 31 id: strconv.FormatInt(index, 10), 32 store: store, 33 memoizedFS: newMemoizedFS(), 34 modCache: &sharedModCache{ 35 caches: make(map[string]*imports.DirInfoCache), 36 timers: make(map[string]*refreshTimer), 37 }, 38 } 39 return c 40 } 41 42 // A Cache holds content that is shared across multiple gopls sessions. 43 type Cache struct { 44 id string 45 46 // store holds cached calculations. 47 // 48 // TODO(rfindley): at this point, these are not important, as we've moved our 49 // content-addressable cache to the file system (the filecache package). It 50 // is unlikely that this shared cache provides any shared value. We should 51 // consider removing it, replacing current uses with a simpler futures cache, 52 // as we've done for e.g. type-checked packages. 53 store *memoize.Store 54 55 // memoizedFS holds a shared file.Source that caches reads. 56 // 57 // Reads are invalidated when *any* session gets a didChangeWatchedFile 58 // notification. This is fine: it is the responsibility of memoizedFS to hold 59 // our best knowledge of the current file system state. 60 *memoizedFS 61 62 // modCache holds the 63 modCache *sharedModCache 64 } 65 66 var cacheIndex, sessionIndex, viewIndex int64 67 68 func (c *Cache) ID() string { return c.id } 69 func (c *Cache) MemStats() map[reflect.Type]int { return c.store.Stats() } 70 71 // FileStats returns information about the set of files stored in the cache. 72 // It is intended for debugging only. 73 func (c *Cache) FileStats() (stats command.FileStats) { 74 stats.Total, stats.Largest, stats.Errs = c.fileStats() 75 return 76 }