sessions

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2021 License: Apache-2.0 Imports: 11 Imported by: 0

README

sessions

go gin版本的session存储,支持存取多个cookie,内存版和redis版本

使用

下载

$ go get github.com/qm012/sessions

导入

import "github.com/qm012/sessions"

示例

示例项目 session-demo

redis 版本
import (
	"github.com/gin-gonic/gin"
	"github.com/qm012/sessions"
)

var (
	UsernameStr = "username"
	PasswordStr = "password"
	IsLoginStr  = "isLogin"
)
r := gin.Default()
sessions.SetCookie(sessions.ValueMap, "session_id", age, "/", "127.0.0.1", false, false)
sessions.SetCookie(sessions.ValueString, "password", age, "/", "127.0.0.1", false, false)
r.Use(sessions.Sessions(sessions.ChooseSessionStore(sessions.Redis, rdbClient)))

r.GET("/login",func(c *gin.Context){
    sessions.GetSession(ctx, "password").Set(user.Password)
    sessions.GetSession(ctx, "session_id").Set(true, IsLoginStr)
    sessions.GetSession(ctx, "session_id").Set(user.Username, UsernameStr)
    sessions.GetSession(ctx, "session_id").Set(user.Password, PasswordStr)
})
r.GET("/home",func(c *gin.Context){
    username := sessions.GetSession(ctx, "session_id").Get(UsernameStr)
	password := sessions.GetSession(ctx, "password").Get()
	login := sessions.GetSession(ctx, "session_id").Get(IsLoginStr)
})
内存版本


Documentation

Index

Constants

View Source
const (
	Redis  storeType = "redis"
	Memory storeType = "memory"

	ValueMap valueType = iota
	ValueString
)

Variables

This section is empty.

Functions

func Sessions

func Sessions(mgr SessionMgr) gin.HandlerFunc

Sessions middleware of cookies

func SetCookie

func SetCookie(valueType valueType, name string, maxAge int, path, domain string, secure, httpOnly bool)

SetCookie setting cookie

Types

type Session

type Session interface {
	Get(keys ...string) interface{}
	Set(value interface{}, keys ...string)
	Delete(keys ...string)
}

Session single cookie value object

func GetSession

func GetSession(ctx *gin.Context, name string) Session

GetSession Session Object If you use a custom plaintext cookie, use c.Cookie(name) or http.Request.Cookie(name) @param ctx gin.context @param name is cookie name

type SessionMgr

type SessionMgr interface {
	GetSession(cookValue string) Session
	CreateSession(cookValue string, valueType valueType, expire int) Session
}

SessionMgr object manage

func ChooseSessionStore

func ChooseSessionStore(st storeType, clients ...*redis.Client) (mgr SessionMgr)

Jump to

Keyboard shortcuts

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