Skip to content

Latest commit

 

History

History
111 lines (83 loc) · 3.71 KB

README.md

File metadata and controls

111 lines (83 loc) · 3.71 KB

logo

project status project stability

Momento Client Library for Go

Momento Cache is a fast, simple, pay-as-you-go caching solution without any of the operational overhead required by traditional caching solutions. This repo contains the source code for the Momento client library for Go.

To get started with Momento you will need a Momento Auth Token. You can get one from the Momento Console.

Packages

The Momento Golang SDK is available here on github: momentohq/client-sdk-go.

go get github.com/momentohq/client-sdk-go

Usage

Checkout our examples directory for complete examples of how to use the SDK.

Here is a quickstart you can use in your own project:

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_API_KEY")
	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)
	}
}

Getting Started and Documentation

Documentation is available on the Momento Docs website.

Examples

Check out full working code in the examples directory of this repository!

Developing

If you are interested in contributing to the SDK, please see the CONTRIBUTING docs.


For more info, visit our website at https://gomomento.com!