github.com/boyter/gocodewalker@v1.3.2/go-gitignore/cache.go (about) 1 // SPDX-License-Identifier: MIT 2 3 package gitignore 4 5 import ( 6 "sync" 7 ) 8 9 // Cache is the interface for the GitIgnore cache 10 type Cache interface { 11 // Set stores the GitIgnore ignore against its path. 12 Set(path string, ig GitIgnore) 13 14 // Get attempts to retrieve an GitIgnore instance associated with the given 15 // path. If the path is not known nil is returned. 16 Get(path string) GitIgnore 17 } 18 19 // cache is the default thread-safe cache implementation 20 type cache struct { 21 _i map[string]GitIgnore 22 _lock sync.Mutex 23 } 24 25 // NewCache returns a Cache instance. This is a thread-safe, in-memory cache 26 // for GitIgnore instances. 27 func NewCache() Cache { 28 return &cache{} 29 } // Cache() 30 31 // Set stores the GitIgnore ignore against its path. 32 func (c *cache) Set(path string, ignore GitIgnore) { 33 if ignore == nil { 34 return 35 } 36 37 // ensure the map is defined 38 if c._i == nil { 39 c._i = make(map[string]GitIgnore) 40 } 41 42 // set the cache item 43 c._lock.Lock() 44 c._i[path] = ignore 45 c._lock.Unlock() 46 } // Set() 47 48 // Get attempts to retrieve an GitIgnore instance associated with the given 49 // path. If the path is not known nil is returned. 50 func (c *cache) Get(path string) GitIgnore { 51 c._lock.Lock() 52 _ignore, _ok := c._i[path] 53 c._lock.Unlock() 54 if _ok { 55 return _ignore 56 } else { 57 return nil 58 } 59 } // Get() 60 61 // ensure cache supports the Cache interface 62 var _ Cache = &cache{}