Пример #1
0
        public async Task <NetflixAccount> GetAsync(CancellationToken cancellationToken = default)
        {
            await _lock.WaitAsync(cancellationToken).ConfigureAwait(false);

            try
            {
                CachingOptions cachingOptions = _cachingOptions.Get(CacheOptionName);
                if (cachingOptions.Enabled && _cachedAccount != null && !_cachedAccount.IsExpired)
                {
                    _log.LogTrace("Netflix account found in cache");
                    return(_cachedAccount);
                }

                _log.LogDebug("Retrieving Netflix account from database");
                NetflixAccount result = await _netflixAccountsCollection.Find(_ => true).FirstOrDefaultAsync(cancellationToken).ConfigureAwait(false);

                if (result == null)
                {
                    _log.LogTrace("Netflix account not found, creating default");
                    result = new NetflixAccount();
                }

                if (cachingOptions.Enabled)
                {
                    _cachedAccount = new CachedEntity <string, NetflixAccount>(result.Login, result, cachingOptions.Lifetime);
                }
                return(result);
            }
            finally
            {
                _lock.Release();
            }
        }
Пример #2
0
        public async Task SetAsync(NetflixAccount account, CancellationToken cancellationToken = default)
        {
            await _lock.WaitAsync(cancellationToken).ConfigureAwait(false);

            try
            {
                CachingOptions cachingOptions = _cachingOptions.Get(CacheOptionName);
                if (cachingOptions.Enabled)
                {
                    _log.LogTrace("Updating cached Netflix account");
                    _cachedAccount = new CachedEntity <string, NetflixAccount>(account.Login, account, cachingOptions.Lifetime);
                }

                _log.LogTrace("Saving Netflix account in the database");
                await _netflixAccountsCollection.ReplaceOneAsync(_ => true, account, _replaceOptions, cancellationToken).ConfigureAwait(false);
            }
            finally
            {
                _lock.Release();
            }
        }