session

package
v0.0.0-...-1781da9 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2014 License: MIT Imports: 9 Imported by: 2

Documentation

Overview

session的操作包。仅支持go1.3+

session的存储方式多种多样,大家可以通过实现Store接口,之后 将该Store的实例传递给session.New()的第一个参数,就可以实现 自定义的session存储。

不能将一个Store指针同时传递给多个session.New(),它们必须是 独立的实例,否则将会发生串号等错误:

// 错误用法
mem := memory.New()
inst1 := session.New(mem, ...)
inst2 := session.New(mem, ...)
// inst1,inst2使用同一个Store,将会发生串号现象。

// 正确用法
mem1 := memory.New()
mem2 := memory.New()
inst1 := session.New(mem1, ...)
inst2 := session.New(mem2, ...)
// inst1,inst2将会是2个完全独立的存储系统,互不干扰。

Index

Constants

View Source
const Version = "0.1.0.140816"

当前库的版本号

Variables

This section is empty.

Functions

func FreeSession

func FreeSession(s *Session)

释放Session。 供Store实现者调用。

func SessionAccessed

func SessionAccessed(s *Session) time.Time

获取最后的存取时间

func SessionData

func SessionData(s *Session) map[interface{}]interface{}

获取Session中的数据。 供Store实现者调用

Types

type Instance

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

func New

func New(store Store, sessionIDName string, lifetime int, secure bool) *Instance

实例一个新的Instance。会自动开始GC操作。

sessionIDName:用于保存sessionid的变量名称,如果用cookie传递,则为cookie的名 称,不能包含特殊字符; lifetime:sessionid的生存时间,如果是cookie传递,则为cookie的max-age属性; secure: 是否只能用于https、ssl等安全链接,若为cookie传递,则为cookie的secure属性。

func (*Instance) DeleteSession

func (i *Instance) DeleteSession(w http.ResponseWriter, sessid string) error

删除一个Session

func (*Instance) EndSession

func (i *Instance) EndSession(w http.ResponseWriter, r *http.Request) error

结束当前的session。这将会使保存Sessionid的cookie失效。

func (*Instance) Free

func (i *Instance) Free()

释放整个store的数据并停止回收GC。

func (*Instance) StartSession

func (i *Instance) StartSession(w http.ResponseWriter, r *http.Request) (*Session, error)

开始一个新的Session 应该在任何输出之前调用,否则不会输出成功

func (*Instance) StartSessionWithForm

func (i *Instance) StartSessionWithForm(r *http.Request) (*Session, error)

type Session

type Session struct {
	sync.Mutex
	// contains filtered or unexported fields
}

针对Session的相关操作

func NewSession

func NewSession(sid string, data map[interface{}]interface{}, s Store) *Session

新建Session。一般由store的实现者调用。

func (*Session) Delete

func (s *Session) Delete(key interface{})

删除某个Session的值

func (*Session) Get

func (s *Session) Get(key interface{}) (val interface{}, found bool)

获取某个Session的值

func (*Session) ID

func (s *Session) ID() string

sessionid

func (*Session) MustGet

func (s *Session) MustGet(key, def interface{}) interface{}

同Get,但是在值不存在时,返回def作为默认值。

func (*Session) Release

func (s *Session) Release() error

保存并删除当前Session的内容。 必须释放Session,否则可能造成内存泄漏。

func (*Session) Save

func (s *Session) Save() error

保存Session所做的修改到Store中

func (*Session) Set

func (s *Session) Set(key, val interface{})

设置某个Session的值

type Store

type Store interface {
	// 返回数据,不存在就创建
	Get(sid string) (sess *Session, err error)

	// 保存数据
	Save(sess *Session) error

	// 删除数据
	Delete(sid string) error

	// 保存Session中的内容,并将使sess对象处于不可用状态
	Release(sess *Session) error

	// 执行一次垃圾回收,时间小于duration都会被回收
	GC(duration int)

	// 释放整个空间
	Free()
}

各类Session存储系统需要实现的接口。

Directories

Path Synopsis
stores
file
session的文件存储模式
session的文件存储模式
memory
session的内存存储模式
session的内存存储模式

Jump to

Keyboard shortcuts

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