コード例 #1
0
        /// <remarks>
        /// Possibly refreshes the internal cache by calling OnBeforeAccessAsync and OnAfterAccessAsync delegates.
        /// </remarks>
        private async Task RefreshCacheForReadOperationsAsync(CacheEvent.TokenTypes cacheEventType)
        {
            if (TokenCacheInternal.IsTokenCacheSerialized())
            {
                if (!_cacheRefreshedForRead)
                {
                    string telemetryId = _requestParams.RequestContext.CorrelationId.AsMatsCorrelationId();
                    var    cacheEvent  = new CacheEvent(CacheEvent.TokenCacheLookup, telemetryId)
                    {
                        TokenType = cacheEventType
                    };

                    await TokenCacheInternal.Semaphore.WaitAsync().ConfigureAwait(false);

                    try
                    {
                        if (!_cacheRefreshedForRead) // double check locking
                        {
                            using (_requestParams.RequestContext.CreateTelemetryHelper(cacheEvent))
                            {
                                string key = SuggestedWebCacheKeyFactory.GetKeyFromRequest(_requestParams);

                                try
                                {
                                    var args = new TokenCacheNotificationArgs(
                                        TokenCacheInternal,
                                        _requestParams.ClientId,
                                        _requestParams.Account,
                                        hasStateChanged: false,
                                        TokenCacheInternal.IsApplicationCache,
                                        hasTokens: TokenCacheInternal.HasTokensNoLocks(),
                                        suggestedCacheKey: key);
                                    await TokenCacheInternal.OnBeforeAccessAsync(args).ConfigureAwait(false);
                                }
                                finally
                                {
                                    var args = new TokenCacheNotificationArgs(
                                        TokenCacheInternal,
                                        _requestParams.ClientId,
                                        _requestParams.Account,
                                        hasStateChanged: false,
                                        TokenCacheInternal.IsApplicationCache,
                                        hasTokens: TokenCacheInternal.HasTokensNoLocks(),
                                        suggestedCacheKey: key);
                                    await TokenCacheInternal.OnAfterAccessAsync(args).ConfigureAwait(false);
                                }

                                _cacheRefreshedForRead = true;
                            }
                        }
                    }
                    finally
                    {
                        TokenCacheInternal.Semaphore.Release();
                    }
                }
            }
        }
        /// <remarks>
        /// Possibly refreshes the internal cache by calling OnBeforeAccessAsync and OnAfterAccessAsync delegates.
        /// </remarks>
        private async Task RefreshCacheForReadOperationsAsync(CacheEvent.TokenTypes cacheEventType)
        {
            if (TokenCacheInternal.IsTokenCacheSerialized())
            {
                if (!_cacheRefreshedForRead)
                {
                    var cacheEvent = new CacheEvent(CacheEvent.TokenCacheLookup, _requestParams.RequestContext.CorrelationId.AsMatsCorrelationId())
                    {
                        TokenType = cacheEventType
                    };

                    _requestParams.RequestContext.Logger.Verbose("[Cache Session Manager] Waiting for cache semaphore");
                    await TokenCacheInternal.Semaphore.WaitAsync().ConfigureAwait(false);

                    _requestParams.RequestContext.Logger.Verbose("[Cache Session Manager] Entered cache semaphore");

                    Stopwatch stopwatch = new Stopwatch();
                    try
                    {
                        if (!_cacheRefreshedForRead) // double check locking
                        {
                            using (_requestParams.RequestContext.CreateTelemetryHelper(cacheEvent))
                            {
                                string key = SuggestedWebCacheKeyFactory.GetKeyFromRequest(_requestParams);

                                try
                                {
                                    var args = new TokenCacheNotificationArgs(
                                        TokenCacheInternal,
                                        _requestParams.AppConfig.ClientId,
                                        _requestParams.Account,
                                        hasStateChanged: false,
                                        TokenCacheInternal.IsApplicationCache,
                                        hasTokens: TokenCacheInternal.HasTokensNoLocks(),
                                        _requestParams.RequestContext.UserCancellationToken,
                                        suggestedCacheKey: key);

                                    stopwatch.Start();
                                    await TokenCacheInternal.OnBeforeAccessAsync(args).ConfigureAwait(false);

                                    RequestContext.ApiEvent.DurationInCacheInMs += stopwatch.ElapsedMilliseconds;
                                }
                                finally
                                {
                                    var args = new TokenCacheNotificationArgs(
                                        TokenCacheInternal,
                                        _requestParams.AppConfig.ClientId,
                                        _requestParams.Account,
                                        hasStateChanged: false,
                                        TokenCacheInternal.IsApplicationCache,
                                        hasTokens: TokenCacheInternal.HasTokensNoLocks(),
                                        _requestParams.RequestContext.UserCancellationToken,
                                        suggestedCacheKey: key);

                                    stopwatch.Reset();
                                    stopwatch.Start();
                                    await TokenCacheInternal.OnAfterAccessAsync(args).ConfigureAwait(false);

                                    RequestContext.ApiEvent.DurationInCacheInMs += stopwatch.ElapsedMilliseconds;
                                }

                                _cacheRefreshedForRead = true;
                            }
                        }
                    }
                    finally
                    {
                        TokenCacheInternal.Semaphore.Release();
                        _requestParams.RequestContext.Logger.Verbose("[Cache Session Manager] Released cache semaphore");
                    }
                }
            }
        }