gf: github.com/gogf/gf/os/gsession Index | Files

package gsession

import "github.com/gogf/gf/os/gsession"

Package gsession implements manager and storage features for sessions.

Index

Package Files

gsession.go gsession_manager.go gsession_session.go gsession_storage.go gsession_storage_file.go gsession_storage_memory.go gsession_storage_redis.go gsession_storage_redis_hashtable.go

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 = time.Minute
)
var (
    ErrorDisabled = errors.New("this feature is disabled in this storage")
)

func NewSessionId Uses

func NewSessionId() string

NewSessionId creates and returns a new and unique session id string, which is in 36 bytes.

type Manager Uses

type Manager struct {
    // contains filtered or unexported fields
}

Manager for sessions.

func New Uses

func New(ttl time.Duration, storage ...Storage) *Manager

New creates and returns a new session manager.

func (*Manager) New Uses

func (m *Manager) New(sessionId ...string) *Session

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 Uses

func (m *Manager) SetStorage(storage Storage)

SetStorage sets the session storage for manager.

func (*Manager) SetTTL Uses

func (m *Manager) SetTTL(ttl time.Duration)

SetTTL the TTL for the session manager.

func (*Manager) TTL Uses

func (m *Manager) TTL() time.Duration

TTL returns the TTL of the session manager.

func (*Manager) UpdateSessionTTL Uses

func (m *Manager) UpdateSessionTTL(id string, data *gmap.StrAnyMap)

UpdateSessionTTL updates the ttl for given session.

type Session Uses

type Session struct {
    // contains filtered or unexported fields
}

Session struct for storing single session data, which is bound to a single request.

func (*Session) Clear Uses

func (s *Session) Clear() error

Clear is alias of RemoveAll.

func (*Session) Close Uses

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) Contains Uses

func (s *Session) Contains(key string) bool

Contains checks whether key exist in the session.

func (*Session) Get Uses

func (s *Session) Get(key string, def ...interface{}) interface{}

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) GetBool Uses

func (s *Session) GetBool(key string, def ...interface{}) bool

func (*Session) GetBytes Uses

func (s *Session) GetBytes(key string, def ...interface{}) []byte

func (*Session) GetDuration Uses

func (s *Session) GetDuration(key string, def ...interface{}) time.Duration

func (*Session) GetFloat32 Uses

func (s *Session) GetFloat32(key string, def ...interface{}) float32

func (*Session) GetFloat64 Uses

func (s *Session) GetFloat64(key string, def ...interface{}) float64

func (*Session) GetFloats Uses

func (s *Session) GetFloats(key string, def ...interface{}) []float64

func (*Session) GetGTime Uses

func (s *Session) GetGTime(key string, format ...string) *gtime.Time

func (*Session) GetInt Uses

func (s *Session) GetInt(key string, def ...interface{}) int

func (*Session) GetInt16 Uses

func (s *Session) GetInt16(key string, def ...interface{}) int16

func (*Session) GetInt32 Uses

func (s *Session) GetInt32(key string, def ...interface{}) int32

func (*Session) GetInt64 Uses

func (s *Session) GetInt64(key string, def ...interface{}) int64

func (*Session) GetInt8 Uses

func (s *Session) GetInt8(key string, def ...interface{}) int8

func (*Session) GetInterfaces Uses

func (s *Session) GetInterfaces(key string, def ...interface{}) []interface{}

func (*Session) GetInts Uses

func (s *Session) GetInts(key string, def ...interface{}) []int

func (*Session) GetMap Uses

func (s *Session) GetMap(key string, tags ...string) map[string]interface{}

func (*Session) GetMapDeep Uses

func (s *Session) GetMapDeep(key string, tags ...string) map[string]interface{}

func (*Session) GetMaps Uses

func (s *Session) GetMaps(key string, tags ...string) []map[string]interface{}

func (*Session) GetMapsDeep Uses

func (s *Session) GetMapsDeep(key string, tags ...string) []map[string]interface{}

func (*Session) GetString Uses

func (s *Session) GetString(key string, def ...interface{}) string

func (*Session) GetStrings Uses

func (s *Session) GetStrings(key string, def ...interface{}) []string

func (*Session) GetStruct Uses

func (s *Session) GetStruct(key string, pointer interface{}, mapping ...map[string]string) error

func (*Session) GetStructDeep Uses

func (s *Session) GetStructDeep(key string, pointer interface{}, mapping ...map[string]string) error

func (*Session) GetStructs Uses

func (s *Session) GetStructs(key string, pointer interface{}, mapping ...map[string]string) error

func (*Session) GetStructsDeep Uses

func (s *Session) GetStructsDeep(key string, pointer interface{}, mapping ...map[string]string) error

func (*Session) GetTime Uses

func (s *Session) GetTime(key string, format ...string) time.Time

func (*Session) GetUint Uses

