cache.go (592B)
1 package memstore 2 3 import ( 4 "sync" 5 ) 6 7 type cache struct { 8 data map[string]valueType 9 mutex sync.RWMutex 10 } 11 12 func newCache() *cache { 13 return &cache{ 14 data: make(map[string]valueType), 15 } 16 } 17 18 func (c *cache) value(name string) (valueType, bool) { 19 c.mutex.RLock() 20 defer c.mutex.RUnlock() 21 22 v, ok := c.data[name] 23 return v, ok 24 } 25 26 func (c *cache) setValue(name string, value valueType) { 27 c.mutex.Lock() 28 defer c.mutex.Unlock() 29 30 c.data[name] = value 31 } 32 33 func (c *cache) delete(name string) { 34 c.mutex.Lock() 35 defer c.mutex.Unlock() 36 37 if _, ok := c.data[name]; ok { 38 delete(c.data, name) 39 } 40 }