memstore

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: 3 Imported by: 0

README

memstore

godoc

Package memstore is an in-memory storage engine for the SCS session package.

Warning: Because memstore uses in-memory storage only, all session data will be lost when your Go program is stopped or restarted. On the upside though, it is blazingly fast.

In production, memstore should only be used where this volatility is an acceptable trade-off for the high performance, and where lost session data will have a negligible impact on users. As an example, a use case could be using it to track which adverts a visitor has already been shown.

Usage

Installation

Either:

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

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

$ gvt fetch github.com/alexedwards/scs/engine/memstore
$ gvt fetch github.com/alexedwards/scs/session
Example
package main

import (
    "io"
    "net/http"
    "time"

    "github.com/alexedwards/scs/engine/memstore"
    "github.com/alexedwards/scs/session"
)

func main() {
    // Create a new MemStore instance with a cleanup interval of 5 minutes.
    engine := memstore.New(5 * time.Minute)

    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

The memstore package provides a background 'cleanup' goroutine to delete expired session data. This stops the underlying cache from holding on to invalid sessions forever and taking up unnecessary memory.

You can specify how frequently to run the cleanup when creating a new MemStore instance:

// Run a cleanup every 30 minutes.
memstore.New(30 * time.Minute)

// Setting the cleanup interval to zero prevents the cleanup from being run.
memstore.New(0)

Notes

The memstore package is underpinned by the excellent go-cache package.

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

Documentation

Overview

Package memstore is a in-memory storage engine for the SCS session package.

Warning: Because memstore uses in-memory storage only, all session data will be lost when your Go program is stopped or restarted. On the upside though, it is blazingly fast.

In production, memstore should only be used where this volatility is an acceptable trade-off for the high performance, and where lost session data will have a negligible impact on users.

The memstore package provides a background 'cleanup' goroutine to delete expired session data. This stops the underlying cache from holding on to invalid sessions forever and taking up unnecessary memory.

Usage:

func main() {
	// Create a new memstore storage engine with a cleanup interval of 5 minutes.
	engine := memstore.New(5 * time.Minute)

	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 MemStore

type MemStore struct {
	*cache.Cache
}

MemStore represents the currently configured session storage engine. It is essentially a wrapper around a go-cache instance (see https://github.com/patrickmn/go-cache).

func New

func New(cleanupInterval time.Duration) *MemStore

New returns a new MemStore instance.

The cleanupInterval parameter controls how frequently expired session data is removed by the background 'cleanup' goroutine. Setting it to 0 prevents the cleanup goroutine from running (i.e. expired sessions will not be removed).

func (*MemStore) Delete

func (m *MemStore) Delete(token string) error

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

func (*MemStore) Find

func (m *MemStore) Find(token string) (b []byte, exists bool, err error)

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

func (*MemStore) Save

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

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

Jump to

Keyboard shortcuts

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