Esempio n. 1
0
        public async Task <CommunityGoal> GetAsync(int id, CancellationToken cancellationToken = default)
        {
            await _lock.WaitAsync(cancellationToken).ConfigureAwait(false);

            try
            {
                CommunityGoal  result;
                CachingOptions cachingOptions = _cachingOptions.Get(CacheOptionName);
                if (cachingOptions.Enabled && (result = _cgCache.Get(id)) != null)
                {
                    _log.LogTrace("Community goal {ID} ({Name}) found in cache", id, result.Name);
                    return(result);
                }

                // get from DB
                _log.LogTrace("Retrieving community goal {ID} from history database", id);
                result = await _collection.Find(dbData => dbData.ID == id).FirstOrDefaultAsync(cancellationToken).ConfigureAwait(false);

                if (result != null)
                {
                    _cgCache.AddOrReplace(result.ID, result, cachingOptions.Lifetime);
                }
                return(result);
            }
            finally
            {
                _lock.Release();
            }
        }
Esempio n. 2
0
        public async Task <UserData> GetAsync(ulong userID, CancellationToken cancellationToken = default)
        {
            await _lock.WaitAsync(cancellationToken).ConfigureAwait(false);

            try
            {
                UserData       result;
                CachingOptions cachingOptions = _cachingOptions.Get(CacheOptionName);
                if (cachingOptions.Enabled && (result = _userDataCache.Get(userID)) != null)
                {
                    _log.LogTrace("User data for user {UserID} found in cache", userID);
                    return(result);
                }

                // get from DB
                _log.LogTrace("Retrieving user data for user {UserID} from database", userID);
                result = await _collection.Find(dbData => dbData.ID == userID).FirstOrDefaultAsync(cancellationToken).ConfigureAwait(false);

                // if not found, return default data
                if (result == null)
                {
                    _log.LogTrace("User data for user {UserID} not found, creating new with defaults", userID);
                    result = new UserData(userID);
                }

                _userDataCache.AddOrReplace(result.ID, result, cachingOptions.Lifetime);
                return(result);
            }
            finally
            {
                _lock.Release();
            }
        }
        public async Task <PatchbotGame> GetAsync(string name, CancellationToken cancellationToken = default)
        {
            string trimmedName   = name.Trim();
            string lowercaseName = trimmedName.ToLowerInvariant();
            await _lock.WaitAsync(cancellationToken).ConfigureAwait(false);

            try
            {
                PatchbotGame   result;
                CachingOptions cachingOptions = _cachingOptions.Get(CacheOptionName);
                if (cachingOptions.Enabled)
                {
                    result = _patchbotGameCache.Find(e => e.Entity.MatchesName(trimmedName)).FirstOrDefault();
                    if (result != null)
                    {
                        _log.LogTrace("Patchbot game {Game} found in cache", trimmedName);
                        return(result);
                    }
                }

                // get from DB
                _log.LogTrace("Retrieving patchbot game {Game} from database", trimmedName);
                FilterDefinition <PatchbotGame> filter = Builders <PatchbotGame> .Filter.Or(
                    Builders <PatchbotGame> .Filter.Regex(dbData => dbData.Name, new BsonRegularExpression($"/^{trimmedName}$/i")),
                    Builders <PatchbotGame> .Filter.AnyEq(dbData => dbData.Aliases, lowercaseName));

                result = await _collection.Find(filter).FirstOrDefaultAsync(cancellationToken).ConfigureAwait(false);

                // if not found, return null
                if (result == null)
                {
                    _log.LogTrace("Patchbot game {Game} not found", trimmedName);
                    return(null);
                }

                _patchbotGameCache.AddOrReplace(result.Name, result, cachingOptions.Lifetime);
                return(result);
            }
            finally
            {
                _lock.Release();
            }
        }
        public async Task AddAsync(StellarisMod mod, CancellationToken cancellationToken = default)
        {
            await _lock.WaitAsync(cancellationToken).ConfigureAwait(false);

            try
            {
                _log.LogDebug("Adding mod {Name} to database", mod.Name);
                await _collection.InsertOneAsync(mod, null, cancellationToken).ConfigureAwait(false);

                if (_cachingOptions.CurrentValue.Enabled)
                {
                    _stellarisModsCache.AddOrReplace(mod.ID, mod, _cachingOptions.CurrentValue.Lifetime);
                }
            }
            finally
            {
                _lock.Release();
            }
        }