-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: use updated repo generator v2 (#316)
* chore: use updated repo generator v2 Co-authored-by: Chris Price <cprice404@users.noreply.github.com>
- Loading branch information
1 parent
651f787
commit f4927c0
Showing
4 changed files
with
83 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,33 @@ | ||
{{ ossHeader }} | ||
|
||
## Getting Started :running: | ||
## Packages | ||
|
||
### Requirements | ||
|
||
- [Go](https://go.dev/dl/) | ||
- A Momento Auth Token is required, you can generate one using | ||
the [Momento CLI](https://github.com/momentohq/momento-cli) | ||
|
||
### Examples | ||
|
||
Check out full working code in the [examples](./examples/README.md) directory of this repository! | ||
|
||
### Installation | ||
The Momento Golang SDK is available here on github: [momentohq/client-sdk-go](https://github.com/momentohq/client-sdk-go). | ||
|
||
```bash | ||
go get github.com/momentohq/client-sdk-go | ||
``` | ||
|
||
### Usage | ||
## Usage | ||
|
||
Checkout our [examples](./examples/README.md) directory for complete examples of how to use the SDK. | ||
|
||
Here is a quickstart you can use in your own project: | ||
|
||
```go | ||
{{ usageExampleCode }} | ||
{% include "./examples/readme.go" %} | ||
``` | ||
|
||
### Error Handling | ||
## Getting Started and Documentation | ||
|
||
The preferred way of interpreting the return values from `CacheClient` methods is using a `switch` statement to match and handle the specific response type. | ||
Here's a quick example: | ||
Documentation is available on the [Momento Docs website](https://docs.momentohq.com). | ||
|
||
```go | ||
switch r := resp.(type) { | ||
case *momento.GetHit: | ||
log.Printf("Lookup resulted in cahce HIT. value=%s\n", r.ValueString()) | ||
default: | ||
// you can handle other cases via pattern matching in other `switch case`, or a default case | ||
// via the `default` block. For each return value your IDE should be able to give you code | ||
// completion indicating the other possible "case"; in this case, `momento.GetMiss`. | ||
} | ||
``` | ||
## Examples | ||
|
||
Using this approach, you get a type-safe `GetHit` object in the case of a cache hit. | ||
But if the cache read results in a Miss, you'll also get a type-safe object that you can use to get more info about what happened. | ||
|
||
In cases where you get an error response, it can be treated as `momentoErr` using `As` method and it always include an `momentoErr.Code` that you can use to check the error type: | ||
|
||
```go | ||
_, err := client.Set(ctx, &momento.SetRequest{ | ||
CacheName: cacheName, | ||
Key: momento.String(key), | ||
Value: momento.String(value), | ||
}) | ||
|
||
if err != nil { | ||
var momentoErr momento.MomentoError | ||
if errors.As(err, &momentoErr) { | ||
if momentoErr.Code() != momento.TimeoutError { | ||
// this would represent a client-side timeout, and you could fall back to your original data source | ||
} | ||
} | ||
} | ||
``` | ||
Check out full working code in the [examples](./examples/README.md) directory of this repository! | ||
|
||
### Tuning | ||
## Developing | ||
|
||
Coming soon... | ||
If you are interested in contributing to the SDK, please see the [CONTRIBUTING](./CONTRIBUTING.md) docs. | ||
|
||
{{ ossFooter }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
"github.com/momentohq/client-sdk-go/auth" | ||
"github.com/momentohq/client-sdk-go/config" | ||
"github.com/momentohq/client-sdk-go/config/logger" | ||
"github.com/momentohq/client-sdk-go/momento" | ||
"github.com/momentohq/client-sdk-go/responses" | ||
) | ||
|
||
func main() { | ||
context := context.Background() | ||
configuration := config.LaptopLatestWithLogger(logger.NewNoopMomentoLoggerFactory()).WithClientTimeout(15 * time.Second) | ||
credentialProvider, err := auth.NewEnvMomentoTokenProvider("MOMENTO_AUTH_TOKEN") | ||
if err != nil { | ||
panic(err) | ||
} | ||
defaultTtl := time.Duration(9999) | ||
|
||
client, err := momento.NewCacheClient(configuration, credentialProvider, defaultTtl) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
key := uuid.NewString() | ||
value := uuid.NewString() | ||
log.Printf("Setting key: %s, value: %s\n", key, value) | ||
_, _ = client.Set(context, &momento.SetRequest{ | ||
CacheName: "cache-name", | ||
Key: momento.String(key), | ||
Value: momento.String(value), | ||
Ttl: time.Duration(9999), | ||
}) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
resp, err := client.Get(context, &momento.GetRequest{ | ||
CacheName: "cache-name", | ||
Key: momento.String(key), | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
switch r := resp.(type) { | ||
case *responses.GetHit: | ||
log.Printf("Lookup resulted in cache HIT. value=%s\n", r.ValueString()) | ||
case *responses.GetMiss: | ||
log.Printf("Look up did not find a value key=%s", key) | ||
} | ||
} |