Skip to content

Commit ef62dd4

Browse files
committed
Add HGETDEL, HGETEX, HSETEX commands
1 parent 8269e6a commit ef62dd4

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

hash_commands.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@ type HashCmdable interface {
1010
HExists(ctx context.Context, key, field string) *BoolCmd
1111
HGet(ctx context.Context, key, field string) *StringCmd
1212
HGetAll(ctx context.Context, key string) *MapStringStringCmd
13+
HGetDel(ctx context.Context, key string, fields ...string) *IntSliceCmd
14+
HGetEX(ctx context.Context, key string, fields ...string) *IntSliceCmd
15+
HGetEXWithArgs(ctx context.Context, key string, expirationType HGetEXExpirationType, expirationVal int64, fields ...string) *IntSliceCmd
1316
HIncrBy(ctx context.Context, key, field string, incr int64) *IntCmd
1417
HIncrByFloat(ctx context.Context, key, field string, incr float64) *FloatCmd
1518
HKeys(ctx context.Context, key string) *StringSliceCmd
1619
HLen(ctx context.Context, key string) *IntCmd
1720
HMGet(ctx context.Context, key string, fields ...string) *SliceCmd
1821
HSet(ctx context.Context, key string, values ...interface{}) *IntCmd
1922
HMSet(ctx context.Context, key string, values ...interface{}) *BoolCmd
23+
HSetEX(ctx context.Context, key string, fieldsAndValues ...string) *IntCmd
24+
HSetEXWithArgs(ctx context.Context, key string, options HSetXOptions, fieldsAndValues ...string) *IntCmd
2025
HSetNX(ctx context.Context, key, field string, value interface{}) *BoolCmd
2126
HScan(ctx context.Context, key string, cursor uint64, match string, count int64) *ScanCmd
2227
HScanNoValues(ctx context.Context, key string, cursor uint64, match string, count int64) *ScanCmd
@@ -454,3 +459,111 @@ func (c cmdable) HPTTL(ctx context.Context, key string, fields ...string) *IntSl
454459
_ = c(ctx, cmd)
455460
return cmd
456461
}
462+
463+
// TODO check return type
464+
func (c cmdable) HGetDel(ctx context.Context, key string, fields ...string) *IntSliceCmd {
465+
args := []interface{}{"HGETDEL", key, "FIELDS", len(fields)}
466+
for _, field := range fields {
467+
args = append(args, field)
468+
}
469+
cmd := NewIntSliceCmd(ctx, args...)
470+
_ = c(ctx, cmd)
471+
return cmd
472+
}
473+
474+
func (c cmdable) HGetEX(ctx context.Context, key string, fields ...string) *IntSliceCmd {
475+
args := []interface{}{"HGETEX", key, "FIELDS", len(fields)}
476+
for _, field := range fields {
477+
args = append(args, field)
478+
}
479+
cmd := NewIntSliceCmd(ctx, args...)
480+
_ = c(ctx, cmd)
481+
return cmd
482+
}
483+
484+
// ExpirationType represents an expiration option for the hash commands.
485+
type HGetEXExpirationType string
486+
487+
const (
488+
HGetEXExpirationEX HGetEXExpirationType = "EX"
489+
HGetEXExpirationPX HGetEXExpirationType = "PX"
490+
HGetEXExpirationEXAT HGetEXExpirationType = "EXAT"
491+
HGetEXExpirationPXAT HGetEXExpirationType = "PXAT"
492+
HGetEXExpirationPERSIST HGetEXExpirationType = "PERSIST"
493+
)
494+
495+
func (c cmdable) HGetEXWithArgs(ctx context.Context, key string, expirationType HGetEXExpirationType, expirationVal int64, fields ...string) *IntSliceCmd {
496+
args := []interface{}{"HGETEX", key}
497+
498+
// Append expiration option and its value if necessary.
499+
args = append(args, string(expirationType))
500+
if expirationType != HGetEXExpirationPERSIST {
501+
args = append(args, expirationVal)
502+
}
503+
504+
args = append(args, "FIELDS", len(fields))
505+
for _, field := range fields {
506+
args = append(args, field)
507+
}
508+
509+
cmd := NewIntSliceCmd(ctx, args...)
510+
_ = c(ctx, cmd)
511+
return cmd
512+
}
513+
514+
type HSetEXCondition string
515+
516+
const (
517+
HSetEXFNX HSetEXCondition = "FNX" // Only set the fields if none of them already exist.
518+
HSetEXFXX HSetEXCondition = "FXX" // Only set the fields if all already exist.
519+
)
520+
521+
type HSetEXExpirationType string
522+
523+
const (
524+
HSetEXExpirationEX HSetEXExpirationType = "EX"
525+
HSetEXExpirationPX HSetEXExpirationType = "PX"
526+
HSetEXExpirationEXAT HSetEXExpirationType = "EXAT"
527+
HSetEXExpirationPXAT HSetEXExpirationType = "PXAT"
528+
HSetEXExpirationKEEPTTL HSetEXExpirationType = "KEEPTTL"
529+
)
530+
531+
type HSetXOptions struct {
532+
Condition HSetEXCondition
533+
ExpirationType HSetEXExpirationType
534+
ExpirationVal int64
535+
}
536+
537+
func (c cmdable) HSetEX(ctx context.Context, key string, fieldsAndValues ...string) *IntCmd {
538+
args := []interface{}{"HSETEX", key, "FIELDS", len(fieldsAndValues)}
539+
for _, field := range fieldsAndValues {
540+
args = append(args, field)
541+
}
542+
543+
cmd := NewIntCmd(ctx, args...)
544+
_ = c(ctx, cmd)
545+
return cmd
546+
}
547+
548+
func (c cmdable) HSetEXWithArgs(ctx context.Context, key string, options HSetXOptions, fieldsAndValues ...string) *IntCmd {
549+
// Start with the command name and key.
550+
args := []interface{}{"HSETEX", key}
551+
if options.Condition != "" {
552+
args = append(args, string(options.Condition))
553+
}
554+
if options.ExpirationType != "" {
555+
args = append(args, string(options.ExpirationType))
556+
if options.ExpirationType != HSetEXExpirationKEEPTTL {
557+
args = append(args, options.ExpirationVal)
558+
}
559+
}
560+
args = append(args, "FIELDS", len(fieldsAndValues))
561+
for _, field := range fieldsAndValues {
562+
args = append(args, field)
563+
}
564+
565+
// Create and execute the command.
566+
cmd := NewIntCmd(ctx, args...)
567+
_ = c(ctx, cmd)
568+
return cmd
569+
}

0 commit comments

Comments
 (0)