1
0
mirror of https://github.com/hrfee/jfa-go.git synced 2024-12-22 17:10:10 +00:00

added user caching with 30m timeout

This commit is contained in:
Harvey Tindall 2020-08-16 16:08:37 +01:00
parent 252e13757b
commit 8750efe101
Signed by: hrfee
GPG Key ID: BBC65952848FB1A2

View File

@ -37,6 +37,9 @@ type Jellyfin struct {
userId string
httpClient *http.Client
loginParams map[string]string
userCache []map[string]interface{}
cacheExpiry time.Time
cacheLength int
}
func (jf *Jellyfin) timeoutHandler() {
@ -73,6 +76,8 @@ func (jf *Jellyfin) init(server, client, version, device, deviceId string) error
data, _ := ioutil.ReadAll(resp.Body)
json.Unmarshal(data, &jf.serverInfo)
}
jf.cacheLength = 30
jf.cacheExpiry = time.Now()
return nil
}
@ -199,6 +204,7 @@ func (jf *Jellyfin) getUsers(public bool) ([]map[string]interface{}, int, error)
var data io.Reader
var status int
var err error
if time.Now().After(jf.cacheExpiry) {
if public {
url := fmt.Sprintf("%s/emby/Users/Public", jf.server)
data, status, err = jf._getReader(url, nil)
@ -211,8 +217,11 @@ func (jf *Jellyfin) getUsers(public bool) ([]map[string]interface{}, int, error)
return nil, status, err
}
json.NewDecoder(data).Decode(&result)
jf.userCache = result
jf.cacheExpiry = time.Now().Add(time.Minute * time.Duration(jf.cacheLength))
return result, status, nil
}
return jf.userCache, 200, nil
}
func (jf *Jellyfin) userByName(username string, public bool) (map[string]interface{}, int, error) {
@ -229,6 +238,13 @@ func (jf *Jellyfin) userByName(username string, public bool) (map[string]interfa
}
func (jf *Jellyfin) userById(userId string, public bool) (map[string]interface{}, int, error) {
if jf.cacheExpiry.After(time.Now()) {
for _, user := range jf.userCache {
if user["Id"].(string) == userId {
return user, 200, nil
}
}
}
if public {
users, status, err := jf.getUsers(public)
if err != nil || status != 200 {