Skip to content

Latest commit

 

History

History
172 lines (126 loc) · 4.92 KB

README_CN.md

File metadata and controls

172 lines (126 loc) · 4.92 KB

logo

Sourcegraph build godoc license go_Report release powered_by platforms

fasthttpsession 是一个快速且强大的 fasthttp session 管理包

English Document

描述

fasthttpsession 是 Go 实现的一个 session 管理器。它只能用于 fasthttp 框架, 目前支持的 session 存储如下:

  • file
  • memcache
  • memory
  • mysql
  • postgres
  • redis
  • sqlite3

功能

  • 关注代码架构和扩展的设计。
  • 提供全面的 session 存储。
  • 方便的 session 存储切换。
  • 可自由定制数据序列化函数。
  • 实现了并发 map(ccmap.go) 去提高性能。

安装

要求是 Go 至少是 v1.7。

$ go get -u github.com/phachon/fasthttpsession
$ go get ./...

使用

快速开始

// fasthttpsession use memory provider

import (
	"github.com/mikhailzhukov/fasthttpsession"
	"github.com/mikhailzhukov/fasthttpsession/memory"
	"github.com/valyala/fasthttp"
	"log"
	"os"
)

// 默认的 session 全局配置
var session = fasthttpsession.NewSession(fasthttpsession.NewDefaultConfig())

func main()  {
	// 必须在使用之前指定 session 的存储
	err := session.SetProvider("memory", &memory.Config{})
	if err != nil {
		log.Println(err.Error())
		os.Exit(1)
	}
	addr := ":8086"
	log.Println("fasthttpsession example server listen: "+addr)
	
	// fasthttp start listen serve
	err = fasthttp.ListenAndServe(addr, requestHandle)
	if err != nil {
		log.Println("listen server error :"+err.Error())
	}
}

// request handler
func requestHandle(ctx *fasthttp.RequestCtx) {
	// start session
	sessionStore, err := session.Start(ctx)
	if err != nil {
		ctx.SetBodyString(err.Error())
		return
	}
	// 必须 defer sessionStore.save(ctx)
	defer sessionStore.Save(ctx)

	sessionStore.Set("name", "fasthttpsession")

	ctx.SetBodyString(fmt.Sprintf("fasthttpsession setted key name= %s ok", sessionStore.Get("name").(string)))
}

自定义配置

如果您不想使用默认配置,请使用以下结构自定义你想要的配置。

type Config struct {

	// cookie name
	CookieName string
	
	// cookie domain
	Domain string
	
	// If you want to delete the cookie when the browser closes, set it to -1.
	//
	//  0 means no expire, (24 years)
	// -1 means when browser closes
	// >0 is the time.Duration which the session cookies should expire.
	Expires time.Duration
	
	// gc life time(s)
	GCLifetime int64
	
	// session life time(s)
	SessionLifetime int64
	
	// set whether to pass this bar cookie only through HTTPS
	Secure bool
	
	// sessionId is in url query
	SessionIdInURLQuery bool
	
	// sessionName in url query
	SessionNameInUrlQuery string
	
	// sessionId is in http header
	SessionIdInHttpHeader bool
	
	// sessionName in http header
	SessionNameInHttpHeader string
	
	// SessionIdGeneratorFunc should returns a random session id.
	SessionIdGeneratorFunc func() string
	
	// Encode the cookie value if not nil.
	EncodeFunc func(cookieValue string) (string, error)
	
	// Decode the cookie value if not nil.
	DecodeFunc func(cookieValue string) (string, error)
}

不同的 session 存储提供有着不同的配置,请查看存储名称目录下的 Config.go

文档

文档地址: http://godoc.org/github.com/phachon/fasthttpsession

示例

一些例子

反馈

  • 如果您喜欢该项目,请 Start.
  • 如果在使用过程中有任何问题, 请提交 Issue.
  • 如果您发现并解决了bug,请提交 Pull Request.
  • 如果您想扩展 session 存储,欢迎 Fork and merge this rep.
  • 如果你想交个朋友,欢迎发邮件给 phachon@163.com.

License

MIT

Thanks

Create By phachon@163.com