mirror of
https://github.com/gofiber/storage.git
synced 2025-10-05 16:48:25 +08:00
🐛 add Get exp checking
This commit is contained in:
@@ -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,
|
||||||
|
Reference in New Issue
Block a user