Skip to content

Commit

Permalink
feat: add HExpire, HPExpire, HTTL, and HPTTL to rueidiscompat (#618)
Browse files Browse the repository at this point in the history
Signed-off-by: Rueian <[email protected]>
  • Loading branch information
rueian authored Aug 27, 2024
1 parent 372da2f commit 1b6c355
Show file tree
Hide file tree
Showing 6 changed files with 460 additions and 30 deletions.
12 changes: 6 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ version: "3.8"

services:
redis:
image: redis:7.2-rc-alpine
image: redis:7.4-alpine
ports:
- "6379:6379"
redislock:
image: redis:7.2-rc-alpine
image: redis:7.4-alpine
ports:
- "6376:6379"
redis5:
Expand All @@ -30,15 +30,15 @@ services:
ports:
- "6377:6379"
compat:
image: redis/redis-stack:7.2.0-v3
image: redis/redis-stack:7.4.0-v0
ports:
- "6378:6379"
compat5:
image: redis:5-alpine
ports:
- "6356:6379"
sentinel:
image: redis:7.2-rc-alpine
image: redis:7.4-alpine
entrypoint:
- /bin/sh
- -c
Expand All @@ -62,7 +62,7 @@ services:
- "6385:6385"
- "26355:26379"
cluster:
image: redis:7.2-rc-alpine
image: redis:7.4-alpine
entrypoint:
- /bin/sh
- -c
Expand Down Expand Up @@ -107,7 +107,7 @@ services:
- "7008:7008"
- "7009:7009"
clusteradapter:
image: redis:7.2-rc-alpine
image: redis:7.4-alpine
entrypoint:
- /bin/sh
- -c
Expand Down
148 changes: 135 additions & 13 deletions rueidiscompat/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,19 +162,19 @@ type CoreCmdable interface {
HVals(ctx context.Context, key string) *StringSliceCmd
HRandField(ctx context.Context, key string, count int64) *StringSliceCmd
HRandFieldWithValues(ctx context.Context, key string, count int64) *KeyValueSliceCmd
// TODO HExpire(ctx context.Context, key string, expiration time.Duration, fields ...string) *IntSliceCmd
// TODO HExpireWithArgs(ctx context.Context, key string, expiration time.Duration, expirationArgs HExpireArgs, fields ...string) *IntSliceCmd
// TODO HPExpire(ctx context.Context, key string, expiration time.Duration, fields ...string) *IntSliceCmd
// TODO HPExpireWithArgs(ctx context.Context, key string, expiration time.Duration, expirationArgs HExpireArgs, fields ...string) *IntSliceCmd
// TODO HExpireAt(ctx context.Context, key string, tm time.Time, fields ...string) *IntSliceCmd
// TODO HExpireAtWithArgs(ctx context.Context, key string, tm time.Time, expirationArgs HExpireArgs, fields ...string) *IntSliceCmd
// TODO HPExpireAt(ctx context.Context, key string, tm time.Time, fields ...string) *IntSliceCmd
// TODO HPExpireAtWithArgs(ctx context.Context, key string, tm time.Time, expirationArgs HExpireArgs, fields ...string) *IntSliceCmd
// TODO HPersist(ctx context.Context, key string, fields ...string) *IntSliceCmd
// TODO HExpireTime(ctx context.Context, key string, fields ...string) *IntSliceCmd
// TODO HPExpireTime(ctx context.Context, key string, fields ...string) *IntSliceCmd
// TODO HTTL(ctx context.Context, key string, fields ...string) *IntSliceCmd
// TODO HPTTL(ctx context.Context, key string, fields ...string) *IntSliceCmd
HExpire(ctx context.Context, key string, expiration time.Duration, fields ...string) *IntSliceCmd
HExpireWithArgs(ctx context.Context, key string, expiration time.Duration, expirationArgs HExpireArgs, fields ...string) *IntSliceCmd
HPExpire(ctx context.Context, key string, expiration time.Duration, fields ...string) *IntSliceCmd
HPExpireWithArgs(ctx context.Context, key string, expiration time.Duration, expirationArgs HExpireArgs, fields ...string) *IntSliceCmd
HExpireAt(ctx context.Context, key string, tm time.Time, fields ...string) *IntSliceCmd
HExpireAtWithArgs(ctx context.Context, key string, tm time.Time, expirationArgs HExpireArgs, fields ...string) *IntSliceCmd
HPExpireAt(ctx context.Context, key string, tm time.Time, fields ...string) *IntSliceCmd
HPExpireAtWithArgs(ctx context.Context, key string, tm time.Time, expirationArgs HExpireArgs, fields ...string) *IntSliceCmd
HPersist(ctx context.Context, key string, fields ...string) *IntSliceCmd
HExpireTime(ctx context.Context, key string, fields ...string) *IntSliceCmd
HPExpireTime(ctx context.Context, key string, fields ...string) *IntSliceCmd
HTTL(ctx context.Context, key string, fields ...string) *IntSliceCmd
HPTTL(ctx context.Context, key string, fields ...string) *IntSliceCmd

BLPop(ctx context.Context, timeout time.Duration, keys ...string) *StringSliceCmd
BLMPop(ctx context.Context, timeout time.Duration, direction string, count int64, keys ...string) *KeyValuesCmd
Expand Down Expand Up @@ -1408,6 +1408,128 @@ func (c *Compat) HRandFieldWithValues(ctx context.Context, key string, count int
return newKeyValueSliceCmd(c.client.Do(ctx, c.client.B().Hrandfield().Key(key).Count(count).Withvalues().Build()))
}

func (c *Compat) HExpire(ctx context.Context, key string, expiration time.Duration, fields ...string) *IntSliceCmd {
cmd := c.client.B().Hexpire().Key(key).Seconds(formatSec(expiration)).Fields().Numfields(int64(len(fields))).Field(fields...).Build()
resp := c.client.Do(ctx, cmd)
return newIntSliceCmd(resp)
}

func (c *Compat) HExpireWithArgs(ctx context.Context, key string, expiration time.Duration, expirationArgs HExpireArgs, fields ...string) *IntSliceCmd {
var cmd rueidis.Completed
if expirationArgs.NX {
cmd = c.client.B().Hexpire().Key(key).Seconds(formatSec(expiration)).Nx().Fields().Numfields(int64(len(fields))).Field(fields...).Build()
} else if expirationArgs.XX {
cmd = c.client.B().Hexpire().Key(key).Seconds(formatSec(expiration)).Xx().Fields().Numfields(int64(len(fields))).Field(fields...).Build()
} else if expirationArgs.GT {
cmd = c.client.B().Hexpire().Key(key).Seconds(formatSec(expiration)).Gt().Fields().Numfields(int64(len(fields))).Field(fields...).Build()
} else if expirationArgs.LT {
cmd = c.client.B().Hexpire().Key(key).Seconds(formatSec(expiration)).Lt().Fields().Numfields(int64(len(fields))).Field(fields...).Build()
} else {
cmd = c.client.B().Hexpire().Key(key).Seconds(formatSec(expiration)).Fields().Numfields(int64(len(fields))).Field(fields...).Build()
}
resp := c.client.Do(ctx, cmd)
return newIntSliceCmd(resp)
}

func (c *Compat) HPExpire(ctx context.Context, key string, expiration time.Duration, fields ...string) *IntSliceCmd {
cmd := c.client.B().Hpexpire().Key(key).Milliseconds(formatMs(expiration)).Fields().Numfields(int64(len(fields))).Field(fields...).Build()
resp := c.client.Do(ctx, cmd)
return newIntSliceCmd(resp)
}

func (c *Compat) HPExpireWithArgs(ctx context.Context, key string, expiration time.Duration, expirationArgs HExpireArgs, fields ...string) *IntSliceCmd {
var cmd rueidis.Completed
if expirationArgs.NX {
cmd = c.client.B().Hpexpire().Key(key).Milliseconds(formatMs(expiration)).Nx().Fields().Numfields(int64(len(fields))).Field(fields...).Build()
} else if expirationArgs.XX {
cmd = c.client.B().Hpexpire().Key(key).Milliseconds(formatMs(expiration)).Xx().Fields().Numfields(int64(len(fields))).Field(fields...).Build()
} else if expirationArgs.GT {
cmd = c.client.B().Hpexpire().Key(key).Milliseconds(formatMs(expiration)).Gt().Fields().Numfields(int64(len(fields))).Field(fields...).Build()
} else if expirationArgs.LT {
cmd = c.client.B().Hpexpire().Key(key).Milliseconds(formatMs(expiration)).Lt().Fields().Numfields(int64(len(fields))).Field(fields...).Build()
} else {
cmd = c.client.B().Hpexpire().Key(key).Milliseconds(formatMs(expiration)).Fields().Numfields(int64(len(fields))).Field(fields...).Build()
}
resp := c.client.Do(ctx, cmd)
return newIntSliceCmd(resp)
}

func (c *Compat) HExpireAt(ctx context.Context, key string, tm time.Time, fields ...string) *IntSliceCmd {
cmd := c.client.B().Hexpireat().Key(key).UnixTimeSeconds(tm.Unix()).Fields().Numfields(int64(len(fields))).Field(fields...).Build()
resp := c.client.Do(ctx, cmd)
return newIntSliceCmd(resp)
}

func (c *Compat) HExpireAtWithArgs(ctx context.Context, key string, tm time.Time, expirationArgs HExpireArgs, fields ...string) *IntSliceCmd {
var cmd rueidis.Completed
if expirationArgs.NX {
cmd = c.client.B().Hexpireat().Key(key).UnixTimeSeconds(tm.Unix()).Nx().Fields().Numfields(int64(len(fields))).Field(fields...).Build()
} else if expirationArgs.XX {
cmd = c.client.B().Hexpireat().Key(key).UnixTimeSeconds(tm.Unix()).Xx().Fields().Numfields(int64(len(fields))).Field(fields...).Build()
} else if expirationArgs.GT {
cmd = c.client.B().Hexpireat().Key(key).UnixTimeSeconds(tm.Unix()).Gt().Fields().Numfields(int64(len(fields))).Field(fields...).Build()
} else if expirationArgs.LT {
cmd = c.client.B().Hexpireat().Key(key).UnixTimeSeconds(tm.Unix()).Lt().Fields().Numfields(int64(len(fields))).Field(fields...).Build()
} else {
cmd = c.client.B().Hexpireat().Key(key).UnixTimeSeconds(tm.Unix()).Fields().Numfields(int64(len(fields))).Field(fields...).Build()
}
resp := c.client.Do(ctx, cmd)
return newIntSliceCmd(resp)
}

func (c *Compat) HPExpireAt(ctx context.Context, key string, tm time.Time, fields ...string) *IntSliceCmd {
cmd := c.client.B().Hpexpireat().Key(key).UnixTimeMilliseconds(tm.UnixMilli()).Fields().Numfields(int64(len(fields))).Field(fields...).Build()
resp := c.client.Do(ctx, cmd)
return newIntSliceCmd(resp)
}

func (c *Compat) HPExpireAtWithArgs(ctx context.Context, key string, tm time.Time, expirationArgs HExpireArgs, fields ...string) *IntSliceCmd {
var cmd rueidis.Completed
if expirationArgs.NX {
cmd = c.client.B().Hpexpireat().Key(key).UnixTimeMilliseconds(tm.UnixMilli()).Nx().Fields().Numfields(int64(len(fields))).Field(fields...).Build()
} else if expirationArgs.XX {
cmd = c.client.B().Hpexpireat().Key(key).UnixTimeMilliseconds(tm.UnixMilli()).Xx().Fields().Numfields(int64(len(fields))).Field(fields...).Build()
} else if expirationArgs.GT {
cmd = c.client.B().Hpexpireat().Key(key).UnixTimeMilliseconds(tm.UnixMilli()).Gt().Fields().Numfields(int64(len(fields))).Field(fields...).Build()
} else if expirationArgs.LT {
cmd = c.client.B().Hpexpireat().Key(key).UnixTimeMilliseconds(tm.UnixMilli()).Lt().Fields().Numfields(int64(len(fields))).Field(fields...).Build()
} else {
cmd = c.client.B().Hpexpireat().Key(key).UnixTimeMilliseconds(tm.UnixMilli()).Fields().Numfields(int64(len(fields))).Field(fields...).Build()
}
resp := c.client.Do(ctx, cmd)
return newIntSliceCmd(resp)
}

func (c *Compat) HPersist(ctx context.Context, key string, fields ...string) *IntSliceCmd {
cmd := c.client.B().Hpersist().Key(key).Fields().Numfields(int64(len(fields))).Field(fields...).Build()
resp := c.client.Do(ctx, cmd)
return newIntSliceCmd(resp)
}

func (c *Compat) HExpireTime(ctx context.Context, key string, fields ...string) *IntSliceCmd {
cmd := c.client.B().Hexpiretime().Key(key).Fields().Numfields(int64(len(fields))).Field(fields...).Build()
resp := c.client.Do(ctx, cmd)
return newIntSliceCmd(resp)
}

func (c *Compat) HPExpireTime(ctx context.Context, key string, fields ...string) *IntSliceCmd {
cmd := c.client.B().Hpexpiretime().Key(key).Fields().Numfields(int64(len(fields))).Field(fields...).Build()
resp := c.client.Do(ctx, cmd)
return newIntSliceCmd(resp)
}

func (c *Compat) HTTL(ctx context.Context, key string, fields ...string) *IntSliceCmd {
cmd := c.client.B().Httl().Key(key).Fields().Numfields(int64(len(fields))).Field(fields...).Build()
resp := c.client.Do(ctx, cmd)
return newIntSliceCmd(resp)
}

func (c *Compat) HPTTL(ctx context.Context, key string, fields ...string) *IntSliceCmd {
cmd := c.client.B().Hpttl().Key(key).Fields().Numfields(int64(len(fields))).Field(fields...).Build()
resp := c.client.Do(ctx, cmd)
return newIntSliceCmd(resp)
}

func (c *Compat) BLPop(ctx context.Context, timeout time.Duration, keys ...string) *StringSliceCmd {
cmd := c.client.B().Blpop().Key(keys...).Timeout(float64(formatSec(timeout))).Build()
resp := c.client.Do(ctx, cmd)
Expand Down
Loading

0 comments on commit 1b6c355

Please sign in to comment.