Add: RetrieveAndExtend to session manager

Merged Akshay Bharambe requested to merge ab_Add_SMGetExt into devbranch
Compare and
1 file
+ 34
0
Preferences
File browser
Compare changes
@@ -122,6 +122,40 @@ func Retrieve(key string) (Entry, error) {
}
}
// RetrieveAndExtend returns the entry and extends the entry expiration by provided `SECONDS`, only if remaining time < extendBy.
// If extendBy < 0, it is same as Retrieve function.
func RetrieveAndExtend(key string, extendBy int64) (Entry, error) {
entry, err := Retrieve(key)
if err != nil {
return Entry{}, err
}
if extendBy > 0 {
timeRemaining := entry.ExpiredAT - time.Now().Unix()
if timeRemaining < extendBy {
// update with new expiratin
entry.ExpiredAT = time.Now().Add(time.Second * time.Duration(extendBy)).Unix()
Store(key, entry)
}
}
return entry, nil
}
// RetrieveAndDelete deletes the entry after first retrieval
func RetrieveAndDelete(key string) (Entry, error) {
entry, err := Retrieve(key)
if err != nil {
return Entry{}, err
}
store.Delete(key)
return entry, nil
}
// Delete removes the entry from session manager. If the key is not present, error `ErrSessionNotFound` will be thrown. Caller can ignore error if this is acceptable.
func Delete(key string) error {
_, ok := store.Get(key)