loc.go (1045B)
1 package pqtime 2 3 import ( 4 "sync" 5 "time" 6 ) 7 8 // The location cache caches the time zones typically used by the client. 9 type locationCache struct { 10 cache map[int]*time.Location 11 lock sync.Mutex 12 } 13 14 // All connections share the same list of timezones. Benchmarking shows that 15 // about 5% speed could be gained by putting the cache in the connection and 16 // losing the mutex, at the cost of a small amount of memory and a somewhat 17 // significant increase in code complexity. 18 var globalLocationCache = &locationCache{cache: make(map[int]*time.Location)} 19 20 func Reset() { 21 globalLocationCache = &locationCache{cache: make(map[int]*time.Location)} 22 } 23 24 // Returns the cached timezone for the specified offset, creating and caching 25 // it if necessary. 26 func (c *locationCache) getLocation(offset int) *time.Location { 27 c.lock.Lock() 28 defer c.lock.Unlock() 29 l, ok := c.cache[offset] 30 if !ok { 31 // TODO(v2): for offset=0 it should use some descriptive text like 32 // "without time zone". 33 l = time.FixedZone("", offset) 34 c.cache[offset] = l 35 } 36 return l 37 }