Documentation ¶
Overview ¶
Package gsession implements manager and storage features for sessions.
Index ¶
- Constants
- Variables
- func NewSessionId() string
- type Manager
- type Session
- func (s *Session) Close() error
- func (s *Session) Contains(key string) (ok bool, err error)
- func (s *Session) Data() (sessionData map[string]interface{}, err error)
- func (s *Session) Get(key string, def ...interface{}) (value *gvar.Var, err error)
- func (s *Session) Id() (id string, err error)
- func (s *Session) IsDirty() bool
- func (s *Session) MustContains(key string) bool
- func (s *Session) MustData() map[string]interface{}
- func (s *Session) MustGet(key string, def ...interface{}) *gvar.Var
- func (s *Session) MustId() string
- func (s *Session) MustRemove(keys ...string)
- func (s *Session) MustSet(key string, value interface{})
- func (s *Session) MustSetMap(data map[string]interface{})
- func (s *Session) MustSize() int
- func (s *Session) Remove(keys ...string) (err error)
- func (s *Session) RemoveAll() (err error)
- func (s *Session) Set(key string, value interface{}) (err error)
- func (s *Session) SetId(id string) error
- func (s *Session) SetIdFunc(f func(ttl time.Duration) string) error
- func (s *Session) SetMap(data map[string]interface{}) (err error)
- func (s *Session) Size() (size int, err error)
- type Storage
- type StorageBase
- func (s *StorageBase) Data(ctx context.Context, sessionId string) (sessionData map[string]interface{}, err error)
- func (s *StorageBase) Get(ctx context.Context, sessionId string, key string) (value interface{}, err error)
- func (s *StorageBase) GetSession(ctx context.Context, sessionId string, ttl time.Duration) (*gmap.StrAnyMap, error)
- func (s *StorageBase) GetSize(ctx context.Context, sessionId string) (size int, err error)
- func (s *StorageBase) New(ctx context.Context, ttl time.Duration) (id string, err error)
- func (s *StorageBase) Remove(ctx context.Context, sessionId string, key string) error
- func (s *StorageBase) RemoveAll(ctx context.Context, sessionId string) error
- func (s *StorageBase) Set(ctx context.Context, sessionId string, key string, value interface{}, ...) error
- func (s *StorageBase) SetMap(ctx context.Context, sessionId string, mapData map[string]interface{}, ...) error
- func (s *StorageBase) SetSession(ctx context.Context, sessionId string, sessionData *gmap.StrAnyMap, ...) error
- func (s *StorageBase) UpdateTTL(ctx context.Context, sessionId string, ttl time.Duration) error
- type StorageFile
- func (s *StorageFile) GetSession(ctx context.Context, sessionId string, ttl time.Duration) (sessionData *gmap.StrAnyMap, err error)
- func (s *StorageFile) RemoveAll(ctx context.Context, sessionId string) error
- func (s *StorageFile) SetCryptoEnabled(enabled bool)
- func (s *StorageFile) SetCryptoKey(key []byte)
- func (s *StorageFile) SetSession(ctx context.Context, sessionId string, sessionData *gmap.StrAnyMap, ...) error
- func (s *StorageFile) UpdateTTL(ctx context.Context, sessionId string, ttl time.Duration) error
- type StorageMemory
- func (s *StorageMemory) GetSession(ctx context.Context, sessionId string, ttl time.Duration) (*gmap.StrAnyMap, error)
- func (s *StorageMemory) RemoveAll(ctx context.Context, sessionId string) error
- func (s *StorageMemory) SetSession(ctx context.Context, sessionId string, sessionData *gmap.StrAnyMap, ...) error
- func (s *StorageMemory) UpdateTTL(ctx context.Context, sessionId string, ttl time.Duration) error
- type StorageRedis
- func (s *StorageRedis) GetSession(ctx context.Context, sessionId string, ttl time.Duration) (*gmap.StrAnyMap, error)
- func (s *StorageRedis) RemoveAll(ctx context.Context, sessionId string) error
- func (s *StorageRedis) SetSession(ctx context.Context, sessionId string, sessionData *gmap.StrAnyMap, ...) error
- func (s *StorageRedis) UpdateTTL(ctx context.Context, sessionId string, ttl time.Duration) error
- type StorageRedisHashTable
- func (s *StorageRedisHashTable) Data(ctx context.Context, sessionId string) (data map[string]interface{}, err error)
- func (s *StorageRedisHashTable) Get(ctx context.Context, sessionId string, key string) (value interface{}, err error)
- func (s *StorageRedisHashTable) GetSession(ctx context.Context, sessionId string, ttl time.Duration) (*gmap.StrAnyMap, error)
- func (s *StorageRedisHashTable) GetSize(ctx context.Context, sessionId string) (size int, err error)
- func (s *StorageRedisHashTable) Remove(ctx context.Context, sessionId string, key string) error
- func (s *StorageRedisHashTable) RemoveAll(ctx context.Context, sessionId string) error
- func (s *StorageRedisHashTable) Set(ctx context.Context, sessionId string, key string, value interface{}, ...) error
- func (s *StorageRedisHashTable) SetMap(ctx context.Context, sessionId string, data map[string]interface{}, ...) error
- func (s *StorageRedisHashTable) SetSession(ctx context.Context, sessionId string, sessionData *gmap.StrAnyMap, ...) error
- func (s *StorageRedisHashTable) UpdateTTL(ctx context.Context, sessionId string, ttl time.Duration) error
Examples ¶
- Manager.GetStorage
- Manager.SetStorage
- Manager.SetTTL
- New
- Session.Contains
- Session.Data
- Session.Id
- Session.Remove
- Session.RemoveAll
- Session.Set
- Session.SetId
- Session.SetIdFunc
- Session.SetMap
- Session.Size
- StorageFile.SetCryptoEnabled
- StorageFile.SetCryptoKey
- StorageFile.UpdateTTL
- StorageRedis.RemoveAll
- StorageRedis.UpdateTTL
- StorageRedisHashTable.Data
- StorageRedisHashTable.Get
- StorageRedisHashTable.GetSession
- StorageRedisHashTable.GetSize
- StorageRedisHashTable.Remove
- StorageRedisHashTable.RemoveAll
- StorageRedisHashTable.SetSession
- StorageRedisHashTable.UpdateTTL
Constants ¶
const ( DefaultStorageFileCryptoEnabled = false DefaultStorageFileUpdateTTLInterval = 10 * time.Second DefaultStorageFileClearExpiredInterval = time.Hour )
const ( // DefaultStorageRedisLoopInterval is the interval updating TTL for session ids // in last duration. DefaultStorageRedisLoopInterval = 10 * time.Second )
Variables ¶
var ( DefaultStorageFilePath = gfile.Temp("gsessions") DefaultStorageFileCryptoKey = []byte("Session storage file crypto key!") )
var ( // ErrorDisabled is used for marking certain interface function not used. ErrorDisabled = gerror.NewWithOption(gerror.Option{ Text: "this feature is disabled in this storage", Code: gcode.CodeNotSupported, }) )
Functions ¶
func NewSessionId ¶
func NewSessionId() string
NewSessionId creates and returns a new and unique session id string, which is in 32 bytes.
Types ¶
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager for sessions.
func New ¶
New creates and returns a new session manager.
Example ¶
package main import ( "fmt" "time" "github.com/gogf/gf/v2/os/gsession" ) func main() { manager := gsession.New(time.Second) fmt.Println(manager.GetTTL()) }
Output: 1s
func (*Manager) GetStorage ¶
GetStorage returns the session storage of current manager.
Example ¶
package main import ( "fmt" "time" "github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/gsession" ) func main() { manager := gsession.New(time.Second, gsession.NewStorageMemory()) size, _ := manager.GetStorage().GetSize(gctx.New(), "id") fmt.Println(size) }
Output: 0
func (*Manager) New ¶
New creates or fetches the session for given session id. The parameter `sessionId` is optional, it creates a new one if not it's passed depending on Storage.New.
func (*Manager) SetStorage ¶
SetStorage sets the session storage for manager.
Example ¶
package main import ( "fmt" "time" "github.com/gogf/gf/v2/os/gsession" ) func main() { manager := gsession.New(time.Second) manager.SetStorage(gsession.NewStorageMemory()) fmt.Println(manager.GetTTL()) }
Output: 1s
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session struct for storing single session data, which is bound to a single request. The Session struct is the interface with user, but the Storage is the underlying adapter designed interface for functionality implements.
func (*Session) Close ¶
Close closes current session and updates its ttl in the session manager. If this session is dirty, it also exports it to storage.
NOTE that this function must be called ever after a session request done.
func (*Session) Contains ¶
Contains checks whether key exist in the session.
Example ¶
package main import ( "fmt" "time" "github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/gsession" ) func main() { storage := gsession.NewStorageFile("", time.Second) manager := gsession.New(time.Second, storage) s1 := manager.New(gctx.New()) notContains, _ := s1.Contains("Contains") fmt.Println(notContains) s2 := manager.New(gctx.New(), "Contains") contains, _ := s2.Contains("Contains") fmt.Println(contains) }
Output: false false
func (*Session) Data ¶
Data returns all data as map. Note that it's using value copy internally for concurrent-safe purpose.
Example ¶
package main import ( "fmt" "time" "github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/gsession" ) func main() { storage := gsession.NewStorageFile("", time.Second) manager := gsession.New(time.Second, storage) s1 := manager.New(gctx.New()) data1, _ := s1.Data() fmt.Println(data1) s2 := manager.New(gctx.New(), "id_data") data2, _ := s2.Data() fmt.Println(data2) }
Output: map[] map[]
func (*Session) Get ¶
Get retrieves session value with given key. It returns `def` if the key does not exist in the session if `def` is given, or else it returns nil.
func (*Session) Id ¶
Id returns the session id for this session. It creates and returns a new session id if the session id is not passed in initialization.
Example ¶
package main import ( "fmt" "time" "github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/gsession" ) func main() { storage := gsession.NewStorageFile("", time.Second) manager := gsession.New(time.Second, storage) s := manager.New(gctx.New(), "Id") id, _ := s.Id() fmt.Println(id) }
Output: Id
func (*Session) MustContains ¶
MustContains performs as function Contains, but it panics if any error occurs.
func (*Session) MustRemove ¶
MustRemove performs as function Remove, but it panics if any error occurs.
func (*Session) MustSetMap ¶
MustSetMap performs as function SetMap, but it panics if any error occurs.
func (*Session) Remove ¶
Remove removes key along with its value from this session.
Example ¶
package main import ( "fmt" "time" "github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/gsession" ) func main() { storage := gsession.NewStorageFile("", time.Second) manager := gsession.New(time.Second, storage) s1 := manager.New(gctx.New()) fmt.Println(s1.Remove("key")) s2 := manager.New(gctx.New(), "Remove") fmt.Println(s2.Remove("key")) }
Output: <nil> <nil>
func (*Session) RemoveAll ¶
RemoveAll deletes all key-value pairs from this session.
Example ¶
package main import ( "fmt" "time" "github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/gsession" ) func main() { storage := gsession.NewStorageFile("", time.Second) manager := gsession.New(time.Second, storage) s1 := manager.New(gctx.New()) fmt.Println(s1.RemoveAll()) s2 := manager.New(gctx.New(), "Remove") fmt.Println(s2.RemoveAll()) }
Output: <nil> <nil>
func (*Session) Set ¶
Set sets key-value pair to this session.
Example ¶
package main import ( "fmt" "time" "github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/gsession" ) func main() { storage := gsession.NewStorageFile("", time.Second) manager := gsession.New(time.Second, storage) s := manager.New(gctx.New()) fmt.Println(s.Set("key", "val") == nil) }
Output: true
func (*Session) SetId ¶
SetId sets custom session before session starts. It returns error if it is called after session starts.
Example ¶
package main import ( "fmt" "time" "github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/gsession" ) func main() { nilSession := &gsession.Session{} fmt.Println(nilSession.SetId("id")) storage := gsession.NewStorageFile("", time.Second) manager := gsession.New(time.Second, storage) s := manager.New(gctx.New()) s.Id() fmt.Println(s.SetId("id")) }
Output: <nil> session already started
func (*Session) SetIdFunc ¶
SetIdFunc sets custom session id creating function before session starts. It returns error if it is called after session starts.
Example ¶
package main import ( "fmt" "time" "github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/gsession" ) func main() { nilSession := &gsession.Session{} fmt.Println(nilSession.SetIdFunc(func(ttl time.Duration) string { return "id" })) storage := gsession.NewStorageFile("", time.Second) manager := gsession.New(time.Second, storage) s := manager.New(gctx.New()) s.Id() fmt.Println(s.SetIdFunc(func(ttl time.Duration) string { return "id" })) }
Output: <nil> session already started
func (*Session) SetMap ¶
SetMap batch sets the session using map.
Example ¶
package main import ( "fmt" "time" "github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/gsession" ) func main() { storage := gsession.NewStorageFile("", time.Second) manager := gsession.New(time.Second, storage) s := manager.New(gctx.New()) fmt.Println(s.SetMap(map[string]interface{}{}) == nil) }
Output: true
func (*Session) Size ¶
Size returns the size of the session.
Example ¶
package main import ( "fmt" "time" "github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/gsession" ) func main() { storage := gsession.NewStorageFile("", time.Second) manager := gsession.New(time.Second, storage) s1 := manager.New(gctx.New()) size1, _ := s1.Size() fmt.Println(size1) s2 := manager.New(gctx.New(), "Size") size2, _ := s2.Size() fmt.Println(size2) }
Output: 0 0
type Storage ¶
type Storage interface { // New creates a custom session id. // This function can be used for custom session creation. New(ctx context.Context, ttl time.Duration) (sessionId string, err error) // Get retrieves and returns certain session value with given key. // It returns nil if the key does not exist in the session. Get(ctx context.Context, sessionId string, key string) (value interface{}, err error) // GetSize retrieves and returns the size of key-value pairs from storage. GetSize(ctx context.Context, sessionId string) (size int, err error) // Data retrieves all key-value pairs as map from storage. Data(ctx context.Context, sessionId string) (sessionData map[string]interface{}, err error) // Set sets one key-value session pair to the storage. // The parameter `ttl` specifies the TTL for the session id. Set(ctx context.Context, sessionId string, key string, value interface{}, ttl time.Duration) error // SetMap batch sets key-value session pairs as map to the storage. // The parameter `ttl` specifies the TTL for the session id. SetMap(ctx context.Context, sessionId string, mapData map[string]interface{}, ttl time.Duration) error // Remove deletes key-value pair from specified session from storage. Remove(ctx context.Context, sessionId string, key string) error // RemoveAll deletes session from storage. RemoveAll(ctx context.Context, sessionId string) error // GetSession returns the session data as `*gmap.StrAnyMap` for given session from storage. // // The parameter `ttl` specifies the TTL for this session. // The parameter `data` is the current old session data stored in memory, // and for some storage it might be nil if memory storage is disabled. // // This function is called ever when session starts. // It returns nil if the session does not exist or its TTL is expired. GetSession(ctx context.Context, sessionId string, ttl time.Duration) (*gmap.StrAnyMap, error) // SetSession updates the data for specified session id. // This function is called ever after session, which is changed dirty, is closed. // This copy all session data map from memory to storage. SetSession(ctx context.Context, sessionId string, sessionData *gmap.StrAnyMap, ttl time.Duration) error // UpdateTTL updates the TTL for specified session id. // This function is called ever after session, which is not dirty, is closed. UpdateTTL(ctx context.Context, sessionId string, ttl time.Duration) error }
Storage is the interface definition for session storage.
type StorageBase ¶
type StorageBase struct{}
StorageBase is a base implement for Session Storage.
func (*StorageBase) Data ¶
func (s *StorageBase) Data(ctx context.Context, sessionId string) (sessionData map[string]interface{}, err error)
Data retrieves all key-value pairs as map from storage.
func (*StorageBase) Get ¶
func (s *StorageBase) Get(ctx context.Context, sessionId string, key string) (value interface{}, err error)
Get retrieves certain session value with given key. It returns nil if the key does not exist in the session.
func (*StorageBase) GetSession ¶
func (s *StorageBase) GetSession(ctx context.Context, sessionId string, ttl time.Duration) (*gmap.StrAnyMap, error)
GetSession returns the session data as *gmap.StrAnyMap for given session id from storage.
The parameter `ttl` specifies the TTL for this session, and it returns nil if the TTL is exceeded. The parameter `data` is the current old session data stored in memory, and for some storage it might be nil if memory storage is disabled.
This function is called ever when session starts.
func (*StorageBase) New ¶
New creates a session id. This function can be used for custom session creation.
func (*StorageBase) RemoveAll ¶
func (s *StorageBase) RemoveAll(ctx context.Context, sessionId string) error
RemoveAll deletes session from storage.
func (*StorageBase) Set ¶
func (s *StorageBase) Set(ctx context.Context, sessionId string, key string, value interface{}, ttl time.Duration) error
Set sets key-value session pair to the storage. The parameter `ttl` specifies the TTL for the session id (not for the key-value pair).
func (*StorageBase) SetMap ¶
func (s *StorageBase) SetMap(ctx context.Context, sessionId string, mapData map[string]interface{}, ttl time.Duration) error
SetMap batch sets key-value session pairs with map to the storage. The parameter `ttl` specifies the TTL for the session id(not for the key-value pair).
func (*StorageBase) SetSession ¶
func (s *StorageBase) SetSession(ctx context.Context, sessionId string, sessionData *gmap.StrAnyMap, ttl time.Duration) error
SetSession updates the data map for specified session id. This function is called ever after session, which is changed dirty, is closed. This copy all session data map from memory to storage.
type StorageFile ¶
type StorageFile struct { StorageBase // contains filtered or unexported fields }
StorageFile implements the Session Storage interface with file system.
func NewStorageFile ¶
func NewStorageFile(path string, ttl time.Duration) *StorageFile
NewStorageFile creates and returns a file storage object for session.
func (*StorageFile) GetSession ¶
func (s *StorageFile) GetSession(ctx context.Context, sessionId string, ttl time.Duration) (sessionData *gmap.StrAnyMap, err error)
GetSession returns the session data as *gmap.StrAnyMap for given session id from storage.
The parameter `ttl` specifies the TTL for this session, and it returns nil if the TTL is exceeded. The parameter `data` is the current old session data stored in memory, and for some storage it might be nil if memory storage is disabled.
This function is called ever when session starts.
func (*StorageFile) RemoveAll ¶
func (s *StorageFile) RemoveAll(ctx context.Context, sessionId string) error
RemoveAll deletes all key-value pairs from storage.
func (*StorageFile) SetCryptoEnabled ¶
func (s *StorageFile) SetCryptoEnabled(enabled bool)
SetCryptoEnabled enables/disables the crypto feature for session storage.
Example ¶
package main import ( "fmt" "time" "github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/gsession" ) func main() { storage := gsession.NewStorageFile("", time.Second) storage.SetCryptoEnabled(true) size, _ := storage.GetSize(gctx.New(), "id") fmt.Println(size) }
Output: 0
func (*StorageFile) SetCryptoKey ¶
func (s *StorageFile) SetCryptoKey(key []byte)
SetCryptoKey sets the crypto key for session storage. The crypto key is used when crypto feature is enabled.
Example ¶
package main import ( "fmt" "time" "github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/gsession" ) func main() { storage := gsession.NewStorageFile("", time.Second) storage.SetCryptoKey([]byte("key")) size, _ := storage.GetSize(gctx.New(), "id") fmt.Println(size) }
Output: 0
func (*StorageFile) SetSession ¶
func (s *StorageFile) SetSession(ctx context.Context, sessionId string, sessionData *gmap.StrAnyMap, ttl time.Duration) error
SetSession updates the data map for specified session id. This function is called ever after session, which is changed dirty, is closed. This copy all session data map from memory to storage.
func (*StorageFile) UpdateTTL ¶
UpdateTTL updates the TTL for specified session id. This function is called ever after session, which is not dirty, is closed. It just adds the session id to the async handling queue.
Example ¶
package main import ( "fmt" "time" "github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/gsession" ) func main() { var ( ctx = gctx.New() ) storage := gsession.NewStorageFile("", time.Second) fmt.Println(storage.UpdateTTL(ctx, "id", time.Second*15)) time.Sleep(time.Second * 11) }
Output: <nil>
type StorageMemory ¶
type StorageMemory struct { StorageBase // contains filtered or unexported fields }
StorageMemory implements the Session Storage interface with memory.
func NewStorageMemory ¶
func NewStorageMemory() *StorageMemory
NewStorageMemory creates and returns a file storage object for session.
func (*StorageMemory) GetSession ¶
func (s *StorageMemory) GetSession(ctx context.Context, sessionId string, ttl time.Duration) (*gmap.StrAnyMap, error)
GetSession returns the session data as *gmap.StrAnyMap for given session id from storage.
The parameter `ttl` specifies the TTL for this session, and it returns nil if the TTL is exceeded. The parameter `data` is the current old session data stored in memory, and for some storage it might be nil if memory storage is disabled.
This function is called ever when session starts.
func (*StorageMemory) RemoveAll ¶
func (s *StorageMemory) RemoveAll(ctx context.Context, sessionId string) error
RemoveAll deletes session from storage.
func (*StorageMemory) SetSession ¶
func (s *StorageMemory) SetSession(ctx context.Context, sessionId string, sessionData *gmap.StrAnyMap, ttl time.Duration) error
SetSession updates the data map for specified session id. This function is called ever after session, which is changed dirty, is closed. This copy all session data map from memory to storage.
type StorageRedis ¶
type StorageRedis struct { StorageBase // contains filtered or unexported fields }
StorageRedis implements the Session Storage interface with redis.
func NewStorageRedis ¶
func NewStorageRedis(redis *gredis.Redis, prefix ...string) *StorageRedis
NewStorageRedis creates and returns a redis storage object for session.
func (*StorageRedis) GetSession ¶
func (s *StorageRedis) GetSession(ctx context.Context, sessionId string, ttl time.Duration) (*gmap.StrAnyMap, error)
GetSession returns the session data as *gmap.StrAnyMap for given session id from storage.
The parameter `ttl` specifies the TTL for this session, and it returns nil if the TTL is exceeded. The parameter `data` is the current old session data stored in memory, and for some storage it might be nil if memory storage is disabled.
This function is called ever when session starts.
func (*StorageRedis) RemoveAll ¶
func (s *StorageRedis) RemoveAll(ctx context.Context, sessionId string) error
RemoveAll deletes all key-value pairs from storage.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/gsession" ) func main() { storage := gsession.NewStorageRedis(g.Redis()) err := storage.RemoveAll(gctx.New(), "id") fmt.Println(err != nil) // May Output: // true }
Output:
func (*StorageRedis) SetSession ¶
func (s *StorageRedis) SetSession(ctx context.Context, sessionId string, sessionData *gmap.StrAnyMap, ttl time.Duration) error
SetSession updates the data map for specified session id. This function is called ever after session, which is changed dirty, is closed. This copy all session data map from memory to storage.
func (*StorageRedis) UpdateTTL ¶
UpdateTTL updates the TTL for specified session id. This function is called ever after session, which is not dirty, is closed. It just adds the session id to the async handling queue.
Example ¶
package main import ( "fmt" "time" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/gsession" ) func main() { storage := gsession.NewStorageRedis(g.Redis()) err := storage.UpdateTTL(gctx.New(), "id", time.Second*15) fmt.Println(err) time.Sleep(time.Second * 11) // May Output: // <nil> }
Output:
type StorageRedisHashTable ¶
type StorageRedisHashTable struct { StorageBase // contains filtered or unexported fields }
StorageRedisHashTable implements the Session Storage interface with redis hash table.
func NewStorageRedisHashTable ¶
func NewStorageRedisHashTable(redis *gredis.Redis, prefix ...string) *StorageRedisHashTable
NewStorageRedisHashTable creates and returns a redis hash table storage object for session.
func (*StorageRedisHashTable) Data ¶
func (s *StorageRedisHashTable) Data(ctx context.Context, sessionId string) (data map[string]interface{}, err error)
Data retrieves all key-value pairs as map from storage.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/gsession" ) func main() { storage := gsession.NewStorageRedisHashTable(g.Redis()) data, err := storage.Data(gctx.New(), "id") fmt.Println(data) fmt.Println(err) // May Output: // map[] // redis adapter is not set, missing configuration or adapter register? possible reference: https://github.com/gogf/gf/tree/master/contrib/nosql/redis }
Output:
func (*StorageRedisHashTable) Get ¶
func (s *StorageRedisHashTable) Get(ctx context.Context, sessionId string, key string) (value interface{}, err error)
Get retrieves session value with given key. It returns nil if the key does not exist in the session.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/gsession" ) func main() { storage := gsession.NewStorageRedisHashTable(g.Redis()) v, err := storage.Get(gctx.New(), "id", "key") fmt.Println(v) fmt.Println(err) // May Output: // <nil> // redis adapter is not set, missing configuration or adapter register? possible reference: https://github.com/gogf/gf/tree/master/contrib/nosql/redis }
Output:
func (*StorageRedisHashTable) GetSession ¶
func (s *StorageRedisHashTable) GetSession(ctx context.Context, sessionId string, ttl time.Duration) (*gmap.StrAnyMap, error)
GetSession returns the session data as *gmap.StrAnyMap for given session id from storage.
The parameter `ttl` specifies the TTL for this session, and it returns nil if the TTL is exceeded. The parameter `data` is the current old session data stored in memory, and for some storage it might be nil if memory storage is disabled.
This function is called ever when session starts.
Example ¶
package main import ( "fmt" "time" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/gsession" ) func main() { storage := gsession.NewStorageRedisHashTable(g.Redis()) data, err := storage.GetSession(gctx.New(), "id", time.Second) fmt.Println(data) fmt.Println(err) // May Output: // // redis adapter is not set, missing configuration or adapter register? possible reference: https://github.com/gogf/gf/tree/master/contrib/nosql/redis }
Output:
func (*StorageRedisHashTable) GetSize ¶
func (s *StorageRedisHashTable) GetSize(ctx context.Context, sessionId string) (size int, err error)
GetSize retrieves the size of key-value pairs from storage.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/gsession" ) func main() { storage := gsession.NewStorageRedisHashTable(g.Redis()) size, err := storage.GetSize(gctx.New(), "id") fmt.Println(size) fmt.Println(err) // May Output: // 0 // redis adapter is not set, missing configuration or adapter register? possible reference: https://github.com/gogf/gf/tree/master/contrib/nosql/redis }
Output:
func (*StorageRedisHashTable) Remove ¶
Remove deletes key with its value from storage.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/gsession" ) func main() { storage := gsession.NewStorageRedisHashTable(g.Redis()) err := storage.Remove(gctx.New(), "id", "key") fmt.Println(err) // May Output: // redis adapter is not set, missing configuration or adapter register? possible reference: https://github.com/gogf/gf/tree/master/contrib/nosql/redis }
Output:
func (*StorageRedisHashTable) RemoveAll ¶
func (s *StorageRedisHashTable) RemoveAll(ctx context.Context, sessionId string) error
RemoveAll deletes all key-value pairs from storage.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/gsession" ) func main() { storage := gsession.NewStorageRedisHashTable(g.Redis()) err := storage.RemoveAll(gctx.New(), "id") fmt.Println(err) // May Output: // redis adapter is not set, missing configuration or adapter register? possible reference: https://github.com/gogf/gf/tree/master/contrib/nosql/redis }
Output:
func (*StorageRedisHashTable) Set ¶
func (s *StorageRedisHashTable) Set(ctx context.Context, sessionId string, key string, value interface{}, ttl time.Duration) error
Set sets key-value session pair to the storage. The parameter `ttl` specifies the TTL for the session id (not for the key-value pair).
func (*StorageRedisHashTable) SetMap ¶
func (s *StorageRedisHashTable) SetMap(ctx context.Context, sessionId string, data map[string]interface{}, ttl time.Duration) error
SetMap batch sets key-value session pairs with map to the storage. The parameter `ttl` specifies the TTL for the session id(not for the key-value pair).
func (*StorageRedisHashTable) SetSession ¶
func (s *StorageRedisHashTable) SetSession(ctx context.Context, sessionId string, sessionData *gmap.StrAnyMap, ttl time.Duration) error
SetSession updates the data map for specified session id. This function is called ever after session, which is changed dirty, is closed. This copy all session data map from memory to storage.
Example ¶
package main import ( "fmt" "time" "github.com/gogf/gf/v2/container/gmap" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/gsession" ) func main() { storage := gsession.NewStorageRedisHashTable(g.Redis()) strAnyMap := gmap.StrAnyMap{} err := storage.SetSession(gctx.New(), "id", &strAnyMap, time.Second) fmt.Println(err) // May Output: // redis adapter is not set, missing configuration or adapter register? possible reference: https://github.com/gogf/gf/tree/master/contrib/nosql/redis }
Output:
func (*StorageRedisHashTable) UpdateTTL ¶
func (s *StorageRedisHashTable) UpdateTTL(ctx context.Context, sessionId string, ttl time.Duration) error
UpdateTTL updates the TTL for specified session id. This function is called ever after session, which is not dirty, is closed. It just adds the session id to the async handling queue.
Example ¶
package main import ( "fmt" "time" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/gsession" ) func main() { storage := gsession.NewStorageRedisHashTable(g.Redis()) err := storage.UpdateTTL(gctx.New(), "id", time.Second) fmt.Println(err) // May Output: // redis adapter is not set, missing configuration or adapter register? possible reference: https://github.com/gogf/gf/tree/master/contrib/nosql/redis }
Output: