Documentation
¶
Index ¶
- Constants
- Variables
- func GenerateSessionID() string
- func GetSessionID(ctx context.Context) string
- func Middleware() func(next http.Handler) http.Handler
- func StartSession(ctx context.Context, storage StorageDriver, opt ...*Options)
- type Callback
- type CookieCarrier
- type Manager
- func (m *Manager) Close() error
- func (m *Manager) Del(w http.ResponseWriter, r *http.Request, name string) error
- func (m *Manager) Destroy(w http.ResponseWriter, r *http.Request) error
- func (m *Manager) Flashes(w http.ResponseWriter, r *http.Request, name string, dst any) error
- func (m *Manager) Get(w http.ResponseWriter, r *http.Request, name string, dst any) error
- func (m *Manager) GetSessionId(w http.ResponseWriter, r *http.Request) (string, error)
- func (m *Manager) GetUp(w http.ResponseWriter, r *http.Request, name string, dst any) error
- func (m *Manager) GetUpByDuration(w http.ResponseWriter, r *http.Request, name string, dst any, ...) error
- func (m *Manager) GetUpByRemainRatio(w http.ResponseWriter, r *http.Request, name string, dst any, ratio float64, ...) error
- func (m *Manager) GetUpByRemainTime(w http.ResponseWriter, r *http.Request, name string, dst any, ...) error
- func (m *Manager) GetWithSessionID(sessionID string, name string, dst any) error
- func (m *Manager) Set(w http.ResponseWriter, r *http.Request, value *Value) error
- func (m *Manager) SetVal(value *Value) error
- func (m *Manager) SetWithNewSessionId(w http.ResponseWriter, r *http.Request, value *Value) error
- func (m *Manager) UpdateSessionId(w http.ResponseWriter, r *http.Request, sessionId ...string) (string, error)
- type Options
- type Session
- type StorageDriver
- type Value
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 ¶
Functions ¶
func GetSessionID ¶
func Middleware ¶
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) GetSessionId ¶
GetSessionId 获取sessionId
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 (*Manager) SetWithNewSessionId ¶
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 存储驱动接口 - 只负责存储,不处理业务逻辑
Source Files
¶
Click to show internal directories.
Click to hide internal directories.