func (s *Session) GetUint(key string, def ...interface{}) uint

func (*Session) GetUint16 Uses

func (s *Session) GetUint16(key string, def ...interface{}) uint16

func (*Session) GetUint32 Uses

func (s *Session) GetUint32(key string, def ...interface{}) uint32

func (*Session) GetUint64 Uses

func (s *Session) GetUint64(key string, def ...interface{}) uint64

func (*Session) GetUint8 Uses

func (s *Session) GetUint8(key string, def ...interface{}) uint8

func (*Session) GetVar Uses

func (s *Session) GetVar(key string, def ...interface{}) *gvar.Var

func (*Session) Id Uses

func (s *Session) Id() string

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) IsDirty Uses

func (s *Session) IsDirty() bool

IsDirty checks whether there's any data changes in the session.

func (*Session) Map Uses

func (s *Session) Map() map[string]interface{}

Map returns all data as map. Note that it's using value copy internally for concurrent-safe purpose.

func (*Session) Remove Uses

func (s *Session) Remove(key string) error

Remove removes key along with its value from this session.

func (*Session) RemoveAll Uses

func (s *Session) RemoveAll() error

RemoveAll deletes all key-value pairs from this session.

func (*Session) Set Uses

func (s *Session) Set(key string, value interface{}) error

Set sets key-value pair to this session.

func (*Session) SetId Uses

func (s *Session) SetId(id string) error

SetId sets custom session before session starts. It returns error if it is called after session starts.

func (*Session) SetIdFunc Uses

func (s *Session) SetIdFunc(f func(ttl time.Duration) string) error

SetIdFunc sets custom session id creating function before session starts. It returns error if it is called after session starts.

func (*Session) Sets Uses

func (s *Session) Sets(data map[string]interface{}) error

Sets batch sets the session using map.

func (*Session) Size Uses

func (s *Session) Size() int

Size returns the size of the session.

type Storage Uses

