wip: grib
This commit is contained in:
parent
5240968c33
commit
b9c1a98895
12 changed files with 414 additions and 5 deletions
40
internal/pkg/grib/cache.go
Normal file
40
internal/pkg/grib/cache.go
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
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[:]
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue