tikvstore

package module
v0.0.0-...-72a6a60 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2021 License: MIT Imports: 13 Imported by: 0

README

tikvstore

Go

A session store backend for gorilla/sessions - src.

Installation

go get github.com/ryicoh/tikvstore

Documentation

See http://www.gorillatoolkit.org/pkg/sessions for full documentation on underlying interface.

Example
package main

import (
	"github.com/ryicoh/tikvstore"
)
func main() {
	ctx := context.TODO()
	cli, err := rawkv.NewClient(ctx, []string{endpoint}, config.DefaultConfig().Security)
	if err != nil {
		panic(err)
	}
	defer cli.Close()

	// Fetch new store.
	store, err := tikvstore.NewTiKVStore(client, []byte("secret-key"))
	if err != nil {
		panic(err)
	}

	// Get a session.
	session, err = store.Get(req, "session-key")
	if err != nil {
		log.Error(err.Error())
	}

	// Add a value.
	session.Values["foo"] = "bar"

	// Save.
	if err = sessions.Save(req, rsp); err != nil {
		t.Fatalf("Error saving session: %v", err)
	}

	// Delete session.
	session.Options.MaxAge = -1
	if err = sessions.Save(req, rsp); err != nil {
		t.Fatalf("Error saving session: %v", err)
	}

	// Change session storage configuration for MaxAge = 10 days.
	store.SetMaxAge(10 * 24 * 3600)
}

Documentation

Overview

Package redistore is a session store backend for gorilla/sessions

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewTiKVGinStore

func NewTiKVGinStore(client *rawkv.Client, keyPairs ...[]byte) ginsessions.Store

NewTiKVStore instantiates a TiKVStore for Gin.

Types

type GobSerializer

type GobSerializer struct{}

GobSerializer uses gob package to encode the session map

func (GobSerializer) Deserialize

func (s GobSerializer) Deserialize(d []byte, ss *sessions.Session) error

Deserialize back to map[interface{}]interface{}

func (GobSerializer) Serialize

func (s GobSerializer) Serialize(ss *sessions.Session) ([]byte, error)

Serialize using gob

type JSONSerializer

type JSONSerializer struct{}

JSONSerializer encode the session map to JSON.

func (JSONSerializer) Deserialize

func (s JSONSerializer) Deserialize(d []byte, ss *sessions.Session) error

Deserialize back to map[string]interface{}

func (JSONSerializer) Serialize

func (s JSONSerializer) Serialize(ss *sessions.Session) ([]byte, error)

Serialize to JSON. Will err if there are unmarshalable key values

type SessionSerializer

type SessionSerializer interface {
	Deserialize(d []byte, ss *sessions.Session) error
	Serialize(ss *sessions.Session) ([]byte, error)
}

SessionSerializer provides an interface hook for alternative serializers

type TiKVStore

type TiKVStore struct {
	Codecs []securecookie.Codec

	DefaultMaxAge int // default TiKV TTL for a MaxAge == 0 session
	// contains filtered or unexported fields
}

TiKVStore stores sessions in a tikv backend.

Example
// TiKVStore
cli, err := rawkv.NewClient(context.TODO(), []string{"127.0.0.1:2379"}, config.DefaultConfig().Security)
if err != nil {
	panic(err)
}
defer cli.Close()

store := NewTiKVStore(cli, []byte("secret-key"))
defer store.Close()
Output:

func NewTiKVStore

func NewTiKVStore(client *rawkv.Client, keyPairs ...[]byte) *TiKVStore

NewTiKVStore instantiates a TiKVStore.

func (*TiKVStore) Close

func (s *TiKVStore) Close() error

Close closes the underlying *tikv.Pool

func (*TiKVStore) Delete

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

Delete removes the session from tikv, and sets the cookie to expire.

WARNING: This method should be considered deprecated since it is not exposed via the gorilla/sessions interface. Set session.Options.MaxAge = -1 and call Save instead. - July 18th, 2013

func (*TiKVStore) Get

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

Get returns a session for the given name after adding it to the registry.

See gorilla/sessions FilesystemStore.Get().

func (*TiKVStore) New

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

New returns a session for the given name without adding it to the registry.

See gorilla/sessions FilesystemStore.New().

func (*TiKVStore) Options

func (s *TiKVStore) Options(opts ginsessions.Options)

Options sets configuration for a session.

See gin-contrib/sessions https://github.com/gin-contrib/sessions/blob/master/sessions.go

func (*TiKVStore) Save

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

Save adds a single session to the response.

func (*TiKVStore) SetKeyPrefix

func (s *TiKVStore) SetKeyPrefix(p string)

SetKeyPrefix set the prefix

func (*TiKVStore) SetMaxAge

func (s *TiKVStore) SetMaxAge(v int)

SetMaxAge restricts the maximum age, in seconds, of the session record both in database and a browser. This is to change session storage configuration. If you want just to remove session use your session `s` object and change it's `Options.MaxAge` to -1, as specified in

http://godoc.org/github.com/gorilla/sessions#Options

Default is the one provided by this package value - `sessionExpire`. Set it to 0 for no restriction. Because we use `MaxAge` also in SecureCookie crypting algorithm you should use this function to change `MaxAge` value.

func (*TiKVStore) SetMaxLength

func (s *TiKVStore) SetMaxLength(l int)

SetMaxLength sets TiKVStore.maxLength if the `l` argument is greater or equal 0 maxLength restricts the maximum length of new sessions to l. If l is 0 there is no limit to the size of a session, use with caution. The default for a new TiKVStore is 4096. TiKV allows for max. value sizes of up to 1.5MB (https://tikv.io/docs/v3.4/dev-guide/limit/) Default: 4096,

func (*TiKVStore) SetSerializer

func (s *TiKVStore) SetSerializer(ss SessionSerializer)

SetSerializer sets the serializer

Jump to

Keyboard shortcuts

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