session

package
v0.0.0-...-40ef913 Latest Latest
Warning

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

Go to latest
Published: May 5, 2021 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Config = struct {
	CookieName     string // 客户端储存sessionID的cookie的名字
	CookieLifeTime int    // cookie保存时间,单位秒
	HTTPOnly       bool
	Gclifetime     int64 // GC间隔时间,并不一定精准,单位秒
	Maxlifetime    int64 // session保存时间,单位秒
}{
	"SESSIONID",
	0,
	true,
	3600,
	600,
}

Config 配置参数

View Source
var Engine struct {
	Read    func(sid string) (map[string]*Data, error) // 通过sessionID读取数据
	Save    func(sid string, d map[string]*Data) error // 保存数据
	Destroy func(sid string) error                     // 通过sessionID删除数据
	GC      func()
}

Engine session引擎开发相关,需要考虑并发情况

View Source
var UtilGenerateSessionID = func() string {
	return util.BytesString(util.GetRandStr())
}

UtilGenerateSessionID 随机生成不重复SessionID

Functions

func StartGC

func StartGC()

StartGC 启动一个协程间歇执行GC

func UtilGetSIDFromRequest

func UtilGetSIDFromRequest(r *http.Request) (sid string)

UtilGetSIDFromRequest 从请求中获取sessionID

Types

type Data

type Data struct {
	V      string
	Expire int64 // 过期时间戳,0表示永不过期
}

Data session数据结构

type Store

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

Store 用于存储和操作session

func Start

func Start(w http.ResponseWriter, r *http.Request) (session *Store, err error)

Start 从cookie中获取sessionID,再读取session数据并返回

func (*Store) Delete

func (s *Store) Delete(key string)

Delete 删除

func (*Store) DeleteAll

func (s *Store) DeleteAll()

DeleteAll 删除全部

func (*Store) Destroy

func (s *Store) Destroy()

Destroy 删除包括sessionID在内的所有数据,需要调用Save()生效

func (*Store) Get

func (s *Store) Get(key string) (value string)

Get 获取

func (*Store) ID

func (s *Store) ID() string

ID 获取sessionID

func (*Store) Save

func (s *Store) Save() error

Save 在所有session操作完毕后必须调用一次,保存数据

func (*Store) Set

func (s *Store) Set(key, value string, second int64)

Set 设置 second: 有效时间,0永不过期,单位秒

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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