session

package module
v0.0.0-...-0ed530a Latest Latest
Warning

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

Go to latest
Published: May 2, 2018 License: MIT Imports: 17 Imported by: 1

README

session

  • 将cookieStore分类为客户端存储器
  • 在manage中保存session信息,用于manager之间的交互,同时GC机制清理manager中保存的过期session
  • 添加session-id,用于在管理器中查找已存在的session进行返回
  • 添加Finder,用于在管理器中查找符合条件的session

TODO

支持data查询 ip锁定,多点登录,支持

  • 同一个ip允许多个session
  • 不同的ip不允许相同的session
  • 一个session 一个用户

demo


package main

import (
	"io"
	"log"
	"net/http"
	"time"

	scs "github.com/ipiao/session"
)

var sessionManager = scs.NewCookieManager("u46IpCV9y5Vlur8YvODJEhgOY8m9JVE4")

func main() {
	sessionManager.Option(scs.Persist(true))
	sessionManager.Option(scs.LifeTime(time.Second * 30))
	
	// mysql
	// db, err := sql.Open("mysql", "root:1001@tcp(127.0.0.1:3306)/test")
    // if err != nil {
    //    log.Fatal(err)
    // }
    // var store = mysqlstore.New(db, time.Minute*10)
    // sessionManager = scs.NewManager(store)

	mux := http.NewServeMux()
	mux.HandleFunc("/put", putHandler)
	mux.HandleFunc("/get", getHandler)

	http.ListenAndServe(":4000", mux)
}

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

	session, err := sessionManager.Load(r)
	if err != nil {
		http.Error(w, err.Error(), 500)
	}

	err = session.PutToResponseWriter(w, "message", "Hello world!")
	// sessionManager.Write(session, w)
	// session.WriteToResponseWriter(w)
	if err != nil {
		http.Error(w, err.Error(), 500)
	}
	log.Println("PUT:", session.GetData())
}

