mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 08:45:16 +00:00
FEATURE: save expiring data to redis
This commit is contained in:
parent
16fe416520
commit
4deefefe0f
|
@ -1,5 +1,7 @@
|
|||
package service
|
||||
|
||||
import "time"
|
||||
|
||||
type PersistenceService interface {
|
||||
NewStore(id string, subIDs ...string) Store
|
||||
}
|
||||
|
@ -10,6 +12,10 @@ type Store interface {
|
|||
Reset() error
|
||||
}
|
||||
|
||||
type Expirable interface {
|
||||
Expiration() time.Duration
|
||||
}
|
||||
|
||||
type RedisPersistenceConfig struct {
|
||||
Host string `yaml:"host" json:"host" env:"REDIS_HOST"`
|
||||
Port string `yaml:"port" json:"port" env:"REDIS_PORT"`
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
"errors"
|
||||
"net"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
@ -87,15 +88,20 @@ func (store *RedisStore) Save(val interface{}) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
var expiration time.Duration
|
||||
if expiringData, ok := val.(Expirable); ok {
|
||||
expiration = expiringData.Expiration()
|
||||
}
|
||||
|
||||
data, err := json.Marshal(val)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cmd := store.redis.Set(context.Background(), store.ID, data, 0)
|
||||
cmd := store.redis.Set(context.Background(), store.ID, data, expiration)
|
||||
_, err = cmd.Result()
|
||||
|
||||
redisLogger.Debugf("[redis] set key %q, data = %s", store.ID, string(data))
|
||||
redisLogger.Debugf("[redis] set key %q, data = %s, expiration = %s", store.ID, string(data), expiration)
|
||||
|
||||
return err
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user