Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Honor current TTL in DMap.Incr and DMap.Decr methods #172

Merged
merged 6 commits into from
Aug 4, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions internal/dmap/atomic.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,31 @@ import (
"context"
"errors"
"fmt"
"time"

"github.com/buraksezer/olric/internal/protocol"
"github.com/buraksezer/olric/internal/resp"
"github.com/buraksezer/olric/internal/util"
"github.com/buraksezer/olric/pkg/storage"
)

func (dm *DMap) loadCurrentAtomicInt(e *env) (int, error) {
func (dm *DMap) loadCurrentAtomicInt(e *env) (int, int64, error) {
entry, err := dm.Get(e.ctx, e.key)
if errors.Is(err, ErrKeyNotFound) {
return 0, nil
return 0, 0, nil
}
if err != nil {
return 0, err
return 0, 0, err
}

if entry == nil {
return 0, nil
return 0, 0, nil
}
nr, err := util.ParseInt(entry.Value(), 10, 64)
if err != nil {
return 0, nil
return 0, 0, nil
}
return int(nr), nil
return int(nr), entry.TTL(), nil
}

func (dm *DMap) atomicIncrDecr(cmd string, e *env, delta int) (int, error) {
Expand All @@ -53,7 +55,7 @@ func (dm *DMap) atomicIncrDecr(cmd string, e *env, delta int) (int, error) {
}
}()

current, err := dm.loadCurrentAtomicInt(e)
current, ttl, err := dm.loadCurrentAtomicInt(e)
if err != nil {
return 0, err
}
Expand All @@ -79,6 +81,10 @@ func (dm *DMap) atomicIncrDecr(cmd string, e *env, delta int) (int, error) {
pool.Put(valueBuf)
}()

if ttl != 0 {
e.putConfig.HasEX = true
hasit marked this conversation as resolved.
Show resolved Hide resolved
e.putConfig.EX = time.Until(time.UnixMilli(ttl))
}
err = dm.put(e)
if err != nil {
return 0, err
Expand Down