func getHandler(w http.ResponseWriter, r *http.Request) {
	session, err := sessionManager.Load(r)
	if err != nil {
		http.Error(w, err.Error(), 500)
	}
	session.WriteToResponseWriter(w)
	sessions := sessionManager.FindSeesion()
	log.Println("GET:", len(sessions))
	sessions1 := sessionManager.FindSeesion(scs.FindByKVEq("message", "Hello world!"))
	log.Println("GET KVEq:", len(sessions1))
	sessions2 := sessionManager.FindSeesion(scs.FindTimeOut())
	log.Println("GET TimeOut:", len(sessions2))
	message, err := session.GetString("message")
	if err != nil {
		http.Error(w, err.Error(), 500)
	}
	log.Println("GET:", session.GetData())
	io.WriteString(w, message)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrTypeAssertionFailed = errors.New("type assertion failed")

ErrTypeAssertionFailed 断言错误

Functions

This section is empty.

Types

type Finder

type Finder func(*Session) bool

Finder 查找session

func FindByID

func FindByID(id string) Finder

FindByID 按id查找

func FindByKVEq

func FindByKVEq(key string, value interface{}) Finder

FindByKVEq 按键值查找,值相等

func FindByToken

func FindByToken(token string) Finder

FindByToken 按token查找

func FindTimeIn

func FindTimeIn() Finder

FindTimeIn 查找未超时

func FindTimeOut

func FindTimeOut() Finder

FindTimeOut 查找超时

func MakeFinder

func MakeFinder(fds ...Finder) Finder

MakeFinder 将多个变成一个

type Handle

type Handle func(*Session)

Handle 操作session,错误内部处理

func HandleSetKV

func HandleSetKV(key string, value interface{}) Handle

HandleSetKV 设置键值

func MakeHandle

func MakeHandle(fds ...Handle) Handle

MakeHandle 将多个变成一个

type Manager

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

Manager session控制器

func NewCookieManager

func NewCookieManager(key string, opts ...Option) *Manager

NewCookieManager 返回cookie-session管理器 客户端存储

func NewManager

func NewManager(store Store, opts ...Option) *Manager

NewManager 返回session管理器 并且伴随生成一个gc任务

func (*Manager) Close

func (m *Manager) Close() error

Close 关闭

func (*Manager) FindHandleSeesion

func (m *Manager) FindHandleSeesion(fd Finder, hd Handle) []*Session

FindHandleSeesion 查找并处理session

func (*Manager) FindSeesion

func (m *Manager) FindSeesion(fds ...Finder) []*Session

FindSeesion 查找session

func (*Manager) Load

func (m *Manager) Load(r *http.Request) (*Session, error)

Load 选择manager中存在时候,从中获取

func (*Manager) LoadIM

func (m *Manager) LoadIM(r *http.Request) (*Session, error)

LoadIM 不从manager中查找获取,从中获取 Load ingore manager

func (*Manager) NewSession

func (m *Manager) NewSession() (*Session, error)

NewSession 创建并且返回一个Session

func (*Manager) Option

func (m *Manager) Option(opts ...Option)

Option ...

func (*Manager) RunGC

func (m *Manager) RunGC()

RunGC 运行gc,简单设定间隔

func (*Manager) Stat

func (m *Manager) Stat()

Stat 状态

func (*Manager) Use

func (m *Manager) Use(next http.Handler) http.Handler

Use 用作中间件,作为示例,具体使用根据业务场景而定

func (*Manager) Write

func (m *Manager) Write(session *Session, w http.ResponseWriter) error

Write 写入数据

type Option

type Option func(o *Options)

Option 处理option赋值

func Domain

func Domain(domain string) Option

Domain 设置domain

func HTTPOnly

func HTTPOnly(b bool) Option

HTTPOnly ..

func IdleTime

func IdleTime(d time.Duration) Option

IdleTime ..

func LifeTime

func LifeTime(d time.Duration) Option

LifeTime ..

func Name

func Name(name string) Option

Name 设置name

func Path

func Path(p string) Option

Path ..

func Persist

func Persist(b bool) Option

Persist ..

func Secure

func Secure(b bool) Option

Secure ..

func TouchInterval

func TouchInterval(d time.Duration) Option

TouchInterval ..

type Options

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

Options 大部分继承自http Cookie里字段

func NewOptions

func NewOptions(opts ...Option) Options

NewOptions 新建Options

type Session

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

Session 一个会话状态

func (*Session) Clear

func (s *Session) Clear() error

Clear 清楚所有的数据

func (*Session) Destroy

func (s *Session) Destroy() error

Destroy 摧毁session

func (*Session) Exists

func (s *Session) Exists(key string) (bool, error)

Exists 是否存在给定键的数据

func (*Session) Get

func (s *Session) Get(key string) (interface{}, bool, error)

Get 获取key对应的值 err:如果将从store中获取值,将会有错误返回

func (*Session) GetBool

func (s *Session) GetBool(key string) (bool, error)

GetBool 获取Bool

func (*Session) GetBytes

func (s *Session) GetBytes(key string) ([]byte, error)

GetBytes 获取

func (*Session) GetData

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

GetData 获取session Data

func (*Session) GetExpiry

func (s *Session) GetExpiry() time.Time

GetExpiry 获取过期时间点

func (*Session) GetFloat64

func (s *Session) GetFloat64(key string) (float64, error)

GetFloat64 获取

func (*Session) GetID

func (s *Session) GetID() string

GetID 获取sessionID

func (*Session) GetInt

func (s *Session) GetInt(key string) (int, error)

GetInt 获取

func (*Session) GetInt64

func (s *Session) GetInt64(key string) (int64, error)

GetInt64 获取

func (*Session) GetObject

func (s *Session) GetObject(key string, dst interface{}) error

GetObject 获取

func (*Session) GetString

func (s *Session) GetString(key string) (string, error)

GetString 获取String

func (*Session) GetTime

func (s *Session) GetTime(key string) (time.Time, error)

GetTime 获取

func (*Session) GetToken

func (s *Session) GetToken() string

GetToken 获取sessionToken

func (*Session) Keys

func (s *Session) Keys() ([]string, error)

Keys 返回所有的键

func (*Session) LastAccessTime

func (s *Session) LastAccessTime() time.Time

LastAccessTime 获取上次时间

func (*Session) MayTouch

func (s *Session) MayTouch() bool

MayTouch 建议刷新

func (*Session) Pop

func (s *Session) Pop(key string) (interface{}, bool, error)

Pop 移除并返回

func (*Session) PopBool

func (s *Session) PopBool(key string) (bool, error)

PopBool 移除并返回

func (*Session) PopBoolFromResponseWriter

func (s *Session) PopBoolFromResponseWriter(w http.ResponseWriter, key string) (bool, error)

PopBoolFromResponseWriter 移除并返回

func (*Session) PopBytes

func (s *Session) PopBytes(key string) ([]byte, error)

PopBytes 移除并返回

func (*Session) PopBytesFromResponseWriter

func (s *Session) PopBytesFromResponseWriter(w http.ResponseWriter, key string) ([]byte, error)

PopBytesFromResponseWriter 移除并返回

func (*Session) PopFloat64

func (s *Session) PopFloat64(key string) (float64, error)

PopFloat64 移除并返回

func (*Session) PopFloat64FromResponseWriter

func (s *Session) PopFloat64FromResponseWriter(w http.ResponseWriter, key string) (float64, error)

PopFloat64FromResponseWriter 移除并返回

func (*Session) PopFromResponseWriter

func (s *Session) PopFromResponseWriter(w http.ResponseWriter, key string) (interface{}, bool, error)

PopFromResponseWriter 移除并返回

func (*Session) PopInt

func (s *Session) PopInt(key string) (int, error)

PopInt 移除并返回

func (*Session) PopInt64

func (s *Session) PopInt64(key string) (int64, error)

PopInt64 移除并返回

func (*Session) PopInt64FromResponseWriter

func (s *Session) PopInt64FromResponseWriter(w http.ResponseWriter, key string) (int64, error)

PopInt64FromResponseWriter 移除并返回

func (*Session) PopIntFromResponseWriter

func (s *Session) PopIntFromResponseWriter(w http.ResponseWriter, key string) (int, error)

PopIntFromResponseWriter 移除并返回

func (*Session) PopObject

func (s *Session) PopObject(key string, dst interface{}) error

PopObject 移除并返回

func (*Session) PopObjectFromResponseWriter

func (s *Session) PopObjectFromResponseWriter(w http.ResponseWriter, key string, dst interface{}) error

PopObject 移除并返回

func (*Session) PopString

func (s *Session) PopString(key string) (string, error)

PopString 移除并返回

func (*Session) PopStringFromResponseWriter

func (s *Session) PopStringFromResponseWriter(w http.ResponseWriter, key string) (string, error)

PopStringFromResponseWriter 移除并返回

func (*Session) PopTime

func (s *Session) PopTime(key string) (time.Time, error)

PopTime 移除并返回

func (*Session) PopTimeFromResponseWriter

func (s *Session) PopTimeFromResponseWriter(w http.ResponseWriter, key string) (time.Time, error)

PopTimeFromResponseWriter 移除并返回

func (*Session) Put

func (s *Session) Put(key string, val interface{}) error

Put 存入,存在则替换

func (*Session) PutBool

func (s *Session) PutBool(key string, val bool) error

PutBool 存入,存在则替换

func (*Session) PutBoolToResponseWriter

func (s *Session) PutBoolToResponseWriter(w http.ResponseWriter, key string, val bool) error

PutBoolToResponseWriter 存储

func (*Session) PutBytes

func (s *Session) PutBytes(key string, val []byte) error

PutBytes 存入,存在则替换

func (*Session) PutBytesToResponseWriter

func (s *Session) PutBytesToResponseWriter(w http.ResponseWriter, key string, val []byte) error

PutBytesToResponseWriter 存储

func (*Session) PutFloat64

func (s *Session) PutFloat64(key string, val float64) error

PutFloat64 存入,存在则替换

func (*Session) PutFloat64ToResponseWriter

func (s *Session) PutFloat64ToResponseWriter(w http.ResponseWriter, key string, val float64) error

PutFloat64ToResponseWriter 存储

func (*Session) PutInt

func (s *Session) PutInt(key string, val int) error

PutInt 存入,存在则替换

func (*Session) PutInt64

func (s *Session) PutInt64(key string, val int64) error

PutInt64 存入,存在则替换

func (*Session) PutInt64ToResponseWriter

func (s *Session) PutInt64ToResponseWriter(w http.ResponseWriter, key string, val int64) error

PutInt64ToResponseWriter 存储

func (*Session) PutIntToResponseWriter

func (s *Session) PutIntToResponseWriter(w http.ResponseWriter, key string, val int) error

PutIntToResponseWriter 存储

func (*Session) PutObject

func (s *Session) PutObject(key string, val interface{}) error

PutObject 存入,存在则替换

func (*Session) PutObjectToResponseWriter

func (s *Session) PutObjectToResponseWriter(w http.ResponseWriter, key string, val interface{}) error

PutObject 存入,存在则替换

func (*Session) PutString

func (s *Session) PutString(key string, val string) error

PutString 存储string

func (*Session) PutStringToResponseWriter

func (s *Session) PutStringToResponseWriter(w http.ResponseWriter, key string, val string) error

PutStringToResponseWriter 存储string

func (*Session) PutTime

func (s *Session) PutTime(key string, val time.Time) error

PutTime 存入,存在则替换

func (*Session) PutTimeToResponseWriter

func (s *Session) PutTimeToResponseWriter(w http.ResponseWriter, key string, val time.Time) error

PutTimeToResponseWriter 存储

func (*Session) PutToResponseWriter

func (s *Session) PutToResponseWriter(w http.ResponseWriter, key string, val interface{}) error

PutToResponseWriter 存入,存在则替换

func (*Session) Remove

func (s *Session) Remove(key string) error

Remove 移除给定键数据

func (*Session) TimeOut

func (s *Session) TimeOut() bool

TimeOut 判断session是否过期 1.验证过期时间 2.如果未过期,到数据库中查找,如果存不存在

func (*Session) Write

func (s *Session) Write(bs []byte) error

Write 相当于刷新一下时间

func (*Session) WriteToResponseWriter

func (s *Session) WriteToResponseWriter(w http.ResponseWriter) error

WriteToResponseWriter 将session数据写入到返回中

type Store

type Store interface {
	// 存储session,如果token一致,则更新session,同时改写过期时间
	Save(token string, b []byte, expiry time.Time) (err error)

	// 移除给定token的session并且获取,如果不存在,返回nil
	Delete(token string) (err error)

	// 查找给定token的session
	Find(token string) (b []byte, found bool, err error)

	// session数据落地,与Loads主要是针对memoryStore
	Dumps() (err error)

	// 加载保存的session数据
	Loads() (bs [][]byte, err error)
}

Store 存储session 存储的实际是Session的data和deadline

Directories

Path Synopsis
example
stores
boltstore
Package boltstore is a boltdb based session store for the SCS session package.
Package boltstore is a boltdb based session store for the SCS session package.
buntstore
Package buntstore is a buntdb based session store for the SCS session package.
Package buntstore is a buntdb based session store for the SCS session package.
dynamostore
Package dynamostore is a DynamoDB-based session store for the SCS session package.
Package dynamostore is a DynamoDB-based session store for the SCS session package.
memstore
Package memstore is a in-memory session store for the SCS session package.
Package memstore is a in-memory session store for the SCS session package.
mysqlstore
Package mysqlstore is a MySQL-based session store for the SCS session package.
Package mysqlstore is a MySQL-based session store for the SCS session package.
pgstore
Package pgstore is a PostgreSQL-based session store for the SCS session package.
Package pgstore is a PostgreSQL-based session store for the SCS session package.
qlstore
Package qlstore is a ql-based session store for the SCS session package.
Package qlstore is a ql-based session store for the SCS session package.
redisstore
Package redisstore is a Redis-based session store for the SCS session package.
Package redisstore is a Redis-based session store for the SCS session package.

Jump to

Keyboard shortcuts

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