Sessions: Change log levels & add docs #1103

This commit is contained in:
Michael Mayer 2021-04-30 10:07:20 +02:00
parent 8fee92feac
commit 6ca3dde25f
3 changed files with 12 additions and 4 deletions

View file

@ -11,6 +11,8 @@ import (
"github.com/photoprism/photoprism/internal/entity"
)
const cacheFileName = "sessions.json"
var fileMutex sync.RWMutex
// New returns a new session store with an optional cachePath.
@ -26,10 +28,10 @@ func New(expiration time.Duration, cachePath string) *Session {
var savedItems map[string]Saved
items := make(map[string]gc.Item)
s.cacheFile = path.Join(cachePath, "sessions.json")
s.cacheFile = path.Join(cachePath, cacheFileName)
if cached, err := ioutil.ReadFile(s.cacheFile); err != nil {
log.Infof("session: %s", err)
log.Debugf("session: %s", err)
} else if err := json.Unmarshal(cached, &savedItems); err != nil {
log.Errorf("session: %s", err)
} else {
@ -70,7 +72,7 @@ func New(expiration time.Duration, cachePath string) *Session {
return s
}
// Stores all sessions in a JSON file.
// Save stores all sessions in a JSON file.
func (s *Session) Save() error {
if s.cacheFile == "" {
return nil

View file

@ -6,6 +6,7 @@ import (
gc "github.com/patrickmn/go-cache"
)
// Create creates a new user session.
func (s *Session) Create(data Data) string {
id := NewID()
s.cache.Set(id, data, gc.DefaultExpiration)
@ -18,6 +19,7 @@ func (s *Session) Create(data Data) string {
return id
}
// Update updates the data of an existing user session.
func (s *Session) Update(id string, data Data) error {
if id == "" {
return fmt.Errorf("session: empty id")
@ -32,12 +34,13 @@ func (s *Session) Update(id string, data Data) error {
log.Debugf("session: updated")
if err := s.Save(); err != nil {
return fmt.Errorf("session: %s (update)", err.Error())
log.Errorf("session: %s (update)", err)
}
return nil
}
// Delete deletes an existing user session.
func (s *Session) Delete(id string) {
s.cache.Delete(id)
log.Debugf("session: deleted")
@ -47,6 +50,7 @@ func (s *Session) Delete(id string) {
}
}
// Get returns the data of an existing user session.
func (s *Session) Get(id string) Data {
if id == "" {
return Data{}
@ -59,6 +63,7 @@ func (s *Session) Get(id string) Data {
return Data{}
}
// Exists tests of a user session with the given id exists.
func (s *Session) Exists(id string) bool {
_, found := s.cache.Get(id)

View file

@ -5,6 +5,7 @@ import (
"fmt"
)
// NewID returns a random session id.
func NewID() string {
b := make([]byte, 24)