type Storage interface {
    // New creates a custom session id.
    // This function can be used for custom session creation.
    New(ttl time.Duration) (id string)

    // Get retrieves and returns session value with given key.
    // It returns nil if the key does not exist in the session.
    Get(id string, key string) interface{}

    // GetMap retrieves all key-value pairs as map from storage.
    GetMap(id string) map[string]interface{}

    // GetSize retrieves and returns the size of key-value pairs from storage.
    GetSize(id string) int

    // Set sets one key-value session pair to the storage.
    // The parameter <ttl> specifies the TTL for the session id.
    Set(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(id string, data map[string]interface{}, ttl time.Duration) error

    // Remove deletes key with its value from storage.
    Remove(id string, key string) error

    // RemoveAll deletes all key-value pairs from storage.
    RemoveAll(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(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(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(id string, ttl time.Duration) error
}

Storage is the interface definition for session storage.

type StorageFile Uses

type StorageFile struct {
    // contains filtered or unexported fields
}

StorageFile implements the Session Storage interface with file system.

func NewStorageFile Uses

func NewStorageFile(path ...string) *StorageFile

NewStorageFile creates and returns a file storage object for session.

func (*StorageFile) Get Uses

func (s *StorageFile) Get(id string, 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 Uses

func (s *StorageFile) GetMap(id string) map[string]interface{}

GetMap retrieves all key-value pairs as map from storage.

func (*StorageFile) GetSession Uses

func (s *StorageFile) GetSession(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) GetSize Uses

func (s *StorageFile) GetSize(id string) int

GetSize retrieves the size of key-value pairs from storage.

func (*StorageFile) New Uses

func (s *StorageFile) New(ttl time.Duration) (id string)

New creates a session id. This function can be used for custom session creation.

func (*StorageFile) Remove Uses

func (s *StorageFile) Remove(id string, key string) error

Remove deletes key with its value from storage.

func (*StorageFile) RemoveAll Uses

func (s *StorageFile) RemoveAll(id string) error

RemoveAll deletes all key-value pairs from storage.

func (*StorageFile) Set Uses

func (s *StorageFile) Set(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 Uses

func (s *StorageFile) SetCryptoEnabled(enabled bool)

SetCryptoEnabled enables/disables the crypto feature for session storage.

func (*StorageFile) SetCryptoKey Uses

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 Uses

func (s *StorageFile) SetMap(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 Uses

func (s *StorageFile) SetSession(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.

func (*StorageFile) UpdateTTL Uses

func (s *StorageFile) UpdateTTL(id 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.

type StorageMemory Uses

type StorageMemory struct{}

StorageMemory implements the Session Storage interface with memory.

func NewStorageMemory Uses

func NewStorageMemory() *StorageMemory

NewStorageMemory creates and returns a file storage object for session.

func (*StorageMemory) Get Uses

func (s *StorageMemory) Get(id string, key string) interface{}

Get retrieves session value with given key. It returns nil if the key does not exist in the session.

func (*StorageMemory) GetMap Uses

func (s *StorageMemory) GetMap(id string) map[string]interface{}

GetMap retrieves all key-value pairs as map from storage.

func (*StorageMemory) GetSession Uses

func (s *StorageMemory) GetSession(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 Uses

func (s *StorageMemory) GetSize(id string) int

GetSize retrieves the size of key-value pairs from storage.

func (*StorageMemory) New Uses

func (s *StorageMemory) New(ttl time.Duration) (id string)

New creates a session id. This function can be used for custom session creation.

func (*StorageMemory) Remove Uses

func (s *StorageMemory) Remove(id string, key string) error

Remove deletes key with its value from storage.

func (*StorageMemory) RemoveAll Uses

func (s *StorageMemory) RemoveAll(id string) error

RemoveAll deletes all key-value pairs from storage.

func (*StorageMemory) Set Uses

func (s *StorageMemory) Set(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 Uses

func (s *StorageMemory) SetMap(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 Uses

func (s *StorageMemory) SetSession(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.

func (*StorageMemory) UpdateTTL Uses

func (s *StorageMemory) UpdateTTL(id 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.

type StorageRedis Uses

type StorageRedis struct {
    // contains filtered or unexported fields
}

StorageRedis implements the Session Storage interface with redis.

func NewStorageRedis Uses

func NewStorageRedis(redis *gredis.Redis, prefix ...string) *StorageRedis

NewStorageRedis creates and returns a redis storage object for session.

func (*StorageRedis) Get Uses

func (s *StorageRedis) Get(id string, key string) interface{}

Get retrieves session value with given key. It returns nil if the key does not exist in the session.

func (*StorageRedis) GetMap Uses

func (s *StorageRedis) GetMap(id string) map[string]interface{}

GetMap retrieves all key-value pairs as map from storage.

func (*StorageRedis) GetSession Uses

func (s *StorageRedis) GetSession(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 Uses

func (s *StorageRedis) GetSize(id string) int

GetSize retrieves the size of key-value pairs from storage.

func (*StorageRedis) New Uses

func (s *StorageRedis) New(ttl time.Duration) (id string)

New creates a session id. This function can be used for custom session creation.

func (*StorageRedis) Remove Uses

func (s *StorageRedis) Remove(id string, key string) error

Remove deletes key with its value from storage.

func (*StorageRedis) RemoveAll Uses

func (s *StorageRedis) RemoveAll(id string) error

RemoveAll deletes all key-value pairs from storage.

func (*StorageRedis) Set Uses

func (s *StorageRedis) Set(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 Uses

func (s *StorageRedis) SetMap(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 Uses

func (s *StorageRedis) SetSession(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.

func (*StorageRedis) UpdateTTL Uses

func (s *StorageRedis) UpdateTTL(id 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.

type StorageRedisHashTable Uses

type StorageRedisHashTable struct {
    // contains filtered or unexported fields
}

StorageRedisHashTable implements the Session Storage interface with redis hash table.

func NewStorageRedisHashTable Uses

func NewStorageRedisHashTable(redis *gredis.Redis, prefix ...string) *StorageRedisHashTable

NewStorageRedisHashTable creates and returns a redis hash table storage object for session.

func (*StorageRedisHashTable) Get Uses

func (s *StorageRedisHashTable) Get(id string, key string) interface{}

Get retrieves session value with given key. It returns nil if the key does not exist in the session.

func (*StorageRedisHashTable) GetMap Uses

func (s *StorageRedisHashTable) GetMap(id string) map[string]interface{}

GetMap retrieves all key-value pairs as map from storage.

func (*StorageRedisHashTable) GetSession Uses

func (s *StorageRedisHashTable) GetSession(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 Uses

func (s *StorageRedisHashTable) GetSize(id string) int

GetSize retrieves the size of key-value pairs from storage.

func (*StorageRedisHashTable) New Uses

func (s *StorageRedisHashTable) New(ttl time.Duration) (id string)

New creates a session id. This function can be used for custom session creation.

func (*StorageRedisHashTable) Remove Uses

func (s *StorageRedisHashTable) Remove(id string, key string) error

Remove deletes key with its value from storage.

func (*StorageRedisHashTable) RemoveAll Uses

func (s *StorageRedisHashTable) RemoveAll(id string) error

RemoveAll deletes all key-value pairs from storage.

func (*StorageRedisHashTable) Set Uses

func (s *StorageRedisHashTable) Set(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 Uses

func (s *StorageRedisHashTable) SetMap(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 Uses

func (s *StorageRedisHashTable) SetSession(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.

func (*StorageRedisHashTable) UpdateTTL Uses

func (s *StorageRedisHashTable) UpdateTTL(id 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.

Package gsession imports 18 packages (graph) and is imported by 1 packages. Updated 2020-07-04. Refresh now. Tools for package owners.