40 lines
667 B
Go
40 lines
667 B
Go
package grib
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"math"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
type vec [2]float64
|
|
|
|
type item struct {
|
|
v vec
|
|
exp time.Time
|
|
}
|
|
|
|
type memCache struct {
|
|
ttl time.Duration
|
|
m sync.Map
|
|
}
|
|
|
|
func (c *memCache) get(k uint64) (vec, bool) {
|
|
if v, ok := c.m.Load(k); ok {
|
|
it := v.(item)
|
|
if time.Now().Before(it.exp) {
|
|
return it.v, true
|
|
}
|
|
c.m.Delete(k)
|
|
}
|
|
return vec{}, false
|
|
}
|
|
|
|
func (c *memCache) set(k uint64, v vec) { c.m.Store(k, item{v, time.Now().Add(c.ttl)}) }
|
|
|
|
func encodeVec(v vec) []byte {
|
|
var b [16]byte
|
|
binary.LittleEndian.PutUint64(b[:8], math.Float64bits(v[0]))
|
|
binary.LittleEndian.PutUint64(b[8:], math.Float64bits(v[1]))
|
|
return b[:]
|
|
}
|