go get -u github.com/nyan2d/safsm
We have several session storage types: MemoryStorage, FileStorage, SQLiteStorage.
But you can implement your own type using the interface below:
type Storage interface {
Close() error
InsertSession(session *Session) error
FindSession(token string) (*Session, error)
RemoveSession(token string) error
Each(f func(session *Session))
}
// open sqlite database
db, err := sql.Open("sqlite", "sessions.db")
if err != nil {
...
}
// create storage
storage, err := safsm.NewSQLiteStorage(db)
if err != nil {
...
}
// create session manager
sm := safsm.New(storage)
ttl := time.Minute * 60
session := sm.CreateSession(userID, ttl)
func demoHandler (w http.ResponseWriter, r *http.Request) {
session.SetCookie(w)
}
func demoHandler (w http.ResponseWriter, r *http.Request) {
session, err := safsm.ReadSession(r)
if err == safsm.ErrNoSession {
// there is no session
}
}
Caching is implemented by a storage-wrapper with the type CachedStorage
cacheSize := 1024
cachedStorage := safsm.NewCachedStorage(originalStorage, cacheSize)
Warning! Cache invalidation works very simply: if the number of sessions hit by the cache exceeds the cache size, the entire cache is just invalidated.
This behavior can be reworked in the future.
If you want to save a session from a storage you work with, you need to assign the session to some session manager
session.AssignTo(sm)
session.Save()
Some of session funcs can be chained. For example:
session.AssignTo(sm).
Update().
SetTTL(time.Minute * 60).
Save()
A session has the following time parameters:
- CreatedAt
- UpdatedAt
- TTL
If the current time exceeds the session update time + TTL, the session is considered invalid. If you need to update the session, you can use the following approach:
if session.TimeToLive().Minutes() < 60 {
session.Update().AssignTo(sm).Save()
}
The code is distributed under the MIT license.