ramstore

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2023 License: BSD-3-Clause Imports: 7 Imported by: 3

README

ramstore

An in memory session store to work with Gorilla web toolkit sessions.

This API is now stable. Any future changes will be bacckward compatible with existing code. However, any future function or data structure in "draft" mode may change in incompatible ways. Such function or data structure will be clearly marked as "draft" in the documentation.

Using

import "github.com/keep94/ramstore"

Online Documentation

Gorilla web toolkit sessions documentation is available [here] (http://www.gorillatoolkit.org/pkg/sessions).

Documentation

Overview

Package ramstore implements an in-memory session store for Gorilla Web Toolkit. In-memory sessions expire after a set time of inactivity.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RAMSessions

type RAMSessions struct {
	// contains filtered or unexported fields
}

RAMSessions stores session data. Session data for a particular session expires after a set time of inactivity for that session. RAMSessions can be safely used with multiple goroutines. Clients should not use this type directly, but should use RAMStore instead.

func NewRAMSessions

func NewRAMSessions(maxAge int) *RAMSessions

NewRAMSessions creates a new RAMSessions instance. maxAge is the maximum allowed inactivity in seconds before data for a particular session expires.

func (*RAMSessions) AsPoller

func (r *RAMSessions) AsPoller() SessionData

AsPoller returns a view of this instance that does not keep session data from expiring when it is fetched.

func (*RAMSessions) Get

func (r *RAMSessions) Get(id string) map[interface{}]interface{}

Get returns a shallow copy of the session data for a particular session ID. Get returns nil if the session ID does not exist or if the session data for the session ID expired from too much inactivity.

func (*RAMSessions) GetData

func (r *RAMSessions) GetData(id string) (map[interface{}]interface{}, error)

GetData returns Get(id), nil

func (*RAMSessions) Poll

func (r *RAMSessions) Poll(id string) map[interface{}]interface{}

Poll works just like Get except that calling Poll does not keep session data from expiring.

func (*RAMSessions) Purge

func (r *RAMSessions) Purge()

Purge removes session data that has already expired. Clients need not call this manually as a separate go routine calls this periodically.

func (*RAMSessions) Save

func (r *RAMSessions) Save(id string, data map[interface{}]interface{})

Save saves new session data for a particular session ID. Save makes a shallow copy of data before saving it.

func (*RAMSessions) SaveData

func (r *RAMSessions) SaveData(
	id string, data map[interface{}]interface{}) error

SaveData calls Save(id, data) and returns nil.

type RAMStore

type RAMStore struct {
	Options *sessions.Options
	Data    *RAMSessions
	// Client sets either Data or SData leaving the other nil. If both Data and
	// SData are non-nil then SData takes precedence.
	SData SessionData
}

RAMStore is an in-memory session store for Gorilla Web Toolkit. This store makes shallow copies of maps, so value objects such as string and int can be safely used with in-memory sessions with no regard for synchronization. Care must be taken with reference types such as pointers, maps, or slices. To what these reference refer must be treated as frozen to prevent contention with other go-routines.

func NewRAMStore

func NewRAMStore(maxAge int) *RAMStore

NewRAMStore creates a new in-memory session store. maxAge is the maximum time of inactivity in seconds before a session expires. NewRamStore is a convenience routine that returns a *RamStore with the Data field set and the SData field nil. The returned *RamStore uses '/' as the cookie path.

func (*RAMStore) Get

func (s *RAMStore) Get(r *http.Request, name string) (*sessions.Session, error)

Get retrieves the session. name is the name of the cookie storing the session ID. If Get is called a second time with the same request pointer, the session is retrieved from the request's context rather than from this store. Callers should call context.Clear() in a defer statement after calling Get.

func (*RAMStore) New

func (s *RAMStore) New(r *http.Request, name string) (*sessions.Session, error)

New fetches the session from this store. name is the name of the cookie holding the session ID. Get calls New if the session is not already cached in the request's context. This implementation of New never returns a non-nil error if client is storing sessions data in a *RamSessions instance.

func (*RAMStore) Save

func (s *RAMStore) Save(r *http.Request, w http.ResponseWriter, session *sessions.Session) error

Save saves a session to the store. If the session has no ID, Save assigns a random one. This implementation of Save never returns a non-nil error if client is storing sessions data in a *RamSessions instance.

type SessionData

type SessionData interface {
	// GetData gets session data by id. GetData must return a shallow copy of
	// the data.
	GetData(id string) (map[interface{}]interface{}, error)
	// SaveData saves session data by id. SaveData must make a shallow copy of
	// values before saving.
	SaveData(id string, values map[interface{}]interface{}) error
}

Clients may choose to write their own implementation of the SessionData interface instead of using RAMSessions.

Jump to

Keyboard shortcuts

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