photoprism/internal/session/store.go
Michael Mayer faa1ec2e4b Sharing: Implement session update #18
Signed-off-by: Michael Mayer <michael@liquidbytes.net>
2020-06-24 17:40:09 +02:00

68 lines
1.3 KiB
Go

package session
import (
"encoding/json"
"fmt"
"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 (create)", err)
}
return token
}
func (s *Session) Update(token string, data interface{}) error {
if _, found := s.cache.Get(token); !found {
return fmt.Errorf("session: %s not found (update)", token)
}
s.cache.Set(token, data, gc.DefaultExpiration)
log.Debugf("session: updated")
if err := s.Save(); err != nil {
return fmt.Errorf("session: %s (update)", err.Error())
}
return nil
}
func (s *Session) Delete(token string) {
s.cache.Delete(token)
log.Debugf("session: deleted")
if err := s.Save(); err != nil {
log.Errorf("session: %s (delete)", 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
}