photoprism/internal/session/store.go
Michael Mayer 4bd0ca4ac6 Keep sessions for 7 days
Signed-off-by: Michael Mayer <michael@liquidbytes.net>
2020-04-20 13:50:28 +02:00

51 lines
983 B
Go

package session
import (
"encoding/json"
"io/ioutil"
gc "github.com/patrickmn/go-cache"
)
func (s *Session) Create(data interface{}) string {
token := Token()
s.cache.Set(token, data, gc.DefaultExpiration)
log.Debugf("session: created")
if err := s.Save(); err != nil {
log.Errorf("session: %s", err)
}
return token
}
func (s *Session) Delete(token string) {
s.cache.Delete(token)
log.Debugf("session: deleted")
if err := s.Save(); err != nil {
log.Errorf("session: %s", err)
}
}
func (s *Session) Get(token string) (data interface{}, exists bool) {
return s.cache.Get(token)
}
func (s *Session) Exists(token string) bool {
_, found := s.cache.Get(token)
return found
}
func (s *Session) Save() error {
if s.cacheFile == "" {
return nil
} else if serialized, err := json.MarshalIndent(s.cache.Items(), "", " "); err != nil {
return err
} else if err = ioutil.WriteFile(s.cacheFile, serialized, 0600); err != nil {
return err
}
return nil
}