comparison pkg/mesh/cache.go @ 4889:ab6eb160cd29

Sounding result cache: Start background cache cleanup lazily only when the cache is really used.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 09 Feb 2020 20:24:54 +0100
parents 181c2c05b12a
children e54635c20d43
comparison
equal deleted inserted replaced
4888:af38a19f615a 4889:ab6eb160cd29
66 WHERE bottleneck_id = $1 AND date_info = $2::date 66 WHERE bottleneck_id = $1 AND date_info = $2::date
67 AND mesh_checksum IS NOT NULL AND mesh_index IS NOT NULL 67 AND mesh_checksum IS NOT NULL AND mesh_index IS NOT NULL
68 ` 68 `
69 ) 69 )
70 70
71 var cache = Cache{ 71 var cache Cache
72 entries: map[cacheKey]*cacheEntry{},
73 }
74
75 func init() {
76 go cache.background()
77 }
78 72
79 func (c *Cache) background() { 73 func (c *Cache) background() {
80 for { 74 for {
81 time.Sleep(cleanupCacheSleep) 75 time.Sleep(cleanupCacheSleep)
82 c.cleanup() 76 c.cleanup()
126 conn *sql.Conn, 120 conn *sql.Conn,
127 bottleneck string, date time.Time, 121 bottleneck string, date time.Time,
128 ) (*STRTree, error) { 122 ) (*STRTree, error) {
129 c.Lock() 123 c.Lock()
130 defer c.Unlock() 124 defer c.Unlock()
125
126 // Start background cleanup lazily.
127 if c.entries == nil {
128 c.entries = map[cacheKey]*cacheEntry{}
129 go c.background()
130 }
131 131
132 key := cacheKey{date, bottleneck} 132 key := cacheKey{date, bottleneck}
133 entry := c.entries[key] 133 entry := c.entries[key]
134 134
135 var data []byte 135 var data []byte