Esempio n. 1
0
        public List <SteamUser> GetBasicDetailsByIDs(IEnumerable <long> ids)
        {
            List <SteamUser>               output         = new List <SteamUser>();
            List <long>                    allIdsToUpdate = new List <long>();
            List <long>                    missingUserIDs = new List <long>();
            DataCacheRepository            repo           = _uow.GetRepo <DataCacheRepository>();
            Dictionary <string, DataCache> dictionary     = repo.Get(ids.ToList(), CacheType.SteamUserBasicDetails).ToDictionary(x => x.EntityID);

            foreach (long id in ids)
            {
                DataCache cache = null;
                if (dictionary.ContainsKey(id.ToString()))
                {
                    cache = dictionary[id.ToString()];
                }
                if (cache != null && cache.StoredOn > DateTime.Now.AddHours(-1))
                {
                    SteamUser user = cache.Deserialize <SteamUser>();
                    output.Add(user);
                }
                else
                {
                    allIdsToUpdate.Add(id);
                    if (cache == null)
                    {
                        missingUserIDs.Add(id);
                    }
                }
            }
            if (allIdsToUpdate.SafeAny())
            {
                List <SteamUser> missingUsers = _steamApi.GetPlayerSummaries(allIdsToUpdate);
                foreach (SteamUser user in missingUsers)
                {
                    DataCache cache = new DataCache {
                        EntityID    = user.ID.ToString(),
                        CacheTypeID = CacheType.SteamUserBasicDetails,
                        StoredOn    = DateTime.Now,
                        JsonData    = JsonConvert.SerializeObject(user)
                    };
                    if (missingUserIDs.Contains(user.ID))
                    {
                        repo.Add(cache);
                    }
                    else
                    {
                        repo.Update(cache);
                    }
                }
                output.AddRange(missingUsers);
            }
            return(output);
        }