dynamostore

package
v0.0.0-...-ca7594f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 3, 2017 License: MIT Imports: 4 Imported by: 0

README

dynamostore

godoc

Package dynamostore is a Redis-based storage engine for the SCS session package.

Usage

Installation

Either:

$ go get github.com/alexedwards/scs/engine/dynamostore

Or (recommended) use use gvt to vendor the engine/dynamostore and session sub-packages:

$ gvt fetch github.com/alexedwards/scs/engine/dynamostore
$ gvt fetch github.com/alexedwards/scs/session
Example

The dynamostore package uses the aws-sdk-go DynamoDB client.

package main

import (
    "io"
    "net/http"

    "github.com/alexedwards/scs/session"
    "github.com/alexedwards/scs/engine/dynamostore"
    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/endpoints"
    awsSession "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/dynamodb"
)

func main() {
    // Create a DynamoDB client.
    conf := &aws.Config{Region: aws.String(endpoints.UsEast1RegionID)}
    dynamo := dynamodb.New(awsSession.New(), conf)

    // Create a new dynamostore instance using the DynamoDB client.
    engine := dynamostore.New(dynamo)

    sessionManager := session.Manage(engine)
    http.HandleFunc("/put", putHandler)
    http.HandleFunc("/get", getHandler)
    http.ListenAndServe(":4000", sessionManager(http.DefaultServeMux))
}

func putHandler(w http.ResponseWriter, r *http.Request) {
    err := session.PutString(r, "message", "Hello world!")
    if err != nil {
        http.Error(w, err.Error(), 500)
    }
}

func getHandler(w http.ResponseWriter, r *http.Request) {
    msg, err := session.GetString(r, "message")
    if err != nil {
        http.Error(w, err.Error(), 500)
    }
    io.WriteString(w, msg)
}
Cleaning up expired session data

DynamoDB has Time To Live option.

Set TTL to DynamoDB session table, then expired session keys automatically removed.

Notes

Full godoc documentation: https://godoc.org/github.com/alexedwards/scs/engine/dynamostore.

Documentation

Overview

Package dynamostore is a DynamoDB-based storage engine for the SCS session package.

The dynamostore package relis on the aws-sdk-go client. (https://godoc.org/github.com/aws/aws-sdk-go/service/dynamodb)

Usage:

package main

import (
	"net/http"

	"github.com/databrary/scs/session"
	"github.com/databrary/scs/engine/dynamostore"
	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/endpoints"
	awsSession "github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/dynamodb"
)

func main() {
	// Create a DynamoDB client.
	conf := &aws.Config{Region: aws.String(endpoints.UsEast1RegionID)}
	dynamo := dynamodb.New(awsSession.New(), conf)

	// Create a new DynamoStore instance using the DynamoDB client.
	engine := dynamostore.New(dynamo)

	sessionManager := session.Manage(engine)
	http.ListenAndServe(":4000", sessionManager(http.DefaultServeMux))
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DynamoStore

type DynamoStore struct {
	DB *dynamodb.DynamoDB
	// contains filtered or unexported fields
}

DynamoStore represents the currently configured session storage engine. It is essentially a wrapper around a DynamoDB client. And table is a table name session stored. token, data, expiry are key names.

func New

func New(dynamo *dynamodb.DynamoDB) *DynamoStore

New returns a new DynamoStore instance. The client parameter shoud be a pointer to a aws-sdk-go DynamoDB client. See https://godoc.org/github.com/aws/aws-sdk-go/service/dynamodb#DynamoDB.

func NewWithOption

func NewWithOption(dynamo *dynamodb.DynamoDB, table string, token string, data string, expiry string, ttl string) *DynamoStore

NewWithOption returns a new DynamoStore instance. The client parameter shoud be a pointer to a aws-sdk-go DynamoDB client. See https://godoc.org/github.com/aws/aws-sdk-go/service/dynamodb#DynamoDB. The parameter table is DynamoDB tabel name, and token/data/expiry are key names.

func (*DynamoStore) DataName

func (d *DynamoStore) DataName() string

DataName returns session data key name.

func (*DynamoStore) Delete

func (d *DynamoStore) Delete(token string) error

Delete removes a session token and corresponding data from the ResisStore instance.

func (*DynamoStore) ExpiryName

func (d *DynamoStore) ExpiryName() string

ExpiryName returns session expiry key name.

func (*DynamoStore) Find

func (d *DynamoStore) Find(token string) (b []byte, found bool, err error)

Find returns the data for a given session token from the DynamoStore instance. If the session token is not found or is expired, the returned exists flag will be set to false.

func (*DynamoStore) Ping

func (d *DynamoStore) Ping() error

Ping checks to exisit session table in DynamoDB.

func (*DynamoStore) Save

func (d *DynamoStore) Save(token string, b []byte, expiry time.Time) error

Save adds a session token and data to the RedisStore instance with the given expiry time. If the session token already exists then the data and expiry time are updated.

func (*DynamoStore) TTLName

func (d *DynamoStore) TTLName() string

TTLName returns session expiry key name.

func (*DynamoStore) TableName

func (d *DynamoStore) TableName() string

TableName returns session table name.

func (*DynamoStore) TokenName

func (d *DynamoStore) TokenName() string

TokenName returns session token key name.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL