Skip to content

Commit

Permalink
refactor: 整理代码
Browse files Browse the repository at this point in the history
  • Loading branch information
祝黄清 committed Sep 16, 2024
1 parent 3cc2bf9 commit dd7f60e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 76 deletions.
21 changes: 8 additions & 13 deletions cmd_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func (c *client) SCard(ctx context.Context, key string) IntCmd {

func (c *client) SDiff(ctx context.Context, keys ...string) StringSliceCmd {
ctx = c.handler.beforeWithKeys(ctx, CommandSDiff, func() []string { return keys })
r := wrapStringSliceCmd(c.adapter.SDiff(ctx, keys...))
r := newStringSliceCmd(c.Do(ctx, c.builder.SDiffCompleted(keys...)))
c.handler.after(ctx, r.Err())
return r
}
Expand All @@ -228,7 +228,7 @@ func (c *client) SDiffStore(ctx context.Context, destination string, keys ...str

func (c *client) SInter(ctx context.Context, keys ...string) StringSliceCmd {
ctx = c.handler.beforeWithKeys(ctx, CommandSInter, func() []string { return keys })
r := wrapStringSliceCmd(c.adapter.SInter(ctx, keys...))
r := newStringSliceCmd(c.Do(ctx, c.builder.SInterCompleted(keys...)))
c.handler.after(ctx, r.Err())
return r
}
Expand Down Expand Up @@ -273,12 +273,7 @@ func (c *client) SMIsMember(ctx context.Context, key string, members ...any) Boo

func (c *client) SMembers(ctx context.Context, key string) StringSliceCmd {
ctx = c.handler.before(ctx, CommandSMembers)
var r StringSliceCmd
if c.ttl > 0 {
r = newStringSliceCmd(c.Do(ctx, c.builder.SMembersCompleted(key)))
} else {
r = wrapStringSliceCmd(c.adapter.SMembers(ctx, key))
}
r := newStringSliceCmd(c.Do(ctx, c.builder.SMembersCompleted(key)))
c.handler.after(ctx, r.Err())
return r
}
Expand All @@ -292,28 +287,28 @@ func (c *client) SMove(ctx context.Context, source, destination string, member a

func (c *client) SPop(ctx context.Context, key string) StringCmd {
ctx = c.handler.before(ctx, CommandSPop)
r := wrapStringCmd(c.adapter.SPop(ctx, key))
r := newStringCmd(c.Do(ctx, c.builder.SPopCompleted(key)))
c.handler.after(ctx, r.Err())
return r
}

func (c *client) SPopN(ctx context.Context, key string, count int64) StringSliceCmd {
ctx = c.handler.before(ctx, CommandSPopN)
r := wrapStringSliceCmd(c.adapter.SPopN(ctx, key, count))
r := newStringSliceCmd(c.Do(ctx, c.builder.SPopNCompleted(key, count)))
c.handler.after(ctx, r.Err())
return r
}

func (c *client) SRandMember(ctx context.Context, key string) StringCmd {
ctx = c.handler.before(ctx, CommandSRandMember)
r := wrapStringCmd(c.adapter.SRandMember(ctx, key))
r := newStringCmd(c.Do(ctx, c.builder.SRandMemberCompleted(key)))
c.handler.after(ctx, r.Err())
return r
}

func (c *client) SRandMemberN(ctx context.Context, key string, count int64) StringSliceCmd {
ctx = c.handler.before(ctx, CommandSRandMemberN)
r := wrapStringSliceCmd(c.adapter.SRandMemberN(ctx, key, count))
r := newStringSliceCmd(c.Do(ctx, c.builder.SRandMemberNCompleted(key, count)))
c.handler.after(ctx, r.Err())
return r
}
Expand All @@ -338,7 +333,7 @@ func (c *client) SScan(ctx context.Context, key string, cursor uint64, match str

func (c *client) SUnion(ctx context.Context, keys ...string) StringSliceCmd {
ctx = c.handler.beforeWithKeys(ctx, CommandSUnion, func() []string { return keys })
r := wrapStringSliceCmd(c.adapter.SUnion(ctx, keys...))
r := newStringSliceCmd(c.Do(ctx, c.builder.SUnionCompleted(keys...)))
c.handler.after(ctx, r.Err())
return r
}
Expand Down
57 changes: 11 additions & 46 deletions cmd_sortedset.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ func (c *client) ZCount(ctx context.Context, key, min, max string) IntCmd {

func (c *client) ZDiff(ctx context.Context, keys ...string) StringSliceCmd {
ctx = c.handler.beforeWithKeys(ctx, CommandZDiff, func() []string { return keys })
r := wrapStringSliceCmd(c.adapter.ZDiff(ctx, keys...))
r := newStringSliceCmd(c.Do(ctx, c.builder.ZDiffCompleted(keys...)))
c.handler.after(ctx, r.Err())
return r
}
Expand Down Expand Up @@ -574,7 +574,7 @@ func (c *client) ZIncrBy(ctx context.Context, key string, increment float64, mem

func (c *client) ZInter(ctx context.Context, store ZStore) StringSliceCmd {
ctx = c.handler.beforeWithKeys(ctx, CommandZInter, func() []string { return store.Keys })
r := wrapStringSliceCmd(c.adapter.ZInter(ctx, store))
r := newStringSliceCmd(c.Do(ctx, c.builder.ZInterCompleted(store)))
c.handler.after(ctx, r.Err())
return r
}
Expand Down Expand Up @@ -647,7 +647,7 @@ func (c *client) ZPopMin(ctx context.Context, key string, count ...int64) ZSlice

func (c *client) ZRandMember(ctx context.Context, key string, count int64) StringSliceCmd {
ctx = c.handler.before(ctx, CommandZRandMember)
r := wrapStringSliceCmd(c.adapter.ZRandMember(ctx, key, count))
r := newStringSliceCmd(c.Do(ctx, c.builder.ZRandMemberCompleted(key, count)))
c.handler.after(ctx, r.Err())
return r
}
Expand All @@ -661,12 +661,7 @@ func (c *client) ZRandMemberWithScores(ctx context.Context, key string, count in

func (c *client) ZRange(ctx context.Context, key string, start, stop int64) StringSliceCmd {
ctx = c.handler.before(ctx, CommandZRange)
var r StringSliceCmd
if c.ttl > 0 {
r = newStringSliceCmd(c.Do(ctx, c.builder.ZRangeCompleted(key, start, stop)))
} else {
r = wrapStringSliceCmd(c.adapter.ZRange(ctx, key, start, stop))
}
r := newStringSliceCmd(c.Do(ctx, c.builder.ZRangeCompleted(key, start, stop)))
c.handler.after(ctx, r.Err())
return r
}
Expand All @@ -689,12 +684,7 @@ func (c *client) ZRangeArgs(ctx context.Context, z ZRangeArgs) StringSliceCmd {
} else {
ctx = c.handler.before(ctx, CommandZRangeArgs)
}
var r StringSliceCmd
if c.ttl > 0 {
r = newStringSliceCmd(c.Do(ctx, c.builder.ZRangeArgsCompleted(z)))
} else {
r = wrapStringSliceCmd(c.adapter.ZRangeArgs(ctx, z))
}
r := newStringSliceCmd(c.Do(ctx, c.builder.ZRangeArgsCompleted(z)))
c.handler.after(ctx, r.Err())
return r
}
Expand All @@ -717,24 +707,14 @@ func (c *client) ZRangeArgsWithScores(ctx context.Context, z ZRangeArgs) ZSliceC

func (c *client) ZRangeByLex(ctx context.Context, key string, opt ZRangeBy) StringSliceCmd {
ctx = c.handler.before(ctx, CommandZRangeByLex)
var r StringSliceCmd
if c.ttl > 0 {
r = newStringSliceCmd(c.Do(ctx, c.builder.ZRangeByLexCompleted(key, opt)))
} else {
r = wrapStringSliceCmd(c.adapter.ZRangeByLex(ctx, key, opt))
}
r := newStringSliceCmd(c.Do(ctx, c.builder.ZRangeByLexCompleted(key, opt)))
c.handler.after(ctx, r.Err())
return r
}

func (c *client) ZRangeByScore(ctx context.Context, key string, opt ZRangeBy) StringSliceCmd {
ctx = c.handler.before(ctx, CommandZRangeByScore)
var r StringSliceCmd
if c.ttl > 0 {
r = newStringSliceCmd(c.Do(ctx, c.builder.ZRangeByScoreCompleted(key, opt)))
} else {
r = wrapStringSliceCmd(c.adapter.ZRangeByScore(ctx, key, opt))
}
r := newStringSliceCmd(c.Do(ctx, c.builder.ZRangeByScoreCompleted(key, opt)))
c.handler.after(ctx, r.Err())
return r
}
Expand Down Expand Up @@ -816,12 +796,7 @@ func (c *client) ZRemRangeByScore(ctx context.Context, key, min, max string) Int

func (c *client) ZRevRange(ctx context.Context, key string, start, stop int64) StringSliceCmd {
ctx = c.handler.before(ctx, CommandZRevRange)
var r StringSliceCmd
if c.ttl > 0 {
r = newStringSliceCmd(c.Do(ctx, c.builder.ZRevRangeCompleted(key, start, stop)))
} else {
r = wrapStringSliceCmd(c.adapter.ZRevRange(ctx, key, start, stop))
}
r := newStringSliceCmd(c.Do(ctx, c.builder.ZRevRangeCompleted(key, start, stop)))
c.handler.after(ctx, r.Err())
return r
}
Expand All @@ -840,24 +815,14 @@ func (c *client) ZRevRangeWithScores(ctx context.Context, key string, start, sto

func (c *client) ZRevRangeByLex(ctx context.Context, key string, opt ZRangeBy) StringSliceCmd {
ctx = c.handler.before(ctx, CommandZRevRangeByLex)
var r StringSliceCmd
if c.ttl > 0 {
r = newStringSliceCmd(c.Do(ctx, c.builder.ZRevRangeByLexCompleted(key, opt)))
} else {
r = wrapStringSliceCmd(c.adapter.ZRevRangeByLex(ctx, key, opt))
}
r := newStringSliceCmd(c.Do(ctx, c.builder.ZRevRangeByLexCompleted(key, opt)))
c.handler.after(ctx, r.Err())
return r
}

func (c *client) ZRevRangeByScore(ctx context.Context, key string, opt ZRangeBy) StringSliceCmd {
ctx = c.handler.before(ctx, CommandZRevRangeByScore)
var r StringSliceCmd
if c.ttl > 0 {
r = newStringSliceCmd(c.Do(ctx, c.builder.ZRevRangeByScoreCompleted(key, opt)))
} else {
r = wrapStringSliceCmd(c.adapter.ZRevRangeByScore(ctx, key, opt))
}
r := newStringSliceCmd(c.Do(ctx, c.builder.ZRevRangeByScoreCompleted(key, opt)))
c.handler.after(ctx, r.Err())
return r
}
Expand Down Expand Up @@ -919,7 +884,7 @@ func (c *client) ZScore(ctx context.Context, key, member string) FloatCmd {

func (c *client) ZUnion(ctx context.Context, store ZStore) StringSliceCmd {
ctx = c.handler.beforeWithKeys(ctx, CommandZUnion, func() []string { return store.Keys })
r := wrapStringSliceCmd(c.adapter.ZUnion(ctx, store))
r := newStringSliceCmd(c.Do(ctx, c.builder.ZUnionCompleted(store)))
c.handler.after(ctx, r.Err())
return r
}
Expand Down
4 changes: 2 additions & 2 deletions cmd_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func (c *client) XAdd(ctx context.Context, a XAddArgs) StringCmd {
} else {
ctx = c.handler.before(ctx, CommandXAdd)
}
r := wrapStringCmd(c.adapter.XAdd(ctx, a))
r := newStringCmd(c.Do(ctx, c.builder.XAddCompleted(a)))
c.handler.after(ctx, r.Err())
return r
}
Expand Down Expand Up @@ -307,7 +307,7 @@ func (c *client) XClaim(ctx context.Context, a XClaimArgs) XMessageSliceCmd {

func (c *client) XClaimJustID(ctx context.Context, a XClaimArgs) StringSliceCmd {
ctx = c.handler.before(ctx, CommandXClaimJustID)
r := wrapStringSliceCmd(c.adapter.XClaimJustID(ctx, a))
r := newStringSliceCmd(c.Do(ctx, c.builder.XClaimJustIDCompleted(a)))
c.handler.after(ctx, r.Err())
return r
}
Expand Down
20 changes: 5 additions & 15 deletions cmd_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,45 +256,35 @@ func (c *client) DecrBy(ctx context.Context, key string, decrement int64) IntCmd

func (c *client) Get(ctx context.Context, key string) StringCmd {
ctx = c.handler.before(ctx, CommandGet)
var r StringCmd
if c.ttl > 0 {
r = newStringCmd(c.Do(ctx, c.builder.GetCompleted(key)))
} else {
r = wrapStringCmd(c.adapter.Get(ctx, key))
}
r := newStringCmd(c.Do(ctx, c.builder.GetCompleted(key)))
c.handler.after(ctx, r.Err())
return r
}

func (c *client) GetDel(ctx context.Context, key string) StringCmd {
ctx = c.handler.before(ctx, CommandGetDel)
r := wrapStringCmd(c.adapter.GetDel(ctx, key))
r := newStringCmd(c.Do(ctx, c.builder.GetDelCompleted(key)))
c.handler.after(ctx, r.Err())
return r
}

func (c *client) GetEx(ctx context.Context, key string, expiration time.Duration) StringCmd {
ctx = c.handler.before(ctx, CommandGetEx)
r := wrapStringCmd(c.adapter.GetEx(ctx, key, expiration))
r := newStringCmd(c.Do(ctx, c.builder.GetExCompleted(key, expiration)))
c.handler.after(ctx, r.Err())
return r
}

func (c *client) GetRange(ctx context.Context, key string, start, end int64) StringCmd {
ctx = c.handler.before(ctx, CommandGetRange)
var r StringCmd
if c.ttl > 0 {
r = newStringCmd(c.Do(ctx, c.builder.GetRangeCompleted(key, start, end)))
} else {
r = wrapStringCmd(c.adapter.GetRange(ctx, key, start, end))
}
r := newStringCmd(c.Do(ctx, c.builder.GetRangeCompleted(key, start, end)))
c.handler.after(ctx, r.Err())
return r
}

func (c *client) GetSet(ctx context.Context, key string, value any) StringCmd {
ctx = c.handler.before(ctx, CommandGetSet)
r := wrapStringCmd(c.adapter.GetSet(ctx, key, value))
r := newStringCmd(c.Do(ctx, c.builder.GetSetCompleted(key, value)))
c.handler.after(ctx, r.Err())
return r
}
Expand Down

0 comments on commit dd7f60e

Please sign in to comment.