Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: support store deploy scenes with read and write splited #103

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions rw/rw.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package rw

import (
"github.com/gin-contrib/sessions"
gsessions "github.com/gorilla/sessions"
"net/http"
)

type Store interface {
sessions.Store
}

// NewStore create a session store split read and write channel
func NewStore(read, write sessions.Store) Store {
return &store{read, write}
}

type store struct {
read sessions.Store
write sessions.Store
}

// Get should return a cached session.
func (c *store) Get(r *http.Request, name string) (*gsessions.Session, error) {
return c.read.Get(r, name)
}

// New should create and return a new session.
//
// Note that New should never return a nil session, even in the case of
// an error if using the Registry infrastructure to cache the session.
func (c *store) New(r *http.Request, name string) (*gsessions.Session, error) {
return c.write.New(r, name)
}

// Save should persist session to the underlying store implementation.
func (c *store) Save(r *http.Request, w http.ResponseWriter, s *gsessions.Session) error {
return c.write.Save(r, w, s)
}

func (c *store) Options(options sessions.Options) {
c.read.Options(options)
c.write.Options(options)
}
44 changes: 44 additions & 0 deletions rw/rw_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package rw

import (
"testing"

"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/redis"
"github.com/gin-contrib/sessions/tester"
)

const redisTestServer = "localhost:6379"

var newRWStore = func(_ *testing.T) sessions.Store {
storeRead, err1 := redis.NewStore(10, "tcp", redisTestServer, "", []byte("secret"))
if err1 != nil {
panic(err1)
}
storeWrite, err2 := redis.NewStore(10, "tcp", redisTestServer, "", []byte("secret"))
if err2 != nil {
panic(err2)
}

return NewStore(storeRead, storeWrite)
}

func TestRW_SessionGetSet(t *testing.T) {
tester.GetSet(t, newRWStore)
}

func TestRW_SessionDeleteKey(t *testing.T) {
tester.DeleteKey(t, newRWStore)
}

func TestRW_SessionFlashes(t *testing.T) {
tester.Flashes(t, newRWStore)
}

func TestRW_SessionClear(t *testing.T) {
tester.Clear(t, newRWStore)
}

func TestRW_SessionOptions(t *testing.T) {
tester.Options(t, newRWStore)
}