🐛 add Get exp checking

This commit is contained in:
Wei Lun
2020-11-03 18:13:48 +08:00
parent d090db07f3
commit 67e2cf0978

View File

@@ -18,9 +18,9 @@ type Storage struct {
type MongoStorage struct { type MongoStorage struct {
ObjectID primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"` ObjectID primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"`
Key string `json:"key,omitempty" bson:"key"` Key string `json:"key" bson:"key"`
Value []byte `json:"value" bson:"value"` Value []byte `json:"value" bson:"value"`
Exp time.Time `json:"exp" bson:"exp,omitempty"` Exp time.Time `json:"exp,omitempty" bson:"exp,omitempty"`
} }
// New creates a new MongoDB storage // New creates a new MongoDB storage
@@ -112,12 +112,16 @@ func (s *Storage) Get(key string) ([]byte, error) {
return nil, err return nil, err
} }
if result.Exp.Unix() > 0 && result.Exp.Unix() <= time.Now().Unix() {
return nil, nil
}
return result.Value, nil return result.Value, nil
} }
// Set key with value, replace if document exits // Set key with value, replace if document exits
func (s *Storage) Set(key string, val []byte, exp time.Duration) error { func (s *Storage) Set(key string, val []byte, exp time.Duration) error {
filter := bson.M{"id": key} filter := bson.M{"key": key}
replace := MongoStorage{ replace := MongoStorage{
Key: key, Key: key,
Value: val, Value: val,