Skip to content

Commit

Permalink
Merge pull request #34 from exuan/fix-format-ex-px-time-duration
Browse files Browse the repository at this point in the history
fix: format ex/px time duration
  • Loading branch information
yangbodong22011 authored Feb 13, 2023
2 parents 231eb66 + 3c4d243 commit d0ce1de
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
14 changes: 14 additions & 0 deletions tair/taircommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,17 @@ type TairCmdable interface {
BfInsert(ctx context.Context, key string, bfInsertArgs *BfInsertArgs, items ...string) *redis.BoolSliceCmd
BfDebug(ctx context.Context, key string) *redis.StringSliceCmd
}

func toMs(dur time.Duration) int64 {
if dur > 0 && dur < time.Millisecond {
return 1
}
return int64(dur / time.Millisecond)
}

func toSec(dur time.Duration) int64 {
if dur > 0 && dur < time.Second {
return 1
}
return int64(dur / time.Second)
}
7 changes: 4 additions & 3 deletions tair/taircpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package tair

import (
"context"
"github.com/go-redis/redis/v8"
"strconv"
"time"

"github.com/go-redis/redis/v8"
)

type CpcData struct {
Expand Down Expand Up @@ -164,10 +165,10 @@ func (a *CpcUpdateArgs) GetArgs() []interface{} {
args = append(args, PXAT, a.pxAt.Unix())
}
if _, ok := a.Set[EX]; ok {
args = append(args, EX, a.ex)
args = append(args, EX, toSec(a.ex))
}
if _, ok := a.Set[PX]; ok {
args = append(args, PX, a.px)
args = append(args, PX, toMs(a.px))
}
if _, ok := a.Set[WIN]; ok {
args = append(args, WIN, a.winSize)
Expand Down
11 changes: 6 additions & 5 deletions tair/tairhash.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package tair

import (
"context"
"github.com/go-redis/redis/v8"
"time"

"github.com/go-redis/redis/v8"
)

type ExHSetArgs struct {
Expand Down Expand Up @@ -79,10 +80,10 @@ func (a *ExHSetArgs) GetArgs() []interface{} {
args = append(args, NX)
}
if _, ok := a.Set[EX]; ok {
args = append(args, EX, a.ex)
args = append(args, EX, toSec(a.ex))
}
if _, ok := a.Set[PX]; ok {
args = append(args, PX, a.px)
args = append(args, PX, toMs(a.px))
}
if _, ok := a.Set[EXAT]; ok {
args = append(args, EXAT, a.exAt.Unix())
Expand Down Expand Up @@ -224,10 +225,10 @@ func (a *ExHIncrArgs) KeepTTL() *ExHIncrArgs {
func (a ExHIncrArgs) GetArgs() []interface{} {
args := make([]interface{}, 0)
if _, ok := a.Set[EX]; ok {
args = append(args, EX, a.ex)
args = append(args, EX, toSec(a.ex))
}
if _, ok := a.Set[PX]; ok {
args = append(args, PX, a.px)
args = append(args, PX, toMs(a.px))
}
if _, ok := a.Set[EXAT]; ok {
args = append(args, EXAT, a.exAt.Unix())
Expand Down
15 changes: 8 additions & 7 deletions tair/tairstring.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package tair

import (
"context"
"github.com/go-redis/redis/v8"
"time"

"github.com/go-redis/redis/v8"
)

type ExSetArgs struct {
Expand Down Expand Up @@ -96,10 +97,10 @@ func (a *ExSetArgs) GetArgs() []interface{} {
args = append(args, NX)
}
if _, ok := a.Set[EX]; ok {
args = append(args, EX, a.ex)
args = append(args, EX, toSec(a.ex))
}
if _, ok := a.Set[PX]; ok {
args = append(args, PX, a.px)
args = append(args, PX, toMs(a.px))
}
if _, ok := a.Set[EXAT]; ok {
args = append(args, EXAT, a.exAt.Unix())
Expand Down Expand Up @@ -176,10 +177,10 @@ func (a *CasArgs) GetArgs() []interface{} {
args = append(args, PXAT, a.pxAt.Unix())
}
if _, ok := a.Set[EX]; ok {
args = append(args, EX, a.ex)
args = append(args, EX, toSec(a.ex))
}
if _, ok := a.Set[PX]; ok {
args = append(args, PX, a.px)
args = append(args, PX, toMs(a.px))
}
if _, ok := a.Set[KEEPTTL]; ok {
args = append(args, KEEPTTL)
Expand Down Expand Up @@ -297,10 +298,10 @@ func (a ExIncrByArgs) GetArgs() []interface{} {
args = append(args, NX)
}
if _, ok := a.Set[EX]; ok {
args = append(args, EX, a.ex)
args = append(args, EX, toSec(a.ex))
}
if _, ok := a.Set[PX]; ok {
args = append(args, PX, a.px)
args = append(args, PX, toMs(a.px))
}
if _, ok := a.Set[EXAT]; ok {
args = append(args, EXAT, a.exAt.Unix())
Expand Down

0 comments on commit d0ce1de

Please sign in to comment.