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) GetStructDeep(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) GetStructsDeep(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(key string) error
- func (s *Session) RemoveAll() error
- func (s *Session) Set(key string, value interface{}) error
- func (s *Session) Sets(data map[string]interface{}) error
- func (s *Session) Size() int
- type Storage
- type StorageFile
- func (s *StorageFile) Get(key string) interface{}
- func (s *StorageFile) GetMap() map[string]interface{}
- func (s *StorageFile) GetSession(id string, ttl time.Duration) map[string]interface{}
- func (s *StorageFile) GetSize(id string) int
- func (s *StorageFile) Remove(key string) error
- func (s *StorageFile) RemoveAll() error
- func (s *StorageFile) Set(key string, value interface{}) error
- func (s *StorageFile) SetCryptoEnabled(enabled bool)
- func (s *StorageFile) SetCryptoKey(key []byte)
- func (s *StorageFile) SetMap(data map[string]interface{}) error
- func (s *StorageFile) SetSession(id string, data map[string]interface{}) error
- func (s *StorageFile) UpdateTTL(id string) error
Constants ¶
This section is empty.
Variables ¶
var ( DefaultStorageFilePath = gfile.Join(gfile.TempDir(), "gsessions") DefaultStorageFileCryptoKey = []byte("Session storage file crypto key!") DefaultStorageFileCryptoEnabled = false DefaultStorageFileLoopInterval = time.Minute ErrorDisabled = errors.New("this feature is disabled in this storage") )
Functions ¶
func NewSessionId ¶
func NewSessionId() string
NewSessionId creates and returns a new and unique session id string, the length of which is 18 bytes.
Types ¶
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager for sessions.
func (*Manager) SetStorage ¶
SetStorage sets the session storage for manager.
func (*Manager) UpdateSessionTTL ¶
UpdateSessionTTL updates the ttl for given session. If this session is dirty, it also exports it to storage.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session struct for storing single session data.
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 return nil.
func (*Session) GetDuration ¶
func (*Session) GetFloat32 ¶
func (*Session) GetFloat64 ¶
func (*Session) GetInterfaces ¶
func (*Session) GetMapDeep ¶
func (*Session) GetMapsDeep ¶
func (*Session) GetStrings ¶
func (*Session) GetStructDeep ¶
func (*Session) GetStructs ¶
func (*Session) GetStructsDeep ¶
func (*Session) Id ¶
Id returns the session id for this session. It create 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.
type Storage ¶
type Storage interface { // Get retrieves session value with given key. // It returns nil if the key does not exist in the session. Get(key string) interface{} // GetMap retrieves all key-value pairs as map from storage. GetMap() map[string]interface{} // GetSize retrieves the size of key-value pairs from storage. GetSize(id string) int // Set sets key-value session pair to the storage. Set(key string, value interface{}) error // SetMap batch sets key-value session pairs with map to the storage. SetMap(data map[string]interface{}) error // Remove deletes key with its value from storage. Remove(key string) error // RemoveAll deletes all key-value pairs from storage. RemoveAll() error // GetSession returns the session data map for given session id. // The parameter specifies the TTL for this session. // It returns nil if the TTL is exceeded. GetSession(id string, ttl time.Duration) map[string]interface{} // SetSession updates the data map for specified session id. SetSession(id string, data map[string]interface{}) error // UpdateTTL updates the TTL for specified session id. UpdateTTL(id string) error }
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(key string) interface{}
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() map[string]interface{}
GetMap retrieves all key-value pairs as map from storage.
func (*StorageFile) GetSession ¶
func (s *StorageFile) GetSession(id string, ttl time.Duration) map[string]interface{}
GetSession return the session data for given session id.
func (*StorageFile) GetSize ¶
func (s *StorageFile) GetSize(id string) int
GetSize retrieves the size of key-value pairs from storage.
func (*StorageFile) Remove ¶
func (s *StorageFile) Remove(key string) error
Remove deletes key with its value from storage.
func (*StorageFile) RemoveAll ¶
func (s *StorageFile) RemoveAll() error
RemoveAll deletes all key-value pairs from storage.
func (*StorageFile) Set ¶
func (s *StorageFile) Set(key string, value interface{}) error
Set sets key-value session pair to the storage.
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(data map[string]interface{}) error
SetMap batch sets key-value session pairs with map to the storage.
func (*StorageFile) SetSession ¶
func (s *StorageFile) SetSession(id string, data map[string]interface{}) error
SetSession updates the content for session id. Note that the parameter <content> is the serialized bytes for session map.
func (*StorageFile) UpdateTTL ¶
func (s *StorageFile) UpdateTTL(id string) error
UpdateTTL updates the TTL for specified session id. It just adds the session id to the async handling queue.