From 90bd07ebc8ad32679bb818a77cc61d1cc2a69362 Mon Sep 17 00:00:00 2001 From: eaddingtonwhite <5491827+eaddingtonwhite@users.noreply.github.com> Date: Wed, 15 Feb 2023 10:07:57 -0800 Subject: [PATCH] feat: adds interface for SimpleCacheClient (#125) * ScsClient -> SimpleCacheClient * feat: adds interface for SimpleCacheClient --------- Co-authored-by: Michael G. Schwern Co-authored-by: eaddingtonwhite <5491827+ellery44@users.noreply.github.com> --- README.template.md | 2 +- examples/list-example/main.go | 2 +- examples/main.go | 2 +- examples/pubsub-example/main.go | 10 ++-- examples/sortedset-example/main.go | 6 +- momento/pub_sub_test.go | 10 ++-- momento/simple_cache_client.go | 92 +++++++++++++++++++---------- momento/simple_cache_client_test.go | 4 +- 8 files changed, 80 insertions(+), 48 deletions(-) diff --git a/README.template.md b/README.template.md index b0385862..34c1d0ba 100644 --- a/README.template.md +++ b/README.template.md @@ -30,7 +30,7 @@ Here is a quickstart you can use in your own project: ### Error Handling -The preferred way of interpreting the return values from `ScsClient` methods is using a `switch` statement to match and handle the specific response type. +The preferred way of interpreting the return values from `SimpleCacheClient` methods is using a `switch` statement to match and handle the specific response type. Here's a quick example: ```go diff --git a/examples/list-example/main.go b/examples/list-example/main.go index a1479a83..c5b5fb31 100644 --- a/examples/list-example/main.go +++ b/examples/list-example/main.go @@ -21,7 +21,7 @@ const ( var ( ctx context.Context - client *momento.ScsClient + client momento.SimpleCacheClient ) func pushFrontToList(value string) { diff --git a/examples/main.go b/examples/main.go index ad853ed5..994cfba6 100644 --- a/examples/main.go +++ b/examples/main.go @@ -72,7 +72,7 @@ func main() { switch r := resp.(type) { case *momento.GetHit: - log.Printf("Lookup resulted in cahce HIT. value=%s\n", r.ValueString()) + log.Printf("Lookup resulted in cache HIT. value=%s\n", r.ValueString()) case *momento.GetMiss: log.Printf("Look up did not find a value key=%s", key) } diff --git a/examples/pubsub-example/main.go b/examples/pubsub-example/main.go index 38085adc..60c6ed2f 100644 --- a/examples/pubsub-example/main.go +++ b/examples/pubsub-example/main.go @@ -20,7 +20,7 @@ func main() { // Initialization client := getClient() ctx := context.Background() - setupCache(*client, ctx) + setupCache(client, ctx) // Instantiate subscriber sub, err := client.TopicSubscribe(ctx, &momento.TopicSubscribeRequest{ @@ -36,7 +36,7 @@ func main() { time.Sleep(time.Second) // Publish messages for the subscriber - publishMessages(*client, ctx) + publishMessages(client, ctx) } func pollForMessages(sub momento.TopicSubscription) { @@ -54,7 +54,7 @@ func pollForMessages(sub momento.TopicSubscription) { } } -func getClient() *momento.ScsClient { +func getClient() momento.SimpleCacheClient { credProvider, err := auth.NewEnvMomentoTokenProvider("MOMENTO_AUTH_TOKEN") if err != nil { panic(err) @@ -70,7 +70,7 @@ func getClient() *momento.ScsClient { return client } -func setupCache(client momento.ScsClient, ctx context.Context) { +func setupCache(client momento.SimpleCacheClient, ctx context.Context) { err := client.CreateCache(ctx, &momento.CreateCacheRequest{ CacheName: "test-cache", }) @@ -84,7 +84,7 @@ func setupCache(client momento.ScsClient, ctx context.Context) { } } -func publishMessages(client momento.ScsClient, ctx context.Context) { +func publishMessages(client momento.SimpleCacheClient, ctx context.Context) { for i := 0; i < 10; i++ { fmt.Printf("publishing message %d\n", i) _, err := client.TopicPublish(ctx, &momento.TopicPublishRequest{ diff --git a/examples/sortedset-example/main.go b/examples/sortedset-example/main.go index 5db409ef..a4385b8e 100644 --- a/examples/sortedset-example/main.go +++ b/examples/sortedset-example/main.go @@ -70,7 +70,7 @@ func main() { displayElements(setName, top5Rsp) } -func getClient() momento.ScsClient { +func getClient() momento.SimpleCacheClient { credProvider, err := auth.NewEnvMomentoTokenProvider("MOMENTO_AUTH_TOKEN") if err != nil { panic(err) @@ -83,10 +83,10 @@ func getClient() momento.ScsClient { if err != nil { panic(err) } - return *client + return client } -func setupCache(client momento.ScsClient, ctx context.Context) { +func setupCache(client momento.SimpleCacheClient, ctx context.Context) { err := client.CreateCache(ctx, &momento.CreateCacheRequest{ CacheName: "test-cache", }) diff --git a/momento/pub_sub_test.go b/momento/pub_sub_test.go index 80d00f96..4f7408e4 100644 --- a/momento/pub_sub_test.go +++ b/momento/pub_sub_test.go @@ -11,16 +11,16 @@ import ( "github.com/momentohq/client-sdk-go/config" ) -var client ScsClient +var client SimpleCacheClient var cacheName = os.Getenv("TEST_CACHE_NAME") func TestMain(m *testing.M) { setup() m.Run() - teardown(&client) + teardown(client) } -func getClient() ScsClient { +func getClient() SimpleCacheClient { credProvider, err := auth.NewEnvMomentoTokenProvider("TEST_AUTH_TOKEN") if err != nil { panic(err) @@ -33,7 +33,7 @@ func getClient() ScsClient { if err != nil { panic(err) } - return *client + return client } func setup() { @@ -52,7 +52,7 @@ func setup() { } } -func publishTopic(ctx context.Context, pubClient ScsClient, i int) { +func publishTopic(ctx context.Context, pubClient SimpleCacheClient, i int) { var topicVal TopicValue if i%2 == 0 { diff --git a/momento/simple_cache_client.go b/momento/simple_cache_client.go index 61f9098f..017501a5 100644 --- a/momento/simple_cache_client.go +++ b/momento/simple_cache_client.go @@ -1,4 +1,4 @@ -// Package momento represents API ScsClient interface accessors including control/data operations, errors, operation requests and responses for the SDK. +// Package momento represents API SimpleCacheClient interface accessors including control/data operations, errors, operation requests and responses for the SDK. package momento import ( @@ -14,8 +14,40 @@ import ( "github.com/momentohq/client-sdk-go/config" ) -// ScsClient represents all information needed for momento client to enable cache control and data operations. -type ScsClient struct { +type SimpleCacheClient interface { + CreateCache(ctx context.Context, request *CreateCacheRequest) error + DeleteCache(ctx context.Context, request *DeleteCacheRequest) error + ListCaches(ctx context.Context, request *ListCachesRequest) (*ListCachesResponse, error) + + Set(ctx context.Context, r *SetRequest) (SetResponse, error) + Get(ctx context.Context, r *GetRequest) (GetResponse, error) + Delete(ctx context.Context, r *DeleteRequest) (DeleteResponse, error) + + TopicSubscribe(ctx context.Context, request *TopicSubscribeRequest) (TopicSubscription, error) + TopicPublish(ctx context.Context, request *TopicPublishRequest) (TopicPublishResponse, error) + + SortedSetFetch(ctx context.Context, r *SortedSetFetchRequest) (SortedSetFetchResponse, error) + SortedSetPut(ctx context.Context, r *SortedSetPutRequest) (SortedSetPutResponse, error) + SortedSetGetScore(ctx context.Context, r *SortedSetGetScoreRequest) (SortedSetGetScoreResponse, error) + SortedSetRemove(ctx context.Context, r *SortedSetRemoveRequest) (SortedSetRemoveResponse, error) + SortedSetGetRank(ctx context.Context, r *SortedSetGetRankRequest) (SortedSetGetRankResponse, error) + SortedSetIncrement(ctx context.Context, r *SortedSetIncrementRequest) (SortedSetIncrementResponse, error) + + ListPushFront(ctx context.Context, r *ListPushFrontRequest) (ListPushFrontResponse, error) + ListPushBack(ctx context.Context, r *ListPushBackRequest) (ListPushBackResponse, error) + ListPopFront(ctx context.Context, r *ListPopFrontRequest) (ListPopFrontResponse, error) + ListPopBack(ctx context.Context, r *ListPopBackRequest) (ListPopBackResponse, error) + ListConcatenateFront(ctx context.Context, r *ListConcatenateFrontRequest) (ListConcatenateFrontResponse, error) + ListConcatenateBack(ctx context.Context, r *ListConcatenateBackRequest) (ListConcatenateBackResponse, error) + ListFetch(ctx context.Context, r *ListFetchRequest) (ListFetchResponse, error) + ListLength(ctx context.Context, r *ListLengthRequest) (ListLengthResponse, error) + ListRemoveValue(ctx context.Context, r *ListRemoveValueRequest) (ListRemoveValueResponse, error) + + Close() +} + +// defaultScsClient represents all information needed for momento client to enable cache control and data operations. +type defaultScsClient struct { credentialProvider auth.CredentialProvider controlClient *services.ScsControlClient dataClient *scsDataClient @@ -28,12 +60,12 @@ type SimpleCacheClientProps struct { DefaultTTL time.Duration } -// NewSimpleCacheClient returns a new ScsClient with provided authToken, DefaultTTLSeconds, and opts arguments. -func NewSimpleCacheClient(props *SimpleCacheClientProps) (*ScsClient, error) { +// NewSimpleCacheClient returns a new SimpleCacheClient with provided authToken, DefaultTTLSeconds, and opts arguments. +func NewSimpleCacheClient(props *SimpleCacheClientProps) (SimpleCacheClient, error) { if props.Configuration.GetClientSideTimeout() < 1 { return nil, momentoerrors.NewMomentoSvcErr(momentoerrors.InvalidArgumentError, "request timeout must not be 0", nil) } - client := &ScsClient{ + client := &defaultScsClient{ credentialProvider: props.CredentialProvider, } @@ -77,7 +109,7 @@ func NewSimpleCacheClient(props *SimpleCacheClientProps) (*ScsClient, error) { return client, nil } -func (c ScsClient) CreateCache(ctx context.Context, request *CreateCacheRequest) error { +func (c defaultScsClient) CreateCache(ctx context.Context, request *CreateCacheRequest) error { if err := isCacheNameValid(request.CacheName); err != nil { return err } @@ -90,7 +122,7 @@ func (c ScsClient) CreateCache(ctx context.Context, request *CreateCacheRequest) return nil } -func (c ScsClient) DeleteCache(ctx context.Context, request *DeleteCacheRequest) error { +func (c defaultScsClient) DeleteCache(ctx context.Context, request *DeleteCacheRequest) error { if err := isCacheNameValid(request.CacheName); err != nil { return err } @@ -103,7 +135,7 @@ func (c ScsClient) DeleteCache(ctx context.Context, request *DeleteCacheRequest) return nil } -func (c ScsClient) ListCaches(ctx context.Context, request *ListCachesRequest) (*ListCachesResponse, error) { +func (c defaultScsClient) ListCaches(ctx context.Context, request *ListCachesRequest) (*ListCachesResponse, error) { rsp, err := c.controlClient.ListCaches(ctx, &models.ListCachesRequest{ NextToken: request.NextToken, }) @@ -116,28 +148,28 @@ func (c ScsClient) ListCaches(ctx context.Context, request *ListCachesRequest) ( }, nil } -func (c ScsClient) Set(ctx context.Context, r *SetRequest) (SetResponse, error) { +func (c defaultScsClient) Set(ctx context.Context, r *SetRequest) (SetResponse, error) { if err := c.dataClient.makeRequest(ctx, r); err != nil { return nil, err } return r.response, nil } -func (c ScsClient) Get(ctx context.Context, r *GetRequest) (GetResponse, error) { +func (c defaultScsClient) Get(ctx context.Context, r *GetRequest) (GetResponse, error) { if err := c.dataClient.makeRequest(ctx, r); err != nil { return nil, err } return r.response, nil } -func (c ScsClient) Delete(ctx context.Context, r *DeleteRequest) (DeleteResponse, error) { +func (c defaultScsClient) Delete(ctx context.Context, r *DeleteRequest) (DeleteResponse, error) { if err := c.dataClient.makeRequest(ctx, r); err != nil { return nil, err } return r.response, nil } -func (c ScsClient) TopicSubscribe(ctx context.Context, request *TopicSubscribeRequest) (TopicSubscription, error) { +func (c defaultScsClient) TopicSubscribe(ctx context.Context, request *TopicSubscribeRequest) (TopicSubscription, error) { clientStream, err := c.pubSubClient.TopicSubscribe(ctx, &TopicSubscribeRequest{ CacheName: request.CacheName, TopicName: request.TopicName, @@ -148,7 +180,7 @@ func (c ScsClient) TopicSubscribe(ctx context.Context, request *TopicSubscribeRe return topicSubscription{grpcClient: clientStream}, err } -func (c ScsClient) TopicPublish(ctx context.Context, request *TopicPublishRequest) (TopicPublishResponse, error) { +func (c defaultScsClient) TopicPublish(ctx context.Context, request *TopicPublishRequest) (TopicPublishResponse, error) { err := c.pubSubClient.TopicPublish(ctx, &TopicPublishRequest{ CacheName: request.CacheName, TopicName: request.TopicName, @@ -162,112 +194,112 @@ func (c ScsClient) TopicPublish(ctx context.Context, request *TopicPublishReques return TopicPublishSuccess{}, err } -func (c ScsClient) SortedSetFetch(ctx context.Context, r *SortedSetFetchRequest) (SortedSetFetchResponse, error) { +func (c defaultScsClient) SortedSetFetch(ctx context.Context, r *SortedSetFetchRequest) (SortedSetFetchResponse, error) { if err := c.dataClient.makeRequest(ctx, r); err != nil { return nil, err } return r.response, nil } -func (c ScsClient) SortedSetPut(ctx context.Context, r *SortedSetPutRequest) (SortedSetPutResponse, error) { +func (c defaultScsClient) SortedSetPut(ctx context.Context, r *SortedSetPutRequest) (SortedSetPutResponse, error) { if err := c.dataClient.makeRequest(ctx, r); err != nil { return nil, err } return r.response, nil } -func (c ScsClient) SortedSetGetScore(ctx context.Context, r *SortedSetGetScoreRequest) (SortedSetGetScoreResponse, error) { +func (c defaultScsClient) SortedSetGetScore(ctx context.Context, r *SortedSetGetScoreRequest) (SortedSetGetScoreResponse, error) { if err := c.dataClient.makeRequest(ctx, r); err != nil { return nil, err } return r.response, nil } -func (c ScsClient) SortedSetRemove(ctx context.Context, r *SortedSetRemoveRequest) (SortedSetRemoveResponse, error) { +func (c defaultScsClient) SortedSetRemove(ctx context.Context, r *SortedSetRemoveRequest) (SortedSetRemoveResponse, error) { if err := c.dataClient.makeRequest(ctx, r); err != nil { return nil, err } return r.response, nil } -func (c ScsClient) SortedSetGetRank(ctx context.Context, r *SortedSetGetRankRequest) (SortedSetGetRankResponse, error) { +func (c defaultScsClient) SortedSetGetRank(ctx context.Context, r *SortedSetGetRankRequest) (SortedSetGetRankResponse, error) { if err := c.dataClient.makeRequest(ctx, r); err != nil { return nil, err } return r.response, nil } -func (c ScsClient) SortedSetIncrement(ctx context.Context, r *SortedSetIncrementRequest) (SortedSetIncrementResponse, error) { +func (c defaultScsClient) SortedSetIncrement(ctx context.Context, r *SortedSetIncrementRequest) (SortedSetIncrementResponse, error) { if err := c.dataClient.makeRequest(ctx, r); err != nil { return nil, err } return r.response, nil } -func (c ScsClient) ListPushFront(ctx context.Context, r *ListPushFrontRequest) (ListPushFrontResponse, error) { +func (c defaultScsClient) ListPushFront(ctx context.Context, r *ListPushFrontRequest) (ListPushFrontResponse, error) { if err := c.dataClient.makeRequest(ctx, r); err != nil { return nil, err } return r.response, nil } -func (c ScsClient) ListPushBack(ctx context.Context, r *ListPushBackRequest) (ListPushBackResponse, error) { +func (c defaultScsClient) ListPushBack(ctx context.Context, r *ListPushBackRequest) (ListPushBackResponse, error) { if err := c.dataClient.makeRequest(ctx, r); err != nil { return nil, err } return r.response, nil } -func (c ScsClient) ListPopFront(ctx context.Context, r *ListPopFrontRequest) (ListPopFrontResponse, error) { +func (c defaultScsClient) ListPopFront(ctx context.Context, r *ListPopFrontRequest) (ListPopFrontResponse, error) { if err := c.dataClient.makeRequest(ctx, r); err != nil { return nil, err } return r.response, nil } -func (c ScsClient) ListPopBack(ctx context.Context, r *ListPopBackRequest) (ListPopBackResponse, error) { +func (c defaultScsClient) ListPopBack(ctx context.Context, r *ListPopBackRequest) (ListPopBackResponse, error) { if err := c.dataClient.makeRequest(ctx, r); err != nil { return nil, err } return r.response, nil } -func (c ScsClient) ListConcatenateFront(ctx context.Context, r *ListConcatenateFrontRequest) (ListConcatenateFrontResponse, error) { +func (c defaultScsClient) ListConcatenateFront(ctx context.Context, r *ListConcatenateFrontRequest) (ListConcatenateFrontResponse, error) { if err := c.dataClient.makeRequest(ctx, r); err != nil { return nil, err } return r.response, nil } -func (c ScsClient) ListConcatenateBack(ctx context.Context, r *ListConcatenateBackRequest) (ListConcatenateBackResponse, error) { +func (c defaultScsClient) ListConcatenateBack(ctx context.Context, r *ListConcatenateBackRequest) (ListConcatenateBackResponse, error) { if err := c.dataClient.makeRequest(ctx, r); err != nil { return nil, err } return r.response, nil } -func (c ScsClient) ListFetch(ctx context.Context, r *ListFetchRequest) (ListFetchResponse, error) { +func (c defaultScsClient) ListFetch(ctx context.Context, r *ListFetchRequest) (ListFetchResponse, error) { if err := c.dataClient.makeRequest(ctx, r); err != nil { return nil, err } return r.response, nil } -func (c ScsClient) ListLength(ctx context.Context, r *ListLengthRequest) (ListLengthResponse, error) { +func (c defaultScsClient) ListLength(ctx context.Context, r *ListLengthRequest) (ListLengthResponse, error) { if err := c.dataClient.makeRequest(ctx, r); err != nil { return nil, err } return r.response, nil } -func (c ScsClient) ListRemoveValue(ctx context.Context, r *ListRemoveValueRequest) (ListRemoveValueResponse, error) { +func (c defaultScsClient) ListRemoveValue(ctx context.Context, r *ListRemoveValueRequest) (ListRemoveValueResponse, error) { if err := c.dataClient.makeRequest(ctx, r); err != nil { return nil, err } return r.response, nil } -func (c ScsClient) Close() { +func (c defaultScsClient) Close() { defer c.controlClient.Close() defer c.dataClient.Close() } diff --git a/momento/simple_cache_client_test.go b/momento/simple_cache_client_test.go index a21752f2..af4edf1a 100644 --- a/momento/simple_cache_client_test.go +++ b/momento/simple_cache_client_test.go @@ -713,7 +713,7 @@ func newCredentialProvider(envVarName string) (auth.CredentialProvider, error) { return credentialProvider, nil } -func newTestClient(credentialProvider auth.CredentialProvider) (*ScsClient, error) { +func newTestClient(credentialProvider auth.CredentialProvider) (SimpleCacheClient, error) { ctx := context.Background() if testCacheName == "" { return nil, errors.New("integration tests require TEST_CACHE_NAME env var") @@ -741,7 +741,7 @@ func newTestClient(credentialProvider auth.CredentialProvider) (*ScsClient, erro return client, nil } -func teardown(client *ScsClient, cacheNames ...string) { +func teardown(client SimpleCacheClient, cacheNames ...string) { ctx := context.Background() for _, cacheName := range cacheNames {