forked from gsn/predictor
feat: remove redis
This commit is contained in:
parent
7a9f81e527
commit
a850615e1f
18 changed files with 170 additions and 1142 deletions
|
|
@ -3,7 +3,6 @@ package grib
|
|||
import (
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"math"
|
||||
"net/http"
|
||||
"os"
|
||||
|
|
@ -17,12 +16,6 @@ import (
|
|||
"github.com/nilsmagnus/grib/griblib"
|
||||
)
|
||||
|
||||
type RedisIface interface {
|
||||
Lock(ctx context.Context, key string, ttl time.Duration) (func(context.Context), error)
|
||||
Set(key string, value []byte, ttl time.Duration) error
|
||||
Get(key string) ([]byte, error)
|
||||
}
|
||||
|
||||
type Service interface {
|
||||
Update(ctx context.Context) error
|
||||
Extract(ctx context.Context, lat, lon, alt float64, ts time.Time) ([2]float64, error)
|
||||
|
|
@ -34,7 +27,6 @@ type ServiceConfig struct {
|
|||
Dir string
|
||||
TTL time.Duration
|
||||
CacheTTL time.Duration
|
||||
Redis RedisIface
|
||||
Parallel int
|
||||
Client *http.Client
|
||||
DatasetURL string
|
||||
|
|
@ -134,12 +126,6 @@ func (s *service) Update(ctx context.Context) error {
|
|||
}
|
||||
}
|
||||
|
||||
unlock, err := s.cfg.Redis.Lock(ctx, "grib-dl", 45*time.Minute)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer unlock(ctx)
|
||||
|
||||
// Check again after acquiring lock (double-checked locking pattern)
|
||||
if d := s.data.Load(); d != nil {
|
||||
runTime := time.Unix(d.runUTC, 0)
|
||||
|
|
@ -290,29 +276,6 @@ func (s *service) Extract(ctx context.Context, lat, lon, alt float64, ts time.Ti
|
|||
return [2]float64(v), nil
|
||||
}
|
||||
|
||||
// Try Redis cache
|
||||
redisKey := fmt.Sprintf("grib:extract:%d", key)
|
||||
if cached, err := s.cfg.Redis.Get(redisKey); err == nil {
|
||||
var result [2]float64
|
||||
if len(cached) == 16 {
|
||||
result[0] = math.Float64frombits(binary.LittleEndian.Uint64(cached[:8]))
|
||||
result[1] = math.Float64frombits(binary.LittleEndian.Uint64(cached[8:]))
|
||||
s.cache.set(key, vec(result))
|
||||
return result, nil
|
||||
} else {
|
||||
// Cache data is corrupted (wrong length)
|
||||
return zero, errcodes.ErrRedisCacheCorrupted
|
||||
}
|
||||
} else {
|
||||
// Check if it's a cache miss (expected error)
|
||||
if errcode, ok := errcodes.AsErr(err); ok && errcode == errcodes.ErrRedisCacheMiss {
|
||||
// Cache miss is expected, continue with calculation
|
||||
} else {
|
||||
// Unexpected error, return it
|
||||
return zero, err
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate result
|
||||
td := ts.Sub(time.Unix(d.runUTC, 0)).Hours()
|
||||
u, v := d.uv(lat, lon, alt, td)
|
||||
|
|
@ -321,12 +284,6 @@ func (s *service) Extract(ctx context.Context, lat, lon, alt float64, ts time.Ti
|
|||
// Cache in memory
|
||||
s.cache.set(key, vec(out))
|
||||
|
||||
// Cache in Redis
|
||||
encoded := make([]byte, 16)
|
||||
binary.LittleEndian.PutUint64(encoded[:8], math.Float64bits(out[0]))
|
||||
binary.LittleEndian.PutUint64(encoded[8:], math.Float64bits(out[1]))
|
||||
s.cfg.Redis.Set(redisKey, encoded, s.cfg.CacheTTL)
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue