session

package
v2.3.1 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Interval   = time.Hour // 默认检测频率
	CookieName = "vsclubId"
	ExpireTime = 1 * time.Hour // session默认24小时过期
)
View Source
const (
	CookieCarrierCookie = "cookie"
	CookieCarrierHeader = "header"
)
View Source
const SessionID = "session_id"

noinspection all

Variables

View Source
var (
	ErrUnSupport  = errors.New("不支持的 session 载体")
	ErrNotFound   = errors.New("session 不存在")
	ErrNotPointer = errors.New("session 变量目标必须是指针")
)

Functions

func GenerateSessionID

func GenerateSessionID() string

GenerateSessionID 创建session ID

func GetSessionID

func GetSessionID(ctx context.Context) string

func Middleware

func Middleware() func(next http.Handler) http.Handler

Middleware session 中间件 noinspection all

func StartSession

func StartSession(ctx context.Context, storage StorageDriver, opt ...*Options)

Types

type Callback

type Callback struct {
	BeforeRenew func(sessionID string, expire dataType.CustomTime, data any) error
	AfterRenew  func(sessionID string, expire dataType.CustomTime, data any) error
}

type CookieCarrier

type CookieCarrier string

func (CookieCarrier) String

func (c CookieCarrier) String() string

type Manager

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

func GetSession

func GetSession() *Manager

func New

func New(ctx context.Context, storage StorageDriver, opt ...*Options) *Manager

noinspection all

func (*Manager) Close

func (m *Manager) Close() error

func (*Manager) Del

func (m *Manager) Del(w http.ResponseWriter, r *http.Request, name string) error

func (*Manager) Destroy

func (m *Manager) Destroy(w http.ResponseWriter, r *http.Request) error

func (*Manager) Flashes

func (m *Manager) Flashes(w http.ResponseWriter, r *http.Request, name string, dst any) error

Flashes 获取并删除session

func (*Manager) Get

func (m *Manager) Get(w http.ResponseWriter, r *http.Request, name string, dst any) error

Get 获取session

func (*Manager) GetSessionId

func (m *Manager) GetSessionId(w http.ResponseWriter, r *http.Request) (string, error)

GetSessionId 获取sessionId

func (*Manager) GetUp

func (m *Manager) GetUp(w http.ResponseWriter, r *http.Request, name string, dst any) error

GetUp 获取session并更新有效期

func (*Manager) GetUpByDuration

func (m *Manager) GetUpByDuration(w http.ResponseWriter, r *http.Request, name string, dst any, duration time.Duration, callbacks ...Callback) error

GetUpByDuration 根据duration 距离session 的过期时间少了duration那么长时间后,就延长 duration 比如:设置了15天的有效期,duration设置成1天,当有效期剩余不到 15-1 的时候延长duration

func (*Manager) GetUpByRemainRatio

func (m *Manager) GetUpByRemainRatio(w http.ResponseWriter, r *http.Request, name string, dst any, ratio float64, callbacks ...Callback) error

GetUpByRemainRatio 根据剩余时间占比自动续期 ratio: 0.0-1.0 之间的值,表示触发续期的剩余时间占比 例如:session总时长15天,ratio=0.2,当剩余时间少于3天(15*0.2)时触发续期

func (*Manager) GetUpByRemainTime

func (m *Manager) GetUpByRemainTime(w http.ResponseWriter, r *http.Request, name string, dst any, duration time.Duration, callbacks ...Callback) error

GetUpByRemainTime 根据剩余时间更新session 当session 的有效期小于duration,那么将session的有效期延长到 session.Duration-duration 比如:设置了15天有效期,duration设置一天,那么当检测到session的有效期 不大于一天的时候就更新session

func (*Manager) GetWithSessionID

func (m *Manager) GetWithSessionID(sessionID string, name string, dst any) error

func (*Manager) Set

func (m *Manager) Set(w http.ResponseWriter, r *http.Request, value *Value) error

Set 设置session

func (*Manager) SetVal

func (m *Manager) SetVal(value *Value) error

func (*Manager) SetWithNewSessionId

func (m *Manager) SetWithNewSessionId(w http.ResponseWriter, r *http.Request, value *Value) error

func (*Manager) UpdateSessionId

func (m *Manager) UpdateSessionId(w http.ResponseWriter, r *http.Request, sessionId ...string) (string, error)

UpdateSessionId 更新sessionId

type Options

type Options struct {
	DisableGc     bool             `json:"disable_gc" yaml:"disable_gc" ini:"disable_gc"`             // 是否禁用session gc
	GcProbability float64          `json:"gc_probability" yaml:"gc_probability" ini:"gc_probability"` // session gc 概率
	CheckInterval time.Duration    `json:"check_interval" yaml:"check_interval" ini:"check_interval"` // session 检测默认有效期
	Carrier       CookieCarrier    `json:"carrier" yaml:"carrier" ini:"carrier"`                      // session 载体,默认cookie
	Cookie        cookiekit.Config `json:"cookie" yaml:"cookie" ini:"cookie"`
}

Options session 配置

type Session

type Session struct {
	Id         string                `json:"id" gorm:"primaryKey;autoIncrement:false;type:varchar(64);not null;index;comment:Session ID"`
	Name       string                `json:"name" gorm:"primaryKey;autoIncrement:false;type:varchar(128);not null;index;comment:Session的名字"`
	Values     dataType.SessionValue `json:"values" gorm:"comment:session数据"`
	CreateTime dataType.CustomTime   `json:"create_time" gorm:"comment:session 创建时间"`
	ExpireTime dataType.CustomTime   `json:"expire_time" gorm:"not null;index;comment:session 过期时间"`
	Duration   time.Duration         `json:"duration" gorm:"comment:session有效期"`
}

Session session 数据结构

type StorageDriver

type StorageDriver interface {
	Save(session *Session) error
	Get(sessionId, name string) (*Session, error)
	GetAll(sessionId string) ([]*Session, error)
	Delete(sessionId, name string) error
	DeleteAll(sessionId string) error

	// GC 相关
	GC(ctx context.Context) error
	Close() error
}

StorageDriver 存储驱动接口 - 只负责存储,不处理业务逻辑

type Value

type Value struct {
	SessionID string        // 可自定义session id
	Field     string        // session 值字段
	Value     any           // session 值
	TTL       time.Duration // 有效期
}

Directories

Path Synopsis
adapter
storage

Jump to

Keyboard shortcuts

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