session

package module
v0.0.0-...-c36cd05 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2019 License: GPL-3.0, GPL-3.0-or-later Imports: 9 Imported by: 0

README

session

练习session

package main

import (
	"fmt"
	"gitea.com/iwhot/session"
	"log"
	"net/http"
)

var globalSessions *session.Manage

func init() {
	var err error
	globalSessions, err = session.NewManager("memory", "goSessionid", 3600)
	if err != nil {
		fmt.Println(err)
		return
	}
	go globalSessions.GC()
	fmt.Println("fd")
}

func sayHelloHandler(w http.ResponseWriter, r *http.Request) {

	cookie, err := r.Cookie("name")
	if err == nil {
		fmt.Println(cookie.Value)
		fmt.Println(cookie.Domain)
		fmt.Println(cookie.Expires)
	}
	//fmt.Fprintf(w, "Hello world!\n") //这个写入到w的是输出到客户端的
}
func login(w http.ResponseWriter, r *http.Request) {
	sess := globalSessions.SessionStart(w, r)
	val := sess.Get("username")
	if val != nil {
		fmt.Println(val)
	} else {
		sess.Set("username", "jerry")
		fmt.Println("set session")
	}
}
func loginOut(w http.ResponseWriter, r *http.Request) {
	//销毁
	globalSessions.SessionDestroy(w, r)
	fmt.Println("session destroy")
}

func main() {
	http.HandleFunc("/", sayHelloHandler) //	设置访问路由
	http.HandleFunc("/login", login)
	http.HandleFunc("/loginout", loginOut) //销毁
	log.Fatal(http.ListenAndServe(":8080", nil))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(name string, provide Provider)

注册 由实现Provider接口的结构体调用

Types

type FromMemory

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

session来自内存 实现

func (*FromMemory) SessionDestroy

func (frommemory *FromMemory) SessionDestroy(sid string) error

func (*FromMemory) SessionGC

func (frommemory *FromMemory) SessionGC(maxLifeTime int64)

func (*FromMemory) SessionInit

func (frommemory *FromMemory) SessionInit(sid string) (Session, error)

func (*FromMemory) SessionRead

func (frommemory *FromMemory) SessionRead(sid string) (Session, error)

func (*FromMemory) SessionUpdate

func (frommemory *FromMemory) SessionUpdate(sid string) error

type Manage

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

session管理

func NewManager

func NewManager(providerName, cookieName string, expire int64) (*Manage, error)

实例化一个session管理器

func (*Manage) GC

func (manager *Manage) GC()

func (*Manage) SessionDestroy

func (manager *Manage) SessionDestroy(w http.ResponseWriter, r *http.Request)

销毁session 同时删除cookie

func (*Manage) SessionStart

func (manager *Manage) SessionStart(w http.ResponseWriter, r *http.Request) (session Session)

判断当前请求的cookie中是否存在有效的session,存在返回,否则创建

type Provider

type Provider interface {
	//初始化一个session,sid根据需要生成后传入
	SessionInit(sid string) (Session, error)
	//根据sid,获取session
	SessionRead(sid string) (Session, error)
	//销毁session
	SessionDestroy(sid string) error
	//回收
	SessionGC(maxLifeTime int64)
}

session存储方式接口

type Session

type Session interface {
	Get(key interface{}) interface{}
	Set(key, value interface{}) error
	Delete(key interface{}) error
	SessionID() string
}

Session操作接口

type SessionStore

type SessionStore struct {
	LastAccessedTime time.Time //最后访问时间
	// contains filtered or unexported fields
}

session实现

func (*SessionStore) Delete

func (st *SessionStore) Delete(key interface{}) error

删除

func (*SessionStore) Get

func (st *SessionStore) Get(key interface{}) interface{}

获取session

func (*SessionStore) SessionID

func (st *SessionStore) SessionID() string

func (*SessionStore) Set

func (st *SessionStore) Set(key, value interface{}) error

设置

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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