Skip to content

Commit

Permalink
Implemented Exists, Expire and TTL commands
Browse files Browse the repository at this point in the history
Signed-off-by: Niharika Bhavaraju <nbhavaraju@google.com>
  • Loading branch information
niharikabhavaraju committed Nov 22, 2024
1 parent fb8e033 commit 022c565
Show file tree
Hide file tree
Showing 4 changed files with 1,121 additions and 0 deletions.
138 changes: 138 additions & 0 deletions go/api/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -948,3 +948,141 @@ func (client *baseClient) Del(keys []string) (Result[int64], error) {

return handleLongResponse(result)
}

func (client *baseClient) Exists(keys []string) (Result[int64], error) {
result, err := client.executeCommand(C.Exists, keys)
if err != nil {
return CreateNilInt64Result(), err
}

return handleLongResponse(result)
}

func (client *baseClient) Expire(key string, seconds int64) (Result[bool], error) {
secondsStr := strconv.FormatInt(seconds, 10)
result, err := client.executeCommand(C.Expire, []string{key, secondsStr})
if err != nil {
return CreateNilBoolResult(), err
}

return handleBooleanResponse(result)
}

func (client *baseClient) ExpireWithOptions(key string, seconds int64, options *ExpireOptions) (Result[bool], error) {
secondsStr := strconv.FormatInt(seconds, 10)
optionArgs, err := options.toArgs()
if err != nil {
return CreateNilBoolResult(), err
}
result, err := client.executeCommand(C.Expire, append([]string{key, secondsStr}, optionArgs...))
if err != nil {
return CreateNilBoolResult(), err
}
return handleBooleanResponse(result)
}

func (client *baseClient) ExpireAt(key string, unixTimestampInSeconds int64) (Result[bool], error) {
timestampStr := strconv.FormatInt(unixTimestampInSeconds, 10)
result, err := client.executeCommand(C.ExpireAt, []string{key, timestampStr})
if err != nil {
return CreateNilBoolResult(), err
}

return handleBooleanResponse(result)
}

func (client *baseClient) ExpireAtWithOptions(key string, unixTimestampInSeconds int64, options *ExpireOptions) (Result[bool], error) {
timestampStr := strconv.FormatInt(unixTimestampInSeconds, 10)
optionArgs, err := options.toArgs()
if err != nil {
return CreateNilBoolResult(), err
}
result, err := client.executeCommand(C.ExpireAt, append([]string{key, timestampStr}, optionArgs...))
if err != nil {
return CreateNilBoolResult(), err
}
return handleBooleanResponse(result)
}

func (client *baseClient) PExpire(key string, milliseconds int64) (Result[bool], error) {

milliSecondsStr := strconv.FormatInt(milliseconds, 10)
result, err := client.executeCommand(C.PExpire, []string{key, milliSecondsStr})
if err != nil {
return CreateNilBoolResult(), err
}
return handleBooleanResponse(result)
}

func (client *baseClient) PExpireWithOptions(key string, milliseconds int64, options *ExpireOptions) (Result[bool], error) {

milliSecondsStr := strconv.FormatInt(milliseconds, 10)
optionArgs, err := options.toArgs()
if err != nil {
return CreateNilBoolResult(), err
}
result, err := client.executeCommand(C.PExpire, append([]string{key, milliSecondsStr}, optionArgs...))
if err != nil {
return CreateNilBoolResult(), err
}
return handleBooleanResponse(result)
}

func (client *baseClient) PExpireAt(key string, unixTimestampInMilliSeconds int64) (Result[bool], error) {

timestampStr := strconv.FormatInt(unixTimestampInMilliSeconds, 10)
result, err := client.executeCommand(C.PExpireAt, []string{key, timestampStr})
if err != nil {
return CreateNilBoolResult(), err
}
return handleBooleanResponse(result)
}

func (client *baseClient) PExpireAtWithOptions(key string, unixTimestampInMilliSeconds int64, options *ExpireOptions) (Result[bool], error) {
timestampStr := strconv.FormatInt(unixTimestampInMilliSeconds, 10)
optionArgs, err := options.toArgs()
if err != nil {
return CreateNilBoolResult(), err
}
result, err := client.executeCommand(C.PExpireAt, append([]string{key, timestampStr}, optionArgs...))
if err != nil {
return CreateNilBoolResult(), err
}
return handleBooleanResponse(result)
}

func (client *baseClient) ExpireTime(key string) (Result[int64], error) {
result, err := client.executeCommand(C.ExpireTime, []string{key})
if err != nil {
return CreateNilInt64Result(), err
}

return handleLongResponse(result)
}

func (client *baseClient) PExpireTime(key string) (Result[int64], error) {
result, err := client.executeCommand(C.PExpireTime, []string{key})
if err != nil {
return CreateNilInt64Result(), err
}

return handleLongResponse(result)
}

func (client *baseClient) TTL(key string) (Result[int64], error) {
result, err := client.executeCommand(C.TTL, []string{key})
if err != nil {
return CreateNilInt64Result(), err
}

return handleLongResponse(result)
}

func (client *baseClient) PTTL(key string) (Result[int64], error) {
result, err := client.executeCommand(C.PTTL, []string{key})
if err != nil {
return CreateNilInt64Result(), err
}

return handleLongResponse(result)
}
35 changes: 35 additions & 0 deletions go/api/command_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,28 @@ func (opts *SetOptions) toArgs() ([]string, error) {
return args, err
}

type ExpireOptions struct {
ExpireConditionalSet ExpireConditionalSet
}

func NewExpireOptionsBuilder() *ExpireOptions {
return &ExpireOptions{}
}

func (expireOptions *ExpireOptions) SetExpireConditionalSet(expireConditionalSet ExpireConditionalSet) *ExpireOptions {
expireOptions.ExpireConditionalSet = expireConditionalSet
return expireOptions
}

func (opts *ExpireOptions) toArgs() ([]string, error) {
args := []string{}
var err error
if opts.ExpireConditionalSet != "" {
args = append(args, string(opts.ExpireConditionalSet))
}
return args, err
}

// GetExOptions represents optional arguments for the [api.StringCommands.GetExWithOptions] command.
//
// See [valkey.io]
Expand Down Expand Up @@ -120,6 +142,19 @@ const (
OnlyIfDoesNotExist ConditionalSet = "NX"
)

type ExpireConditionalSet string

const (
// HasExistingExpiry only sets the key if it already exists. Equivalent to "XX" in the valkey API.
HasExistingExpiry ExpireConditionalSet = "XX"
// HasNoExpiry only sets the key if it does not already exist. Equivalent to "NX" in the valkey API.
HasNoExpiry ExpireConditionalSet = "NX"
// NewExpiryGreaterThanCurrent only sets the key if its greater than current. Equivalent to "GT" in the valkey API.
NewExpiryGreaterThanCurrent ExpireConditionalSet = "GT"
// NewExpiryLessThanCurrent only sets the key if its lesser than current. Equivalent to "LT" in the valkey API.
NewExpiryLessThanCurrent ExpireConditionalSet = "LT"
)

// Expiry is used to configure the lifetime of a value.
type Expiry struct {
Type ExpiryType
Expand Down
Loading

0 comments on commit 022c565

Please sign in to comment.