mirror of
https://github.com/hrfee/jfa-go.git
synced 2024-12-23 01:20:11 +00:00
added user caching with 30m timeout
This commit is contained in:
parent
252e13757b
commit
8750efe101
18
jfapi.go
18
jfapi.go
@ -37,6 +37,9 @@ type Jellyfin struct {
|
|||||||
userId string
|
userId string
|
||||||
httpClient *http.Client
|
httpClient *http.Client
|
||||||
loginParams map[string]string
|
loginParams map[string]string
|
||||||
|
userCache []map[string]interface{}
|
||||||
|
cacheExpiry time.Time
|
||||||
|
cacheLength int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (jf *Jellyfin) timeoutHandler() {
|
func (jf *Jellyfin) timeoutHandler() {
|
||||||
@ -73,6 +76,8 @@ func (jf *Jellyfin) init(server, client, version, device, deviceId string) error
|
|||||||
data, _ := ioutil.ReadAll(resp.Body)
|
data, _ := ioutil.ReadAll(resp.Body)
|
||||||
json.Unmarshal(data, &jf.serverInfo)
|
json.Unmarshal(data, &jf.serverInfo)
|
||||||
}
|
}
|
||||||
|
jf.cacheLength = 30
|
||||||
|
jf.cacheExpiry = time.Now()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -199,6 +204,7 @@ func (jf *Jellyfin) getUsers(public bool) ([]map[string]interface{}, int, error)
|
|||||||
var data io.Reader
|
var data io.Reader
|
||||||
var status int
|
var status int
|
||||||
var err error
|
var err error
|
||||||
|
if time.Now().After(jf.cacheExpiry) {
|
||||||
if public {
|
if public {
|
||||||
url := fmt.Sprintf("%s/emby/Users/Public", jf.server)
|
url := fmt.Sprintf("%s/emby/Users/Public", jf.server)
|
||||||
data, status, err = jf._getReader(url, nil)
|
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
|
return nil, status, err
|
||||||
}
|
}
|
||||||
json.NewDecoder(data).Decode(&result)
|
json.NewDecoder(data).Decode(&result)
|
||||||
|
jf.userCache = result
|
||||||
|
jf.cacheExpiry = time.Now().Add(time.Minute * time.Duration(jf.cacheLength))
|
||||||
return result, status, nil
|
return result, status, nil
|
||||||
|
}
|
||||||
|
return jf.userCache, 200, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (jf *Jellyfin) userByName(username string, public bool) (map[string]interface{}, int, error) {
|
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) {
|
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 {
|
if public {
|
||||||
users, status, err := jf.getUsers(public)
|
users, status, err := jf.getUsers(public)
|
||||||
if err != nil || status != 200 {
|
if err != nil || status != 200 {
|
||||||
|
Loading…
Reference in New Issue
Block a user