gession

package module
v1.0.1-0...-5ec581b Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2021 License: MIT Imports: 7 Imported by: 0

README

Gession会话框架

Gitee link address Github link address Go Version Release Version

  1. Gession是一个会话框架。
  2. 可用于HTTP/HTTPS网络连接的会话保持。
  3. 内置对接Redis接口,连接保存Session到Redis。
  4. 操作极其方便,易上手,源码易懂有注释。

安装

$ go get -u github.com/wangluozhe/gession

依赖

Redigo v1.8.5 go.uuid v1.2.0

导入

import(
    "github.com/wangluozhe/gession/session"
)

初始化Session管理器

Session管理器用于管理Session的各种操作,如:New/Get/Del。 Session管理器变量名:session.Ssmgr

Redis方式:

host := "host"				    // Redis地址
port := 6379				    // Redis端口
password := "password"		    // Redis密码
database := 1				    // Redis库
pool := session.NewRedisPool(host, int(port), password, int(database))		// 创建Redis连接池
expire := 1000				    // Session过期时间
session.Init(pool, expire)	    // 初始化Session管理器
Ssmgr := session.Ssmgr		    // 全局可用,可以直接操作session.Ssmgr也可以赋值后在操作,Ssmgr是Session组管理器,用来创建/读取/删除Session

内存方式:

session.Init()                  // 初始化Session管理器
Ssmgr := session.Ssmgr		    // 全局可用,可以直接操作session.Ssmgr也可以赋值后在操作,Ssmgr是Session组管理器,用来创建/读取/删除Session
创建Session
ss := Ssmgr.New()			    // 创建Session
读取Session
ss,err := Ssmgr.Get(sessionId)  // 读取Session,如果内存中没有此Session会从Redis中读取,都没有返回nil
if err != nil{
    fmt.Println(err)
}
删除Session
ss,err := Ssmgr.Del(sessionId)  // 删除Session,内存和Redis中的此Session都会被删除
if err != nil{
    fmt.Println(err)
}

Session操作

Session操作只有四种操作,简单方便,存储的是map类型,有:Set/Get/Del/Save。

设置Session值
ss.Set(key,value)               // 设置Session中的值
// 或
isSuccess := ss.Set(key,value)  // 返回一个是否设置成功的bool值
获取Session值
result := ss.Get(key)           // 获取Session中的值
删除Session值
ss.Del(key)                     // 删除Session中的值
// 或
isSuccess := ss.Del(key)        // 返回一个是否删除成功的bool值
保存Session到Redis中
isSuccess,err := ss.Save()      // 保存此Session到Redis中
if err != nil{
    fmt.Println(err)
}

Documentation

Index

Constants

View Source
const (
	SessionFlagNone   = iota // 无数据状态
	SessionFlagModify        // 有数据状态
)

Variables

This section is empty.

Functions

func Init

func Init(options ...interface{})

func NewRedisPool

func NewRedisPool(host string, port int, password string, database int) *redis.Pool

创建Redis连接池

Types

type RedisSession

type RedisSession struct {
	SessionId string

	Expire     int
	SessionMap map[string]interface{}

	Flag int
	// contains filtered or unexported fields
}

RedisSession结构体

func (*RedisSession) Del

func (this *RedisSession) Del(key string) bool

删除Session内容

func (*RedisSession) Get

func (this *RedisSession) Get(key string) interface{}

获取Session内容

func (*RedisSession) Save

func (this *RedisSession) Save(expire ...int) (bool, error)

保存Session内容

func (*RedisSession) Set

func (this *RedisSession) Set(key string, value interface{}) bool

设置Session内容

type RedisSessionMgr

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

Session管理器结构体

var Ssmgr *RedisSessionMgr

func InitSessionMgr

func InitSessionMgr(options ...interface{}) *RedisSessionMgr

初始化Session管理器

func (*RedisSessionMgr) Del

func (this *RedisSessionMgr) Del(sessionId string) (bool, error)

从Redis中删除Session

func (*RedisSessionMgr) Get

func (this *RedisSessionMgr) Get(sessionId string) (*RedisSession, error)

读取Session

func (*RedisSessionMgr) New

func (this *RedisSessionMgr) New(options ...string) *RedisSession

新建Session

Jump to

Keyboard shortcuts

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