photoprism/internal/session/save.go
Eng Zer Jun 44f7700c0c
Enable module graph pruning and deprecate io/ioutil (#1600)
* Backend: Enable Go module graph pruning and lazy module loading

This commit applies the changes by running `go mod tidy -go=1.17` to
enable module graph pruning and lazy module loading supported by Go 1.17
or higher.

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* Backend: Move from io/ioutil to io and os package

The io/ioutil package has been deprecated as of Go 1.16, see
https://golang.org/doc/go1.16#ioutil. This commit replaces the existing
io/ioutil functions with their new definitions in io and os packages.

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2021-10-06 07:10:50 +02:00

101 lines
2.1 KiB
Go

package session
import (
"encoding/json"
"os"
"path"
"sync"
"time"
gc "github.com/patrickmn/go-cache"
"github.com/photoprism/photoprism/internal/entity"
)
const cacheFileName = "sessions.json"
var fileMutex sync.RWMutex
// New returns a new session store with an optional cachePath.
func New(expiration time.Duration, cachePath string) *Session {
s := &Session{}
cleanupInterval := 15 * time.Minute
if cachePath != "" {
fileMutex.RLock()
defer fileMutex.RUnlock()
var savedItems map[string]Saved
items := make(map[string]gc.Item)
s.cacheFile = path.Join(cachePath, cacheFileName)
if cached, err := os.ReadFile(s.cacheFile); err != nil {
log.Debugf("session: %s", err)
} else if err := json.Unmarshal(cached, &savedItems); err != nil {
log.Errorf("session: %s", err)
} else {
for key, saved := range savedItems {
user := entity.FindUserByUID(saved.User)
if user == nil {
continue
}
var tokens []string
var shared []string
for _, token := range saved.Tokens {
links := entity.FindValidLinks(token, "")
if len(links) > 0 {
for _, link := range links {
shared = append(shared, link.LinkUID)
}
tokens = append(tokens, token)
}
}
data := Data{User: *user, Tokens: tokens, Shares: shared}
items[key] = gc.Item{Expiration: saved.Expiration, Object: data}
}
s.cache = gc.NewFrom(expiration, cleanupInterval, items)
}
}
if s.cache == nil {
s.cache = gc.New(expiration, cleanupInterval)
}
return s
}
// Save stores all sessions in a JSON file.
func (s *Session) Save() error {
if s.cacheFile == "" {
return nil
}
fileMutex.Lock()
defer fileMutex.Unlock()
items := s.cache.Items()
savedItems := make(map[string]Saved, len(items))
for key, item := range items {
saved := item.Object.(Data).Saved()
saved.Expiration = item.Expiration
savedItems[key] = saved
}
if serialized, err := json.MarshalIndent(savedItems, "", " "); err != nil {
return err
} else if err = os.WriteFile(s.cacheFile, serialized, 0600); err != nil {
return err
}
return nil
}