Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Element ¶ added in v1.0.3
type Element struct {
// contains filtered or unexported fields
}
link list
type InMemorySessionStore ¶ added in v1.0.3
type InMemorySessionStore struct {
// contains filtered or unexported fields
}
InMemorySessionStore stores the session in local memory.
func (*InMemorySessionStore) Delete ¶ added in v1.0.3
func (s *InMemorySessionStore) Delete(ctx context.Context, key string) error
type RedisSessionStore ¶
type RedisSessionStore struct {
// contains filtered or unexported fields
}
InMemorySessionStore stores the session in redis.
func (*RedisSessionStore) Delete ¶
func (r *RedisSessionStore) Delete(ctx context.Context, key string) error
type Session ¶
type Session interface { // Get by key, returning the value and whether the key exists. Get(key string) (interface{}, bool) // Set key-value pair Set(key, value string) error // Save saves all the key-value pairs set before Save(ctx context.Context) error // get session id SessionId() string }
Session represents session which can get and put data into. You can call Get and Set any times but nothing would be store until Save is called.
func CreateSession ¶
func CreateSession(sessionIdGetter SessionIdGetter, sessionIdSetter SessionIdSetter, options []SessionOptions) (Session, error)
CreateSession gets the session store associated with sessionId which can be extracted by SessionIdGetter. If the session doesn't exist, create a new one and set the sessionId with SessionIdSetter. You can specify the store strategy by specify session options. By default, if WithRedisClusters option specifies the redis cluster, RedisSessionStore is used to store the session. Otherwise, InMemorySessionStore is used. You can implement your custom store strategy and specify it by WithSessionStore options.
sessionIdGetter may not be nil, while sessionIdSetter can be.
type SessionIdGetter ¶
type SessionIdGetter func() string
custom session id getter, such as fetched from http cookies.
type SessionIdSetter ¶
type SessionIdSetter func(string)
custom session id setter, such as storing in http cookies.
type SessionOptions ¶
type SessionOptions interface {
// contains filtered or unexported methods
}
func WithExpiration ¶
func WithExpiration(expiration time.Duration) SessionOptions
WithExpiration specifies the session expiration time. Session would be removed when the expiration time passes.
func WithRedisClusters ¶
func WithRedisClusters(clusters []string) SessionOptions
WithRedisClusters specifies redis clusters. If redis only contains one node, put it in the slice whose length is 1.
func WithRedisTimeout ¶
func WithRedisTimeout(timeout time.Duration) SessionOptions
WithRedisTimeout specifies the redis timeout
func WithSessionStore ¶ added in v1.0.3
func WithSessionStore(store SessionStore) SessionOptions
WithSessionStore specifies custom session store implementation. By default, if redis clusters are specified, RedisSessionStore would be used which is implemented based on redis. Otherwise, InMemorySessionStore would be used, which is implemented in memory. You can implement your session store using other persistent strategy such as database.
type SessionStore ¶
type SessionStore interface { // Get gets the value of specified key, returning the value and error if any. Get(ctx context.Context, key string) (string, error) // Set adds the key-value pair with expiration specified. If key exists, nothing would happen. Set(ctx context.Context, key string, value string, expiration time.Duration) error // Delete deletes the key. If key doesn't exist, do nothing. Delete(ctx context.Context, key string) error }
SessionStore represents the store strategy of session, which can be mysql, in memory or redis etc.
func NewInMemorySessionStore ¶ added in v1.0.3
func NewInMemorySessionStore() SessionStore
func NewRedisSessionStore ¶
func NewRedisSessionStore(options *sessionOptions) (SessionStore, error)