Documentation ¶
Overview ¶
Package gsession implements manager and storage features for sessions.
Index ¶
- Variables
- func NewSessionId() string
- type Manager
- type Session
- func (s *Session) Clear() error
- func (s *Session) Close()
- func (s *Session) Contains(key string) bool
- func (s *Session) Get(key string, def ...interface{}) interface{}
- func (s *Session) GetBool(key string, def ...interface{}) bool
- func (s *Session) GetBytes(key string, def ...interface{}) []byte
- func (s *Session) GetDuration(key string, def ...interface{}) time.Duration
- func (s *Session) GetFloat32(key string, def ...interface{}) float32
- func (s *Session) GetFloat64(key string, def ...interface{}) float64
- func (s *Session) GetFloats(key string, def ...interface{}) []float64
- func (s *Session) GetGTime(key string, format ...string) *gtime.Time
- func (s *Session) GetInt(key string, def ...interface{}) int
- func (s *Session) GetInt16(key string, def ...interface{}) int16
- func (s *Session) GetInt32(key string, def ...interface{}) int32
- func (s *Session) GetInt64(key string, def ...interface{}) int64
- func (s *Session) GetInt8(key string, def ...interface{}) int8
- func (s *Session) GetInterfaces(key string, def ...interface{}) []interface{}
- func (s *Session) GetInts(key string, def ...interface{}) []int
- func (s *Session) GetMap(key string, tags ...string) map[string]interface{}
- func (s *Session) GetMapDeep(key string, tags ...string) map[string]interface{}
- func (s *Session) GetMaps(key string, tags ...string) []map[string]interface{}
- func (s *Session) GetMapsDeep(key string, tags ...string) []map[string]interface{}
- func (s *Session) GetString(key string, def ...interface{}) string
- func (s *Session) GetStrings(key string, def ...interface{}) []string
- func (s *Session) GetStruct(key string, pointer interface{}, mapping ...map[string]string) error
- func (s *Session) GetStructs(key string, pointer interface{}, mapping ...map[string]string) error
- func (s *Session) GetTime(key string, format ...string) time.Time
- func (s *Session) GetUint(key string, def ...interface{}) uint
- func (s *Session) GetUint16(key string, def ...interface{}) uint16
- func (s *Session) GetUint32(key string, def ...interface{}) uint32
- func (s *Session) GetUint64(key string, def ...interface{}) uint64
- func (s *Session) GetUint8(key string, def ...interface{}) uint8
- func (s *Session) GetVar(key string, def ...interface{}) *gvar.Var
- func (s *Session) Id() string
- func (s *Session) IsDirty() bool
- func (s *Session) Map() map[string]interface{}
- func (s *Session) Remove(keys ...string) error
- func (s *Session) RemoveAll() error
- func (s *Session) Set(key string, value interface{}) 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{}) error
- func (s *Session) Sets(data map[string]interface{}) error
- func (s *Session) Size() int
- type Storage
- type StorageFile
- func (s *StorageFile) Get(ctx context.Context, id string, key string) (value interface{}, err error)
- func (s *StorageFile) GetMap(ctx context.Context, id string) (data map[string]interface{}, err error)
- func (s *StorageFile) GetSession(ctx context.Context, id string, ttl time.Duration, data *gmap.StrAnyMap) (*gmap.StrAnyMap, error)
- func (s *StorageFile) GetSize(ctx context.Context, id string) (size int, err error)
- func (s *StorageFile) New(ctx context.Context, ttl time.Duration) (id string, err error)
- func (s *StorageFile) Remove(ctx context.Context, id string, key string) error
- func (s *StorageFile) RemoveAll(ctx context.Context, id string) error
- func (s *StorageFile) Set(ctx context.Context, id string, key string, value interface{}, ...) error
- func (s *StorageFile) SetCryptoEnabled(enabled bool)
- func (s *StorageFile) SetCryptoKey(key []byte)
- func (s *StorageFile) SetMap(ctx context.Context, id string, data map[string]interface{}, ttl time.Duration) error
- func (s *StorageFile) SetSession(ctx context.Context, id string, data *gmap.StrAnyMap, ttl time.Duration) error
- func (s *StorageFile) UpdateTTL(ctx context.Context, id string, ttl time.Duration) error
- type StorageMemory
- func (s *StorageMemory) Get(ctx context.Context, id string, key string) (value interface{}, err error)
- func (s *StorageMemory) GetMap(ctx context.Context, id string) (data map[string]interface{}, err error)
- func (s *StorageMemory) GetSession(ctx context.Context, id string, ttl time.Duration, data *gmap.StrAnyMap) (*gmap.StrAnyMap, error)
- func (s *StorageMemory) GetSize(ctx context.Context, id string) (size int, err error)
- func (s *StorageMemory) New(ctx context.Context, ttl time.Duration) (id string, err error)
- func (s *StorageMemory) Remove(ctx context.Context, id string, key string) error
- func (s *StorageMemory) RemoveAll(ctx context.Context, id string) error
- func (s *StorageMemory) Set(ctx context.Context, id string, key string, value interface{}, ...) error
- func (s *StorageMemory) SetMap(ctx context.Context, id string, data map[string]interface{}, ttl time.Duration) error
- func (s *StorageMemory) SetSession(ctx context.Context, id string, data *gmap.StrAnyMap, ttl time.Duration) error
- func (s *StorageMemory) UpdateTTL(ctx context.Context, id string, ttl time.Duration) error
- type StorageRedis
- func (s *StorageRedis) Get(ctx context.Context, id string, key string) (value interface{}, err error)
- func (s *StorageRedis) GetMap(ctx context.Context, id string) (data map[string]interface{}, err error)
- func (s *StorageRedis) GetSession(ctx context.Context, id string, ttl time.Duration, data *gmap.StrAnyMap) (*gmap.StrAnyMap, error)
- func (s *StorageRedis) GetSize(ctx context.Context, id string) (size int, err error)
- func (s *StorageRedis) New(ctx context.Context, ttl time.Duration) (id string, err error)
- func (s *StorageRedis) Remove(ctx context.Context, id string, key string) error
- func (s *StorageRedis) RemoveAll(ctx context.Context, id string) error
- func (s *StorageRedis) Set(ctx context.Context, id string, key string, value interface{}, ...) error
- func (s *StorageRedis) SetMap(ctx context.Context, id string, data map[string]interface{}, ttl time.Duration) error
- func (s *StorageRedis) SetSession(ctx context.Context, id string, data *gmap.StrAnyMap, ttl time.Duration) error
- func (s *StorageRedis) UpdateTTL(ctx context.Context, id string, ttl time.Duration) error
- type StorageRedisHashTable
- func (s *StorageRedisHashTable) Get(ctx context.Context, id string, key string) (value interface{}, err error)
- func (s *StorageRedisHashTable) GetMap(ctx context.Context, id string) (data map[string]interface{}, err error)
- func (s *StorageRedisHashTable) GetSession(ctx context.Context, id string, ttl time.Duration, data *gmap.StrAnyMap) (*gmap.StrAnyMap, error)
- func (s *StorageRedisHashTable) GetSize(ctx context.Context, id string) (size int, err error)
- func (s *StorageRedisHashTable) New(ctx context.Context, ttl time.Duration) (id string, err error)
- func (s *StorageRedisHashTable) Remove(ctx context.Context, id string, key string) error
- func (s *StorageRedisHashTable) RemoveAll(ctx context.Context, id string) error
- func (s *StorageRedisHashTable) Set(ctx context.Context, id string, key string, value interface{}, ...) error
- func (s *StorageRedisHashTable) SetMap(ctx context.Context, id string, data map[string]interface{}, ttl time.Duration) error
- func (s *StorageRedisHashTable) SetSession(ctx context.Context, id string, data *gmap.StrAnyMap, ttl time.Duration) error
- func (s *StorageRedisHashTable) UpdateTTL(ctx context.Context, id string, ttl time.Duration) error
Constants ¶
This section is empty.
Variables ¶
var ( DefaultStorageFilePath = gfile.TempDir("gsessions") DefaultStorageFileCryptoKey = []byte("Session storage file crypto key!") DefaultStorageFileCryptoEnabled = false DefaultStorageFileLoopInterval = 10 * time.Second )
var ( // DefaultStorageRedisLoopInterval is the interval updating TTL for session ids // in last duration. DefaultStorageRedisLoopInterval = 10 * time.Second )
var ( ErrorDisabled = gerror.NewOption(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 36 bytes.
Types ¶
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager for sessions.
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.
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 ¶
func (s *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) 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) GetDuration ¶
func (*Session) GetFloat32 ¶
func (*Session) GetFloat64 ¶
func (*Session) GetInterfaces ¶
func (*Session) GetMapDeep ¶
func (*Session) GetMapsDeep ¶
func (*Session) GetStrings ¶
func (*Session) GetStructs ¶
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.
func (*Session) Map ¶
Map returns all data as map. Note that it's using value copy internally for concurrent-safe purpose.
func (*Session) SetId ¶ added in v1.13.0
SetId sets custom session before session starts. It returns error if it is called after session starts.
func (*Session) SetIdFunc ¶ added in v1.13.0
SetIdFunc sets custom session id creating function before session starts. It returns error if it is called after session starts.
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) (id string, err error) // Get retrieves and returns session value with given key. // It returns nil if the key does not exist in the session. Get(ctx context.Context, id string, key string) (value interface{}, err error) // GetMap retrieves all key-value pairs as map from storage. GetMap(ctx context.Context, id string) (data map[string]interface{}, err error) // GetSize retrieves and returns the size of key-value pairs from storage. GetSize(ctx context.Context, id string) (size int, 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, id 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, id string, data map[string]interface{}, ttl time.Duration) error // Remove deletes key with its value from storage. Remove(ctx context.Context, id string, key string) error // RemoveAll deletes all key-value pairs from storage. RemoveAll(ctx context.Context, id string) error // GetSession returns the session data as `*gmap.StrAnyMap` for given session id 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 TTL is exceeded. GetSession(ctx context.Context, id string, ttl time.Duration, data *gmap.StrAnyMap) (*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, id string, data *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, id string, ttl time.Duration) error }
Storage is the interface definition for session storage.
type StorageFile ¶
type StorageFile struct {
// contains filtered or unexported fields
}
StorageFile implements the Session Storage interface with file system.
func NewStorageFile ¶
func NewStorageFile(path ...string) *StorageFile
NewStorageFile creates and returns a file storage object for session.
func (*StorageFile) Get ¶
func (s *StorageFile) Get(ctx context.Context, id 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.
func (*StorageFile) GetMap ¶
func (s *StorageFile) GetMap(ctx context.Context, id string) (data map[string]interface{}, err error)
GetMap retrieves all key-value pairs as map from storage.
func (*StorageFile) GetSession ¶
func (s *StorageFile) GetSession(ctx context.Context, id string, ttl time.Duration, data *gmap.StrAnyMap) (*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 (*StorageFile) New ¶ added in v1.10.0
New creates a session id. This function can be used for custom session creation.
func (*StorageFile) RemoveAll ¶
func (s *StorageFile) RemoveAll(ctx context.Context, id string) error
RemoveAll deletes all key-value pairs from storage.
func (*StorageFile) Set ¶
func (s *StorageFile) Set(ctx context.Context, id 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 (*StorageFile) SetCryptoEnabled ¶
func (s *StorageFile) SetCryptoEnabled(enabled bool)
SetCryptoEnabled enables/disables the crypto feature for session storage.
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.
func (*StorageFile) SetMap ¶
func (s *StorageFile) SetMap(ctx context.Context, id 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 (*StorageFile) SetSession ¶
func (s *StorageFile) SetSession(ctx context.Context, id string, data *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 StorageMemory ¶ added in v1.10.0
type StorageMemory struct{}
StorageMemory implements the Session Storage interface with memory.
func NewStorageMemory ¶ added in v1.10.0
func NewStorageMemory() *StorageMemory
NewStorageMemory creates and returns a file storage object for session.
func (*StorageMemory) Get ¶ added in v1.10.0
func (s *StorageMemory) Get(ctx context.Context, id 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.
func (*StorageMemory) GetMap ¶ added in v1.10.0
func (s *StorageMemory) GetMap(ctx context.Context, id string) (data map[string]interface{}, err error)
GetMap retrieves all key-value pairs as map from storage.
func (*StorageMemory) GetSession ¶ added in v1.10.0
func (s *StorageMemory) GetSession(ctx context.Context, id string, ttl time.Duration, data *gmap.StrAnyMap) (*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) GetSize ¶ added in v1.10.0
GetSize retrieves the size of key-value pairs from storage.
func (*StorageMemory) New ¶ added in v1.10.0
New creates a session id. This function can be used for custom session creation.
func (*StorageMemory) RemoveAll ¶ added in v1.10.0
func (s *StorageMemory) RemoveAll(ctx context.Context, id string) error
RemoveAll deletes all key-value pairs from storage.
func (*StorageMemory) Set ¶ added in v1.10.0
func (s *StorageMemory) Set(ctx context.Context, id 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 (*StorageMemory) SetMap ¶ added in v1.10.0
func (s *StorageMemory) SetMap(ctx context.Context, id 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 (*StorageMemory) SetSession ¶ added in v1.10.0
func (s *StorageMemory) SetSession(ctx context.Context, id string, data *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 ¶ added in v1.10.0
type StorageRedis struct {
// contains filtered or unexported fields
}
StorageRedis implements the Session Storage interface with redis.
func NewStorageRedis ¶ added in v1.10.0
func NewStorageRedis(redis *gredis.Redis, prefix ...string) *StorageRedis
NewStorageRedis creates and returns a redis storage object for session.
func (*StorageRedis) Get ¶ added in v1.10.0
func (s *StorageRedis) Get(ctx context.Context, id 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.
func (*StorageRedis) GetMap ¶ added in v1.10.0
func (s *StorageRedis) GetMap(ctx context.Context, id string) (data map[string]interface{}, err error)
GetMap retrieves all key-value pairs as map from storage.
func (*StorageRedis) GetSession ¶ added in v1.10.0
func (s *StorageRedis) GetSession(ctx context.Context, id string, ttl time.Duration, data *gmap.StrAnyMap) (*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) GetSize ¶ added in v1.10.0
GetSize retrieves the size of key-value pairs from storage.
func (*StorageRedis) New ¶ added in v1.10.0
New creates a session id. This function can be used for custom session creation.
func (*StorageRedis) RemoveAll ¶ added in v1.10.0
func (s *StorageRedis) RemoveAll(ctx context.Context, id string) error
RemoveAll deletes all key-value pairs from storage.
func (*StorageRedis) Set ¶ added in v1.10.0
func (s *StorageRedis) Set(ctx context.Context, id 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 (*StorageRedis) SetMap ¶ added in v1.10.0
func (s *StorageRedis) SetMap(ctx context.Context, id 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 (*StorageRedis) SetSession ¶ added in v1.10.0
func (s *StorageRedis) SetSession(ctx context.Context, id string, data *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 StorageRedisHashTable ¶ added in v1.10.0
type StorageRedisHashTable struct {
// contains filtered or unexported fields
}
StorageRedisHashTable implements the Session Storage interface with redis hash table.
func NewStorageRedisHashTable ¶ added in v1.10.0
func NewStorageRedisHashTable(redis *gredis.Redis, prefix ...string) *StorageRedisHashTable
NewStorageRedisHashTable creates and returns a redis hash table storage object for session.
func (*StorageRedisHashTable) Get ¶ added in v1.10.0
func (s *StorageRedisHashTable) Get(ctx context.Context, id 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.
func (*StorageRedisHashTable) GetMap ¶ added in v1.10.0
func (s *StorageRedisHashTable) GetMap(ctx context.Context, id string) (data map[string]interface{}, err error)
GetMap retrieves all key-value pairs as map from storage.
func (*StorageRedisHashTable) GetSession ¶ added in v1.10.0
func (s *StorageRedisHashTable) GetSession(ctx context.Context, id string, ttl time.Duration, data *gmap.StrAnyMap) (*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 (*StorageRedisHashTable) GetSize ¶ added in v1.10.0
GetSize retrieves the size of key-value pairs from storage.
func (*StorageRedisHashTable) New ¶ added in v1.10.0
New creates a session id. This function can be used for custom session creation.
func (*StorageRedisHashTable) Remove ¶ added in v1.10.0
Remove deletes key with its value from storage.
func (*StorageRedisHashTable) RemoveAll ¶ added in v1.10.0
func (s *StorageRedisHashTable) RemoveAll(ctx context.Context, id string) error
RemoveAll deletes all key-value pairs from storage.
func (*StorageRedisHashTable) Set ¶ added in v1.10.0
func (s *StorageRedisHashTable) Set(ctx context.Context, id 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 ¶ added in v1.10.0
func (s *StorageRedisHashTable) SetMap(ctx context.Context, id 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 ¶ added in v1.10.0
func (s *StorageRedisHashTable) SetSession(ctx context.Context, id string, data *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.