1
0
mirror of https://github.com/astaxie/beego.git synced 2025-06-12 07:20:38 +00:00

Adapter: session module

This commit is contained in:
Ming Deng
2020-09-03 23:36:09 +08:00
parent 3530457ff9
commit 8ef9965eef
23 changed files with 2395 additions and 4 deletions

View File

@ -0,0 +1,121 @@
// Copyright 2014 beego Author. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package redis for session provider
//
// depend on github.com/go-redis/redis
//
// go install github.com/go-redis/redis
//
// Usage:
// import(
// _ "github.com/astaxie/beego/session/redis_sentinel"
// "github.com/astaxie/beego/session"
// )
//
// func init() {
// globalSessions, _ = session.NewManager("redis_sentinel", ``{"cookieName":"gosessionid","gclifetime":3600,"ProviderConfig":"127.0.0.1:26379;127.0.0.2:26379"}``)
// go globalSessions.GC()
// }
//
// more detail about params: please check the notes on the function SessionInit in this package
package redis_sentinel
import (
"context"
"net/http"
"github.com/astaxie/beego/pkg/adapter/session"
sentinel "github.com/astaxie/beego/pkg/infrastructure/session/redis_sentinel"
)
// DefaultPoolSize redis_sentinel default pool size
var DefaultPoolSize = sentinel.DefaultPoolSize
// SessionStore redis_sentinel session store
type SessionStore sentinel.SessionStore
// Set value in redis_sentinel session
func (rs *SessionStore) Set(key, value interface{}) error {
return (*sentinel.SessionStore)(rs).Set(context.Background(), key, value)
}
// Get value in redis_sentinel session
func (rs *SessionStore) Get(key interface{}) interface{} {
return (*sentinel.SessionStore)(rs).Get(context.Background(), key)
}
// Delete value in redis_sentinel session
func (rs *SessionStore) Delete(key interface{}) error {
return (*sentinel.SessionStore)(rs).Delete(context.Background(), key)
}
// Flush clear all values in redis_sentinel session
func (rs *SessionStore) Flush() error {
return (*sentinel.SessionStore)(rs).Flush(context.Background())
}
// SessionID get redis_sentinel session id
func (rs *SessionStore) SessionID() string {
return (*sentinel.SessionStore)(rs).SessionID(context.Background())
}
// SessionRelease save session values to redis_sentinel
func (rs *SessionStore) SessionRelease(w http.ResponseWriter) {
(*sentinel.SessionStore)(rs).SessionRelease(context.Background(), w)
}
// Provider redis_sentinel session provider
type Provider sentinel.Provider
// SessionInit init redis_sentinel session
// savepath like redis sentinel addr,pool size,password,dbnum,masterName
// e.g. 127.0.0.1:26379;127.0.0.2:26379,100,1qaz2wsx,0,mymaster
func (rp *Provider) SessionInit(maxlifetime int64, savePath string) error {
return (*sentinel.Provider)(rp).SessionInit(context.Background(), maxlifetime, savePath)
}
// SessionRead read redis_sentinel session by sid
func (rp *Provider) SessionRead(sid string) (session.Store, error) {
s, err := (*sentinel.Provider)(rp).SessionRead(context.Background(), sid)
return session.CreateNewToOldStoreAdapter(s), err
}
// SessionExist check redis_sentinel session exist by sid
func (rp *Provider) SessionExist(sid string) bool {
res, _ := (*sentinel.Provider)(rp).SessionExist(context.Background(), sid)
return res
}
// SessionRegenerate generate new sid for redis_sentinel session
func (rp *Provider) SessionRegenerate(oldsid, sid string) (session.Store, error) {
s, err := (*sentinel.Provider)(rp).SessionRegenerate(context.Background(), oldsid, sid)
return session.CreateNewToOldStoreAdapter(s), err
}
// SessionDestroy delete redis session by id
func (rp *Provider) SessionDestroy(sid string) error {
return (*sentinel.Provider)(rp).SessionDestroy(context.Background(), sid)
}
// SessionGC Impelment method, no used.
func (rp *Provider) SessionGC() {
(*sentinel.Provider)(rp).SessionGC(context.Background())
}
// SessionAll return all activeSession
func (rp *Provider) SessionAll() int {
return (*sentinel.Provider)(rp).SessionAll(context.Background())
}

View File

@ -0,0 +1,90 @@
package redis_sentinel
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/astaxie/beego/pkg/adapter/session"
)
func TestRedisSentinel(t *testing.T) {
sessionConfig := &session.ManagerConfig{
CookieName: "gosessionid",
EnableSetCookie: true,
Gclifetime: 3600,
Maxlifetime: 3600,
Secure: false,
CookieLifeTime: 3600,
ProviderConfig: "127.0.0.1:6379,100,,0,master",
}
globalSessions, e := session.NewManager("redis_sentinel", sessionConfig)
if e != nil {
t.Log(e)
return
}
// todo test if e==nil
go globalSessions.GC()
r, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
sess, err := globalSessions.SessionStart(w, r)
if err != nil {
t.Fatal("session start failed:", err)
}
defer sess.SessionRelease(w)
// SET AND GET
err = sess.Set("username", "astaxie")
if err != nil {
t.Fatal("set username failed:", err)
}
username := sess.Get("username")
if username != "astaxie" {
t.Fatal("get username failed")
}
// DELETE
err = sess.Delete("username")
if err != nil {
t.Fatal("delete username failed:", err)
}
username = sess.Get("username")
if username != nil {
t.Fatal("delete username failed")
}
// FLUSH
err = sess.Set("username", "astaxie")
if err != nil {
t.Fatal("set failed:", err)
}
err = sess.Set("password", "1qaz2wsx")
if err != nil {
t.Fatal("set failed:", err)
}
username = sess.Get("username")
if username != "astaxie" {
t.Fatal("get username failed")
}
password := sess.Get("password")
if password != "1qaz2wsx" {
t.Fatal("get password failed")
}
err = sess.Flush()
if err != nil {
t.Fatal("flush failed:", err)
}
username = sess.Get("username")
if username != nil {
t.Fatal("flush failed")
}
password = sess.Get("password")
if password != nil {
t.Fatal("flush failed")
}
sess.SessionRelease(w)
}