session

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2025 License: Apache-2.0 Imports: 14 Imported by: 5

Documentation

Index

Constants

View Source
const ContextRandomStringLength = 4

Variables

View Source
var (
	ErrorStorageNotSet     = values.Errorf(201, "session Storage not set")
	ErrorSessionIdEmpty    = values.Errorf(202, "session id empty")
	ErrorSessionLocked     = values.Errorf(203, "session locked")
	ErrorSessionTypeError  = values.Errorf(204, "session type error")
	ErrorSessionNotExist   = values.Errorf(205, "session not exist")
	ErrorSessionTypeExpire = values.Errorf(206, "session expire")
	ErrorSessionIllegal    = values.Errorf(207, "session illegal")
	ErrorSessionUnknown    = values.Errorf(208, "session unknown error")
	ErrorSessionReplaced   = values.Errorf(209, "session replaced")

	ErrorSessionSecretIllegal = values.Errorf(210, "session secret illegal")
)
View Source
var Heartbeat int32 = 10 //心跳(S)
View Source
var MaxDataIndex = int32(math.MaxInt32 - 1000)
View Source
var Options = struct {
	Name    string //session cookie name
	Token   token  //token生成和解析方式
	MaxAge  int64  //有效期(S)
	Secret  string //16位秘钥
	Storage Storage
}{
	Name:   "_cookie_vars",
	MaxAge: 3600,
	Secret: "UVFGHIJABCopqDNO",
}

Functions

func Decode

func Decode(sid string) (uid string, err error)

func Encode

func Encode(uid string) (sid string, err error)

func NewSetter

func NewSetter(id string, data any) storage.Setter

Types

type Data

type Data struct {
	sync.Mutex
	values.Values
	// contains filtered or unexported fields
}

Data 用户登录信息,不要直接修改 Player.Values 信息

func NewData

func NewData(uuid string, vs map[string]any, token ...string) *Data

func (*Data) Atomic

func (this *Data) Atomic() int32

Atomic 生成一个自增的包序列号

func (*Data) Heartbeat

func (this *Data) Heartbeat(v ...int32) int32

func (*Data) Id

func (this *Data) Id() string

func (*Data) Index

func (this *Data) Index() int32

func (*Data) Is

func (this *Data) Is(v *Data) bool

func (*Data) KeepAlive

func (this *Data) KeepAlive()

func (*Data) Merge

func (this *Data) Merge(p *Data, locked ...bool)

func (*Data) Reset

func (this *Data) Reset()

func (*Data) Set

func (this *Data) Set(key string, value any, locked ...bool) any

func (*Data) TryResetIndex

func (this *Data) TryResetIndex()

func (*Data) UUID

func (this *Data) UUID() string

func (*Data) Update

func (this *Data) Update(data map[string]any, locked ...bool)

Update 批量设置Cookie信息

type Memory

type Memory struct {
	storage.Storage
}

func NewMemory

func NewMemory(cap ...int) *Memory

func (*Memory) Create

func (this *Memory) Create(uuid string, data map[string]any) (p *Data, err error)

Create 创建新SESSION,返回SESSION Index Create会自动设置有效期 Create新数据为锁定状态

func (*Memory) Delete

func (this *Memory) Delete(p *Data) error

func (*Memory) New

func (this *Memory) New(p *Data) error

func (*Memory) Update

func (this *Memory) Update(p *Data, data map[string]any) (err error)

func (*Memory) Verify

func (this *Memory) Verify(token string) (p *Data, err error)

type Redis

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

func NewRedis

func NewRedis(address interface{}, prefix ...string) (c *Redis, err error)

func (*Redis) Create

func (this *Redis) Create(uuid string, data map[string]any) (p *Data, err error)

Create ttl过期时间(s)

func (*Redis) Delete

func (this *Redis) Delete(p *Data) (err error)

func (*Redis) New

func (this *Redis) New(p *Data) error

func (*Redis) Update

func (this *Redis) Update(p *Data, data map[string]any) (err error)

func (*Redis) Verify

func (this *Redis) Verify(token string) (p *Data, err error)

Verify 获取session镜像数据

type Session

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

func New

func New() *Session

func (*Session) Create

func (this *Session) Create(uuid string, data map[string]any) (token string, err error)

Create 创建SESSION,uuid 用户唯一ID,可以检测是不是重复登录

func (*Session) Delete

func (this *Session) Delete() (err error)

func (*Session) Release

func (this *Session) Release()

Release 释放 session 由HTTP SERVER 自动调用

func (*Session) Set

func (this *Session) Set(key string, val any)

func (*Session) Update

func (this *Session) Update(vs map[string]any)

Update 批量修改Session信息

func (*Session) Verify

func (this *Session) Verify(token string) (err error)

Verify 验证TOKEN信息是否有效,并初始化session

type Setter

type Setter struct {
	*Data //数据接口
	// contains filtered or unexported fields
}

func (*Setter) Get

func (this *Setter) Get() interface{}

func (*Setter) KeepAlive

func (this *Setter) KeepAlive()

KeepAlive 设置有效期(s)

func (*Setter) Set

func (this *Setter) Set(data interface{})

type Storage

type Storage interface {
	New(data *Data) error                                             //同Create
	Verify(token string) (data *Data, err error)                      //验证TOKEN信息
	Create(uuid string, value map[string]any) (data *Data, err error) //用户登录创建新session
	Update(data *Data, value map[string]any) error                    //更新session数据
	Delete(data *Data) error                                          //退出登录删除SESSION 	//关闭服务器时断开连接等
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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