Skip to content

Commit df0766c

Browse files
authored
sync and move commands to generic_commands
1 parent f8ed63b commit df0766c

File tree

1 file changed

+113
-16
lines changed

1 file changed

+113
-16
lines changed

generic_commands.go

Lines changed: 113 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@ type GenericCmdable interface {
1010
Dump(ctx context.Context, key string) *StringCmd
1111
Exists(ctx context.Context, keys ...string) *IntCmd
1212
Expire(ctx context.Context, key string, expiration time.Duration) *BoolCmd
13-
ExpireAt(ctx context.Context, key string, tm time.Time) *BoolCmd
14-
ExpireTime(ctx context.Context, key string) *DurationCmd
1513
ExpireNX(ctx context.Context, key string, expiration time.Duration) *BoolCmd
1614
ExpireXX(ctx context.Context, key string, expiration time.Duration) *BoolCmd
1715
ExpireGT(ctx context.Context, key string, expiration time.Duration) *BoolCmd
1816
ExpireLT(ctx context.Context, key string, expiration time.Duration) *BoolCmd
17+
ExpireAt(ctx context.Context, key string, tm time.Time) *BoolCmd
18+
ExpireAtNX(ctx context.Context, key string, tm time.Time) *BoolCmd
19+
ExpireAtXX(ctx context.Context, key string, tm time.Time) *BoolCmd
20+
ExpireAtGT(ctx context.Context, key string, tm time.Time) *BoolCmd
21+
ExpireAtLT(ctx context.Context, key string, tm time.Time) *BoolCmd
22+
ExpireTime(ctx context.Context, key string) *DurationCmd
1923
Keys(ctx context.Context, pattern string) *StringSliceCmd
2024
Migrate(ctx context.Context, host, port, key string, db int, timeout time.Duration) *StatusCmd
2125
Move(ctx context.Context, key string, db int) *BoolCmd
@@ -25,7 +29,15 @@ type GenericCmdable interface {
2529
ObjectIdleTime(ctx context.Context, key string) *DurationCmd
2630
Persist(ctx context.Context, key string) *BoolCmd
2731
PExpire(ctx context.Context, key string, expiration time.Duration) *BoolCmd
32+
PExpireNX(ctx context.Context, key string, expiration time.Duration) *BoolCmd
33+
PExpireXX(ctx context.Context, key string, expiration time.Duration) *BoolCmd
34+
PExpireGT(ctx context.Context, key string, expiration time.Duration) *BoolCmd
35+
PExpireLT(ctx context.Context, key string, expiration time.Duration) *BoolCmd
2836
PExpireAt(ctx context.Context, key string, tm time.Time) *BoolCmd
37+
PExpireAtNX(ctx context.Context, key string, tm time.Time) *BoolCmd
38+
PExpireAtXX(ctx context.Context, key string, tm time.Time) *BoolCmd
39+
PExpireAtGT(ctx context.Context, key string, tm time.Time) *BoolCmd
40+
PExpireAtLT(ctx context.Context, key string, tm time.Time) *BoolCmd
2941
PExpireTime(ctx context.Context, key string) *DurationCmd
3042
PTTL(ctx context.Context, key string) *DurationCmd
3143
RandomKey(ctx context.Context) *StringCmd
@@ -122,9 +134,39 @@ func (c cmdable) expire(
122134
}
123135

124136
func (c cmdable) ExpireAt(ctx context.Context, key string, tm time.Time) *BoolCmd {
125-
cmd := NewBoolCmd(ctx, "expireat", key, tm.Unix())
126-
_ = c(ctx, cmd)
127-
return cmd
137+
return c.expireAt(ctx, key, tm, "")
138+
}
139+
140+
func (c cmdable) ExpireAtNX(ctx context.Context, key string, tm time.Time) *BoolCmd {
141+
return c.expireAt(ctx, key, tm, "NX")
142+
}
143+
144+
func (c cmdable) ExpireAtXX(ctx context.Context, key string, tm time.Time) *BoolCmd {
145+
return c.expireAt(ctx, key, tm, "XX")
146+
}
147+
148+
func (c cmdable) ExpireAtGT(ctx context.Context, key string, tm time.Time) *BoolCmd {
149+
return c.expireAt(ctx, key, tm, "GT")
150+
}
151+
152+
func (c cmdable) ExpireAtLT(ctx context.Context, key string, tm time.Time) *BoolCmd {
153+
return c.expireAt(ctx, key, tm, "LT")
154+
}
155+
156+
func (c cmdable) expireAt(
157+
ctx context.Context, key string, tm time.Time, mode string,
158+
) *BoolCmd {
159+
args := make([]interface{}, 3, 4)
160+
args[0] = "expireat"
161+
args[1] = key
162+
args[2] = tm.Unix()
163+
if mode != "" {
164+
args = append(args, mode)
165+
}
166+
167+
cmd := NewBoolCmd(ctx, args...)
168+
_ = c(ctx, cmd)
169+
return cmd
128170
}
129171

130172
func (c cmdable) ExpireTime(ctx context.Context, key string) *DurationCmd {
@@ -191,20 +233,75 @@ func (c cmdable) Persist(ctx context.Context, key string) *BoolCmd {
191233
}
192234

193235
func (c cmdable) PExpire(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
194-
cmd := NewBoolCmd(ctx, "pexpire", key, formatMs(ctx, expiration))
195-
_ = c(ctx, cmd)
196-
return cmd
236+
return c.pexpire(ctx, key, expiration, "")
237+
}
238+
239+
func (c cmdable) PExpireNX(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
240+
return c.pexpire(ctx, key, expiration, "NX")
241+
}
242+
243+
func (c cmdable) PExpireXX(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
244+
return c.pexpire(ctx, key, expiration, "XX")
245+
}
246+
247+
func (c cmdable) PExpireGT(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
248+
return c.pexpire(ctx, key, expiration, "GT")
249+
}
250+
251+
func (c cmdable) PExpireLT(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
252+
return c.pexpire(ctx, key, expiration, "LT")
253+
}
254+
255+
func (c cmdable) pexpire(
256+
ctx context.Context, key string, expiration time.Duration, mode string,
257+
) *BoolCmd {
258+
args := make([]interface{}, 3, 4)
259+
args[0] = "pexpire"
260+
args[1] = key
261+
args[2] = formatMs(ctx, expiration)
262+
if mode != "" {
263+
args = append(args, mode)
264+
}
265+
266+
cmd := NewBoolCmd(ctx, args...)
267+
_ = c(ctx, cmd)
268+
return cmd
197269
}
198270

199271
func (c cmdable) PExpireAt(ctx context.Context, key string, tm time.Time) *BoolCmd {
200-
cmd := NewBoolCmd(
201-
ctx,
202-
"pexpireat",
203-
key,
204-
tm.UnixNano()/int64(time.Millisecond),
205-
)
206-
_ = c(ctx, cmd)
207-
return cmd
272+
return c.pexpireAt(ctx, key, tm, "")
273+
}
274+
275+
func (c cmdable) PExpireAtNX(ctx context.Context, key string, tm time.Time) *BoolCmd {
276+
return c.pexpireAt(ctx, key, tm, "NX")
277+
}
278+
279+
func (c cmdable) PExpireAtXX(ctx context.Context, key string, tm time.Time) *BoolCmd {
280+
return c.pexpireAt(ctx, key, tm, "XX")
281+
}
282+
283+
func (c cmdable) PExpireAtGT(ctx context.Context, key string, tm time.Time) *BoolCmd {
284+
return c.pexpireAt(ctx, key, tm, "GT")
285+
}
286+
287+
func (c cmdable) PExpireAtLT(ctx context.Context, key string, tm time.Time) *BoolCmd {
288+
return c.pexpireAt(ctx, key, tm, "LT")
289+
}
290+
291+
func (c cmdable) pexpireAt(
292+
ctx context.Context, key string, tm time.Time, mode string,
293+
) *BoolCmd {
294+
args := make([]interface{}, 3, 4)
295+
args[0] = "pexpireat"
296+
args[1] = key
297+
args[2] = tm.UnixNano() / int64(time.Millisecond)
298+
if mode != "" {
299+
args = append(args, mode)
300+
}
301+
302+
cmd := NewBoolCmd(ctx, args...)
303+
_ = c(ctx, cmd)
304+
return cmd
208305
}
209306

210307
func (c cmdable) PExpireTime(ctx context.Context, key string) *DurationCmd {

0 commit comments

Comments
 (0)