session

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2025 License: MIT Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

gFly Session

Copyright © 2023, gFly
https://www.gfly.dev
All rights reserved.

gFly Session - Memory

Usage

Install

go get -u github.com/gflydev/session@v1.0.1
go get -u github.com/gflydev/session/memory@v1.0.1

Quick usage main.go

import (
    "github.com/gflydev/session"
    sessionMemory "github.com/gflydev/session/memory"
)

// Setup session
session.Register(sessionMemory.New())
core.RegisterSession(session.New())
Controller (Page/API)
// Set session
c.SetSession("foo", utils.UnsafeStr(utils.RandByte(make([]byte, 128))))

// Get session parameter `foo`
foo := c.GetSession("foo")

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotSetProvider = errors.New("Not set a session provider")
	ErrEmptySessionID = errors.New("Empty session id")
)

Functions

func Base64Decode

func Base64Decode(dst *Dict, src []byte) error

Base64Decode base64 decode

func Base64Encode

func Base64Encode(src Dict) ([]byte, error)

Base64Encode base64 encode

func MSGPDecode

func MSGPDecode(dst *Dict, src []byte) error

MSGPDecode MessagePack decode

func MSGPEncode

func MSGPEncode(src Dict) ([]byte, error)

MSGPEncode MessagePack encode

func Register

func Register(p Provider)

Register assign session provider type `memory`, `redis`....

Types

type Adapter added in v1.0.1

type Adapter struct {
}

Adapter instance for Session with Core

func New

func New() *Adapter

New create session manager and session adapter

func (*Adapter) Get added in v1.0.1

func (v *Adapter) Get(c *core.Ctx, key string) interface{}

func (*Adapter) Set added in v1.0.1

func (v *Adapter) Set(c *core.Ctx, key string, value interface{})

type Config

type Config struct {
	// cookie name
	CookieName string

	// cookie domain
	Domain string

	// If you want to delete the cookie when the browser closes, set it to -1.
	//
	//  0 means no expire, (24 years)
	// -1 means when browser closes
	// >0 is the time.Duration which the session cookies should expire.
	Expiration time.Duration

	// gc life time to execute it
	GCLifetime time.Duration

	// set whether to pass this bar cookie only through HTTPS
	Secure bool

	// allows you to declare if your cookie should be restricted to a first-party or same-site context.
	// possible values: lax, strict, none
	CookieSameSite fasthttp.CookieSameSite

	// sessionID is in url query
	SessionIDInURLQuery bool

	// sessionName in url query
	SessionNameInURLQuery string

	// sessionID is in http header
	SessionIDInHTTPHeader bool

	// sessionName in http header
	SessionNameInHTTPHeader string

	// SessionIDGeneratorFunc should returns a random session id.
	SessionIDGeneratorFunc func() []byte

	// IsSecureFunc should return whether the communication channel is secure
	// in order to set the secure flag to true according to Secure flag.
	IsSecureFunc func(*fasthttp.RequestCtx) bool

	// EncodeFunc session value serialize func
	EncodeFunc func(src Dict) ([]byte, error)

	// DecodeFunc session value unSerialize func
	DecodeFunc func(dst *Dict, src []byte) error

	// Logger
	Logger Logger
	// contains filtered or unexported fields
}

Config configuration of session manager

func NewDefaultConfig

func NewDefaultConfig() Config

NewDefaultConfig returns a new default configuration

type Dict

type Dict struct {
	KV map[string]interface{}
}

Dict memory store.

func (*Dict) DecodeMsg

func (z *Dict) DecodeMsg(dc *msgp.Reader) (err error)

DecodeMsg implements msgp.Decodable

func (*Dict) EncodeMsg

func (z *Dict) EncodeMsg(en *msgp.Writer) (err error)

EncodeMsg implements msgp.Encodable

func (*Dict) MarshalMsg

func (z *Dict) MarshalMsg(b []byte) (o []byte, err error)

MarshalMsg implements msgp.Marshaler

func (*Dict) Msgsize

func (z *Dict) Msgsize() (s int)

Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message

func (*Dict) UnmarshalMsg

func (z *Dict) UnmarshalMsg(bts []byte) (o []byte, err error)

UnmarshalMsg implements msgp.Unmarshaler

type Logger

type Logger interface {
	Print(v ...interface{})
	Printf(format string, v ...interface{})
}

type Provider

type Provider interface {
	Get(id []byte) ([]byte, error)
	Save(id, data []byte, expiration time.Duration) error
	Destroy(id []byte) error
	Regenerate(id, newID []byte, expiration time.Duration) error
	Count() int
	NeedGC() bool
	GC() error
}

Provider interface implemented by providers

type Session

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

Session manages the users sessions

func NewSession added in v1.0.1

func NewSession(cfg Config) *Session

NewSession returns a configured manager

func (*Session) Destroy

func (s *Session) Destroy(ctx *fasthttp.RequestCtx) error

Destroy destroys the session of the current user

func (*Session) Get

func (s *Session) Get(ctx *fasthttp.RequestCtx) (*Store, error)

Get returns the user session if it does not exist, it will be generated

func (*Session) Regenerate

func (s *Session) Regenerate(ctx *fasthttp.RequestCtx) error

Regenerate generates a new session id to the current user

func (*Session) Save

func (s *Session) Save(ctx *fasthttp.RequestCtx, store *Store) error

Save saves the user session

Warning: Don't use the store after exec this function, because, you will lose the after data For avoid it, defer this function in your request handler

func (*Session) SetProvider

func (s *Session) SetProvider(provider Provider) error

SetProvider sets the session provider used by the sessions manager

type Store

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

Store represents the user session

func NewStore

func NewStore() *Store

NewStore returns a new empty store

func (*Store) Delete

func (s *Store) Delete(key string)

Delete deletes a value from the given key

func (*Store) DeleteBytes

func (s *Store) DeleteBytes(key []byte)

DeleteBytes deletes a value from the given key

func (*Store) Flush

func (s *Store) Flush()

Flush removes all stored values

func (*Store) Get

func (s *Store) Get(key string) interface{}

Get returns a value from the given key

func (*Store) GetAll

func (s *Store) GetAll() Dict

GetAll returns all stored values

func (*Store) GetBytes

func (s *Store) GetBytes(key []byte) interface{}

GetBytes returns a value from the given key

func (*Store) GetExpiration

func (s *Store) GetExpiration() time.Duration

GetExpiration returns the expiration for current session

func (*Store) GetSessionID

func (s *Store) GetSessionID() []byte

GetSessionID returns the session id

func (*Store) HasExpirationChanged

func (s *Store) HasExpirationChanged() bool

HasExpirationChanged checks wether the expiration has been changed

func (*Store) Ptr

func (s *Store) Ptr() *Dict

Ptr returns the internal store pointer

func (*Store) Reset

func (s *Store) Reset()

Reset resets the store

func (*Store) Set

func (s *Store) Set(key string, value interface{})

Set saves a value for the given key

func (*Store) SetBytes

func (s *Store) SetBytes(key []byte, value interface{})

SetBytes saves a value for the given key

func (*Store) SetExpiration

func (s *Store) SetExpiration(expiration time.Duration) error

SetExpiration sets the expiration for current session

func (*Store) SetSessionID

func (s *Store) SetSessionID(id []byte)

SetSessionID sets the session id

Directories

Path Synopsis
memory module
redis module

Jump to

Keyboard shortcuts

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