Skip to content

Commit

Permalink
optimize session module
Browse files Browse the repository at this point in the history
  • Loading branch information
kkumar-gcc committed Sep 12, 2024
1 parent 57cbbc2 commit 26ebab4
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 21 deletions.
6 changes: 6 additions & 0 deletions contracts/session/session.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package session

import "github.com/goravel/framework/contracts/foundation"

Check failure on line 3 in contracts/session/session.go

View workflow job for this annotation

GitHub Actions / lint / lint

could not import github.com/goravel/framework/contracts/foundation (-: import cycle not allowed: import stack: [github.com/goravel/framework/auth github.com/goravel/framework/contracts/foundation github.com/goravel/framework/contracts/http github.com/goravel/framework/contracts/session github.com/goravel/framework/contracts/foundation]) (typecheck)

Check failure on line 3 in contracts/session/session.go

View workflow job for this annotation

GitHub Actions / lint / nilaway

could not import github.com/goravel/framework/contracts/foundation (import cycle: [github.com/goravel/framework/contracts/foundation [github.com/goravel/framework/contracts/foundation.test] github.com/goravel/framework/contracts/http [github.com/goravel/framework/contracts/foundation.test] github.com/goravel/framework/contracts/session [github.com/goravel/framework/contracts/foundation.test]])

// Session is the interface that defines the methods that should be implemented by a session.
type Session interface {
// All returns all attributes of the session.
Expand Down Expand Up @@ -42,8 +44,12 @@ type Session interface {
Remove(key string) any
// Save saves the session.
Save() error
// SetDriver sets the session driver
SetDriver(driver Driver) Session
// SetID sets the ID of the session.
SetID(id string) Session
// SetJson sets the JSON parser for the session, which is used to marshal and unmarshal data.
SetJson(json foundation.Json) Session
// SetName sets the name of the session.
SetName(name string) Session
// Start initiates the session.
Expand Down
7 changes: 7 additions & 0 deletions session/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package session

import "errors"

var (
ErrDriverNotSet = errors.New("session driver is not set")
)
20 changes: 9 additions & 11 deletions session/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ func NewManager(config config.Config, json foundation.Json) *Manager {
drivers: make(map[string]sessioncontract.Driver),
json: json,
sessionPool: sync.Pool{New: func() any {
return &Session{
attributes: make(map[string]any),
}
return NewSession("", nil, json)
},
},
}
Expand All @@ -40,9 +38,10 @@ func (m *Manager) BuildSession(handler sessioncontract.Driver, sessionID ...stri
panic("session driver cannot be nil")
}
session := m.acquireSession()
session.setDriver(handler)
session.setJson(m.json)
session.SetName(m.config.GetString("session.cookie"))
session.SetDriver(handler).
SetJson(m.json).
SetName(m.config.GetString("session.cookie"))

if len(sessionID) > 0 {
session.SetID(sessionID[0])
} else {
Expand Down Expand Up @@ -81,13 +80,12 @@ func (m *Manager) Extend(driver string, handler func() sessioncontract.Driver) e
}

func (m *Manager) ReleaseSession(session sessioncontract.Session) {
s := session.(*Session)
s.reset()
m.sessionPool.Put(s)
session.Flush()
m.sessionPool.Put(session)
}

func (m *Manager) acquireSession() *Session {
session := m.sessionPool.Get().(*Session)
func (m *Manager) acquireSession() sessioncontract.Session {
session := m.sessionPool.Get().(sessioncontract.Session)
return session
}

Expand Down
44 changes: 34 additions & 10 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/goravel/framework/contracts/foundation"
sessioncontract "github.com/goravel/framework/contracts/session"
"github.com/goravel/framework/support/color"
supportmaps "github.com/goravel/framework/support/maps"
"github.com/goravel/framework/support/str"
)
Expand Down Expand Up @@ -155,6 +156,10 @@ func (s *Session) Save() error {
return err
}

if err = s.validateDriver(); err != nil {
return err
}

if err = s.driver.Write(s.GetID(), string(data)); err != nil {
return err
}
Expand All @@ -164,6 +169,11 @@ func (s *Session) Save() error {
return nil
}

func (s *Session) SetDriver(driver sessioncontract.Driver) sessioncontract.Session {
s.driver = driver
return s
}

func (s *Session) SetID(id string) sessioncontract.Session {
if s.isValidID(id) {
s.id = id
Expand All @@ -174,6 +184,11 @@ func (s *Session) SetID(id string) sessioncontract.Session {
return s
}

func (s *Session) SetJson(json foundation.Json) sessioncontract.Session {
s.json = json
return s
}

func (s *Session) SetName(name string) sessioncontract.Session {
s.name = name

Expand Down Expand Up @@ -210,15 +225,25 @@ func (s *Session) loadSession() {
}
}

func (s *Session) validateDriver() error {
if s.driver == nil {
return ErrDriverNotSet
}
return nil
}

func (s *Session) migrate(destroy ...bool) error {
shouldDestroy := false
if len(destroy) > 0 {
shouldDestroy = destroy[0]
}

if shouldDestroy {
err := s.driver.Destroy(s.GetID())
if err != nil {
if err := s.validateDriver(); err != nil {
return err
}

if err := s.driver.Destroy(s.GetID()); err != nil {
return err
}
}
Expand All @@ -229,12 +254,19 @@ func (s *Session) migrate(destroy ...bool) error {
}

func (s *Session) readFromHandler() map[string]any {
if err := s.validateDriver(); err != nil {
color.Red().Println(err)
return nil
}

value, err := s.driver.Read(s.GetID())
if err != nil {
color.Red().Println(err)
return nil
}
var data map[string]any
if err := s.json.Unmarshal([]byte(value), &data); err != nil {
color.Red().Println(err)
return nil
}
return data
Expand Down Expand Up @@ -280,14 +312,6 @@ func (s *Session) reset() {
s.started = false
}

func (s *Session) setDriver(driver sessioncontract.Driver) {
s.driver = driver
}

func (s *Session) setJson(json foundation.Json) {
s.json = json
}

// toStringSlice converts an interface slice to a string slice.
func toStringSlice(anySlice []any) []string {
strSlice := make([]string, len(anySlice))
Expand Down

0 comments on commit 26ebab4

Please sign in to comment.