@@ -3,6 +3,7 @@ package services
33import (
44 "bytes"
55 "context"
6+ "crypto/sha256"
67 "encoding/json"
78 "errors"
89 "expo-open-ota/config"
@@ -258,6 +259,24 @@ func FetchExpoBranches() ([]string, error) {
258259}
259260
260261func FetchExpoUserAccountInformations (expoAuth types.ExpoAuth ) (* ExpoUserAccount , error ) {
262+ cache := cache2 .GetCache ()
263+ var cacheKey string
264+ if expoAuth .Token != nil {
265+ h := sha256 .Sum256 ([]byte (* expoAuth .Token ))
266+ cacheKey = fmt .Sprintf ("expoUserAccount:token:%x" , h )
267+ } else if expoAuth .SessionSecret != nil {
268+ h := sha256 .Sum256 ([]byte (* expoAuth .SessionSecret ))
269+ cacheKey = fmt .Sprintf ("expoUserAccount:session:%x" , h )
270+ }
271+ if cacheKey != "" {
272+ if cachedValue := cache .Get (cacheKey ); cachedValue != "" {
273+ var account ExpoUserAccount
274+ if err := json .Unmarshal ([]byte (cachedValue ), & account ); err == nil {
275+ return & account , nil
276+ }
277+ }
278+ }
279+
261280 query := `
262281 query GetCurrentUserAccount {
263282 me {
@@ -284,17 +303,31 @@ func FetchExpoUserAccountInformations(expoAuth types.ExpoAuth) (*ExpoUserAccount
284303 return nil , err
285304 }
286305
306+ if cacheKey != "" {
307+ if cacheValue , err := json .Marshal (resp .Data .Me ); err == nil {
308+ ttl := 300
309+ _ = cache .Set (cacheKey , string (cacheValue ), & ttl )
310+ }
311+ }
312+
287313 return & resp .Data .Me , nil
288314}
289315
290316func FetchSelfExpoUsername () string {
317+ cache := cache2 .GetCache ()
291318 token := GetExpoAccessToken ()
319+ cacheKey := fmt .Sprintf ("selfExpoUsername:%s:%x" , version .Version , sha256 .Sum256 ([]byte (token )))
320+ if cachedValue := cache .Get (cacheKey ); cachedValue != "" {
321+ return cachedValue
322+ }
292323 expoAccount , err := FetchExpoUserAccountInformations (types.ExpoAuth {
293324 Token : & token ,
294325 })
295326 if err != nil {
296327 return ""
297328 }
329+ ttl := 86400
330+ _ = cache .Set (cacheKey , expoAccount .Username , & ttl )
298331 return expoAccount .Username
299332}
300333
0 commit comments