Skip to content

Commit

Permalink
feat: adds interface for SimpleCacheClient (#125)
Browse files Browse the repository at this point in the history
* ScsClient -> SimpleCacheClient
* feat: adds interface for SimpleCacheClient
---------

Co-authored-by: Michael G. Schwern <schwern@pobox.com>
Co-authored-by: eaddingtonwhite <5491827+ellery44@users.noreply.github.com>
  • Loading branch information
3 people authored and cprice404 committed Mar 14, 2023
1 parent f6f0c7a commit e003656
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 48 deletions.
2 changes: 1 addition & 1 deletion README.template.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/list-example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (

var (
ctx context.Context
client *momento.ScsClient
client momento.SimpleCacheClient
)

func pushFrontToList(value string) {
Expand Down
2 changes: 1 addition & 1 deletion examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
10 changes: 5 additions & 5 deletions examples/pubsub-example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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) {
Expand All @@ -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)
Expand All @@ -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",
})
Expand All @@ -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{
Expand Down
6 changes: 3 additions & 3 deletions examples/sortedset-example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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",
})
Expand Down
10 changes: 5 additions & 5 deletions momento/pub_sub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -33,7 +33,7 @@ func getClient() ScsClient {
if err != nil {
panic(err)
}
return *client
return client
}

func setup() {
Expand All @@ -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 {
Expand Down
92 changes: 62 additions & 30 deletions momento/simple_cache_client.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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
Expand All @@ -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,
}

Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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,
})
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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()
}
Expand Down
Loading

0 comments on commit e003656

Please sign in to comment.