Documentation
¶
Overview ¶
Package suk offers easy server-side session management using single-use keys.
You may use an in-memory map (default) or a Redis client to hold your sessions. Do note that, when using an in-memory map, the session data is lost as soon as the program stops.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrNilRedisClient = errors.New("The given Redis client is nil.") ErrRedisClientAlreadySet = errors.New("A Redis client was already registered for this session storage.") ErrZeroKeyLength = errors.New("The given key length must be at least 1.") ErrNonPositiveKeyDuration = errors.New("The given key duration must be positive.") ErrNilRandomKeyGenerator = errors.New("The given random key generator function is nil.") ErrCustomKeyLengthAlreadySet = errors.New("A custom key length was already registered for this session storage.") ErrCustomKeyDurationAlreadySet = errors.New("A custom key duration was already registered for this session storage.") ErrAutoClearExpiredKeysAlreadySet = errors.New("Auto clear for expired keys was already set for this session storage.") )
Functions ¶
Types ¶
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
func WithAutoClearExpiredKeys ¶
func WithAutoClearExpiredKeys() Option
WithAutoClearExpiredKeys automatically clears expired keys at intervals based on the set key expiration time. By default, the clearing process occurs every 10 minutes, but this can be adjusted by setting a different key expiration time using WithTokenDuration.
func WithCustomRandomKeyGenerator ¶
WithCustomRandomKeyGenerator sets a custom function to generate the keys.
func WithKeyDuration ¶
WithKeyDuration sets a custom duration for generated keys. The default is 10 minutes.
func WithKeyLength ¶
WithKeyLength sets a custom key length for generated keys. The default is 32, which gives an entropy of 192 for each key, which should be fine for most applications.
Be aware that low length keys are susceptible to guessing, as the entropy level goes down. To calculate entropy, do keyLength * log2(65).
type SessionStorage ¶
type SessionStorage struct {
// contains filtered or unexported fields
}
func (*SessionStorage) ClearExpired ¶
func (ss *SessionStorage) ClearExpired() error
ClearExpired removes all expired keys. For Redis, this function is a no-op as Redis handles expiration automatically. To enable similar behavior for the default syncMap, start the SessionStorage with the WithAutoClearExpiredKeys option.
func (*SessionStorage) Get ¶
func (ss *SessionStorage) Get(key string) (any, string, error)
Get retrieves the session and generates a new key for it.
func (*SessionStorage) Remove ¶
func (ss *SessionStorage) Remove(key string) error
Remove deletes the specified key and its associated value.