github.com/gogf/gf/v2@v2.7.4/os/gsession/gsession_manager.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gsession 8 9 import ( 10 "context" 11 "time" 12 ) 13 14 // Manager for sessions. 15 type Manager struct { 16 ttl time.Duration // TTL for sessions. 17 storage Storage // Storage interface for session storage. 18 } 19 20 // New creates and returns a new session manager. 21 func New(ttl time.Duration, storage ...Storage) *Manager { 22 m := &Manager{ 23 ttl: ttl, 24 } 25 if len(storage) > 0 && storage[0] != nil { 26 m.storage = storage[0] 27 } else { 28 // It uses StorageFile in default. 29 m.storage = NewStorageFile(DefaultStorageFilePath, ttl) 30 } 31 return m 32 } 33 34 // New creates or fetches the session for given session id. 35 // The parameter `sessionId` is optional, it creates a new one if not it's passed 36 // depending on Storage.New. 37 func (m *Manager) New(ctx context.Context, sessionId ...string) *Session { 38 var id string 39 if len(sessionId) > 0 && sessionId[0] != "" { 40 id = sessionId[0] 41 } 42 return &Session{ 43 id: id, 44 ctx: ctx, 45 manager: m, 46 } 47 } 48 49 // SetStorage sets the session storage for manager. 50 func (m *Manager) SetStorage(storage Storage) { 51 m.storage = storage 52 } 53 54 // GetStorage returns the session storage of current manager. 55 func (m *Manager) GetStorage() Storage { 56 return m.storage 57 } 58 59 // SetTTL the TTL for the session manager. 60 func (m *Manager) SetTTL(ttl time.Duration) { 61 m.ttl = ttl 62 } 63 64 // GetTTL returns the TTL of the session manager. 65 func (m *Manager) GetTTL() time.Duration { 66 return m.ttl 67 }