Skip to content

Commit

Permalink
feat: add UpdateTtl (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
poppoerika authored Mar 9, 2023
1 parent 0421d51 commit f7fa565
Show file tree
Hide file tree
Showing 13 changed files with 529 additions and 4 deletions.
2 changes: 1 addition & 1 deletion internal/protos/cacheclient.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/protos/cacheping.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/protos/cachepubsub.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/protos/controlclient.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions momento/cache_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ type CacheClient interface {
DictionaryRemoveField(ctx context.Context, r *DictionaryRemoveFieldRequest) (responses.DictionaryRemoveFieldResponse, error)
DictionaryRemoveFields(ctx context.Context, r *DictionaryRemoveFieldsRequest) (responses.DictionaryRemoveFieldsResponse, error)

UpdateTtl(ctx context.Context, r *UpdateTtlRequest) (responses.UpdateTtlResponse, error)
IncreaseTtl(ctx context.Context, r *IncreaseTtlRequest) (responses.IncreaseTtlResponse, error)
DecreaseTtl(ctx context.Context, r *DecreaseTtlRequest) (responses.DecreaseTtlResponse, error)

Ping(ctx context.Context) (responses.PingResponse, error)

Close()
Expand Down Expand Up @@ -465,6 +469,27 @@ func (c defaultScsClient) DictionaryRemoveFields(ctx context.Context, r *Diction
return r.response, nil
}

func (c defaultScsClient) UpdateTtl(ctx context.Context, r *UpdateTtlRequest) (responses.UpdateTtlResponse, error) {
if err := c.dataClient.makeRequest(ctx, r); err != nil {
return nil, err
}
return r.response, nil
}

func (c defaultScsClient) IncreaseTtl(ctx context.Context, r *IncreaseTtlRequest) (responses.IncreaseTtlResponse, error) {
if err := c.dataClient.makeRequest(ctx, r); err != nil {
return nil, err
}
return r.response, nil
}

func (c defaultScsClient) DecreaseTtl(ctx context.Context, r *DecreaseTtlRequest) (responses.DecreaseTtlResponse, error) {
if err := c.dataClient.makeRequest(ctx, r); err != nil {
return nil, err
}
return r.response, nil
}

func (c defaultScsClient) Ping(ctx context.Context) (responses.PingResponse, error) {
if err := c.pingClient.Ping(ctx); err != nil {
return nil, err
Expand Down
78 changes: 78 additions & 0 deletions momento/decrease_ttl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package momento

import (
"context"
"time"

pb "github.com/momentohq/client-sdk-go/internal/protos"
"github.com/momentohq/client-sdk-go/responses"
)

type DecreaseTtlRequest struct {
// Name of the cache to get the item from to be deleted
CacheName string
// string or byte key to be used to store item.
Key Key
// Time to live that you want to decrease to.
Ttl time.Duration

grpcRequest *pb.XUpdateTtlRequest
grpcResponse *pb.XUpdateTtlResponse
response responses.DecreaseTtlResponse
}

func (r *DecreaseTtlRequest) cacheName() string { return r.CacheName }

func (r *DecreaseTtlRequest) key() Key { return r.Key }

func (r *DecreaseTtlRequest) updateTtl() time.Duration { return r.Ttl }

func (r *DecreaseTtlRequest) requestName() string { return "DecreaseTtl" }

func (r *DecreaseTtlRequest) initGrpcRequest(client scsDataClient) error {
var err error
var ttl uint64

var key []byte
if key, err = prepareKey(r); err != nil {
return err
}

if ttl, err = prepareUpdateTtl(r); err != nil {
return err
}
r.grpcRequest = &pb.XUpdateTtlRequest{CacheKey: key, UpdateTtl: &pb.XUpdateTtlRequest_DecreaseToMilliseconds{DecreaseToMilliseconds: ttl}}

return nil
}

func (r *DecreaseTtlRequest) makeGrpcRequest(metadata context.Context, client scsDataClient) (grpcResponse, error) {
resp, err := client.grpcClient.UpdateTtl(metadata, r.grpcRequest)
if err != nil {
return nil, err
}

r.grpcResponse = resp

return resp, nil
}

func (r *DecreaseTtlRequest) interpretGrpcResponse() error {
grpcResp := r.grpcResponse

var resp responses.DecreaseTtlResponse
switch grpcResp.Result.(type) {
case *pb.XUpdateTtlResponse_NotSet:
resp = &responses.DecreaseTtlNotSet{}
case *pb.XUpdateTtlResponse_Missing:
resp = &responses.DecreaseTtlMiss{}
case *pb.XUpdateTtlResponse_Set:
resp = &responses.DecreaseTtlSet{}
default:
return errUnexpectedGrpcResponse(r, r.grpcResponse)
}

r.response = resp

return nil
}
78 changes: 78 additions & 0 deletions momento/increase_ttl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package momento

import (
"context"
"time"

pb "github.com/momentohq/client-sdk-go/internal/protos"
"github.com/momentohq/client-sdk-go/responses"
)

type IncreaseTtlRequest struct {
// Name of the cache to get the item from to be deleted
CacheName string
// string or byte key to be used to store item.
Key Key
// Time to live that you want to increase to.
Ttl time.Duration

grpcRequest *pb.XUpdateTtlRequest
grpcResponse *pb.XUpdateTtlResponse
response responses.IncreaseTtlResponse
}

func (r *IncreaseTtlRequest) cacheName() string { return r.CacheName }

func (r *IncreaseTtlRequest) key() Key { return r.Key }

func (r *IncreaseTtlRequest) updateTtl() time.Duration { return r.Ttl }

func (r *IncreaseTtlRequest) requestName() string { return "IncreaseTtl" }

func (r *IncreaseTtlRequest) initGrpcRequest(client scsDataClient) error {
var err error
var ttl uint64

var key []byte
if key, err = prepareKey(r); err != nil {
return err
}

if ttl, err = prepareUpdateTtl(r); err != nil {
return err
}
r.grpcRequest = &pb.XUpdateTtlRequest{CacheKey: key, UpdateTtl: &pb.XUpdateTtlRequest_IncreaseToMilliseconds{IncreaseToMilliseconds: ttl}}

return nil
}

func (r *IncreaseTtlRequest) makeGrpcRequest(metadata context.Context, client scsDataClient) (grpcResponse, error) {
resp, err := client.grpcClient.UpdateTtl(metadata, r.grpcRequest)
if err != nil {
return nil, err
}

r.grpcResponse = resp

return resp, nil
}

func (r *IncreaseTtlRequest) interpretGrpcResponse() error {
grpcResp := r.grpcResponse

var resp responses.IncreaseTtlResponse
switch grpcResp.Result.(type) {
case *pb.XUpdateTtlResponse_NotSet:
resp = &responses.IncreaseTtlNotSet{}
case *pb.XUpdateTtlResponse_Missing:
resp = &responses.IncreaseTtlMiss{}
case *pb.XUpdateTtlResponse_Set:
resp = &responses.IncreaseTtlSet{}
default:
return errUnexpectedGrpcResponse(r, r.grpcResponse)
}

r.response = resp

return nil
}
11 changes: 11 additions & 0 deletions momento/requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ type hasCollectionTtl interface {
collectionTtl() *utils.CollectionTtl
}

type hasUpdateTtl interface {
updateTtl() time.Duration
}

func buildError(errorCode string, errorMessage string, originalError error) MomentoError {
return convertMomentoSvcErrorToCustomerError(
momentoerrors.NewMomentoSvcErr(errorCode, errorMessage, originalError),
Expand Down Expand Up @@ -191,6 +195,13 @@ func prepareTtl(r hasTtl, defaultTtl time.Duration) (uint64, error) {
return uint64(ttl.Milliseconds()), nil
}

func prepareUpdateTtl(r hasUpdateTtl) (uint64, error) {
if r.updateTtl() <= time.Duration(0) {
return 0, buildError(momentoerrors.InvalidArgumentError, "updateTtl must be a non-zero positive value", nil)
}
return uint64(r.updateTtl().Milliseconds()), nil
}

func momentoValuesToPrimitiveByteList(values []Value) ([][]byte, error) {
if values == nil {
return nil, buildError(momentoerrors.InvalidArgumentError, "values cannot be nil", nil)
Expand Down
Loading

0 comments on commit f7fa565

Please sign in to comment.