internal IChangeToken GetOrAddFilePathChangeToken(string filePath)
        {
            if (!_filePathTokenLookup.TryGetValue(filePath, out var tokenInfo))
            {
                var cancellationTokenSource = new CancellationTokenSource();
                var cancellationChangeToken = new CancellationChangeToken(cancellationTokenSource.Token);
                tokenInfo = new ChangeTokenInfo(cancellationTokenSource, cancellationChangeToken);
                tokenInfo = _filePathTokenLookup.GetOrAdd(filePath, tokenInfo);
            }

            IChangeToken changeToken = tokenInfo.ChangeToken;

            if (PollForChanges)
            {
                // The expiry of CancellationChangeToken is controlled by this type and consequently we can cache it.
                // PollingFileChangeToken on the other hand manages its own lifetime and consequently we cannot cache it.
                var pollingChangeToken = new PollingFileChangeToken(new FileInfo(Path.Combine(_root, filePath)));

                if (UseActivePolling)
                {
                    pollingChangeToken.ActiveChangeCallbacks   = true;
                    pollingChangeToken.CancellationTokenSource = new CancellationTokenSource();
                    PollingChangeTokens.TryAdd(pollingChangeToken, pollingChangeToken);
                }

                changeToken = new CompositeChangeToken(
                    new[]
                {
                    changeToken,
                    pollingChangeToken,
                });
            }

            return(changeToken);
        }
        private IChangeToken GetOrAddFilePathChangeToken(string filePath)
        {
            ChangeTokenInfo tokenInfo;

            if (!_filePathTokenLookup.TryGetValue(filePath, out tokenInfo))
            {
                var cancellationTokenSource = new CancellationTokenSource();
                var cancellationChangeToken = new CancellationChangeToken(cancellationTokenSource.Token);
                tokenInfo = new ChangeTokenInfo(cancellationTokenSource, cancellationChangeToken);
                tokenInfo = _filePathTokenLookup.GetOrAdd(filePath, tokenInfo);
            }

            IChangeToken changeToken = tokenInfo.ChangeToken;

            if (_pollForChanges)
            {
                // The expiry of CancellationChangeToken is controlled by this type and consequently we can cache it.
                // PollingFileChangeToken on the other hand manages its own lifetime and consequently we cannot cache it.
                changeToken = new CompositeChangeToken(
                    new[]
                {
                    changeToken,
                    new PollingFileChangeToken(new FileInfo(Path.Combine(_root, filePath)))
                });
            }

            return(changeToken);
        }
        private IChangeToken GetOrAddWildcardChangeToken(string pattern)
        {
            ChangeTokenInfo tokenInfo;

            if (!_wildcardTokenLookup.TryGetValue(pattern, out tokenInfo))
            {
                var cancellationTokenSource = new CancellationTokenSource();
                var cancellationChangeToken = new CancellationChangeToken(cancellationTokenSource.Token);
                var matcher = new Matcher(StringComparison.OrdinalIgnoreCase);
                matcher.AddInclude(pattern);
                tokenInfo = new ChangeTokenInfo(cancellationTokenSource, cancellationChangeToken, matcher);
                tokenInfo = _wildcardTokenLookup.GetOrAdd(pattern, tokenInfo);
            }

            IChangeToken changeToken = tokenInfo.ChangeToken;

            if (_pollForChanges)
            {
                // The expiry of CancellationChangeToken is controlled by this type and consequently we can cache it.
                // PollingFileChangeToken on the other hand manages its own lifetime and consequently we cannot cache it.
                changeToken = new CompositeChangeToken(
                    new[]
                {
                    changeToken,
                    new PollingWildCardChangeToken(_root, pattern)
                });
            }

            return(changeToken);
        }
示例#4
0
        public CacheDependency(string[] filePaths = null, string[] cacheKeys = null)
        {
            var tokenList = new List <IChangeToken>();

            //add the key tokens
            if (cacheKeys != null)
            {
                foreach (string cacheKey in cacheKeys)
                {
                    if (string.IsNullOrEmpty(cacheKey))
                    {
                        continue;
                    }

                    CancellationTokenSource tokenSource = null;
                    if (!AgilityCache.KeyTokens.TryGetValue(cacheKey, out tokenSource))
                    {
                        //Add a cancellation token for this item
                        tokenSource = new CancellationTokenSource();

                        this.TokenSources[cacheKey] = tokenSource;
                    }

                    //TODO: figure out what to do if the item we want a dependancy on isn't in cache yet...
                    var token = new CancellationChangeToken(tokenSource.Token);
                    tokenList.Add(token);
                }
            }

            //add the file tokens
            if (filePaths != null)
            {
                foreach (string filePath in filePaths)
                {
                    //unix vs windows
                    var lastSlash = filePath.LastIndexOf('/');

                    var fileName = filePath.Substring(lastSlash + 1);
                    if (fileName.StartsWith(fileProvider.Root))
                    {
                        fileName = fileName.Substring(fileProvider.Root.Length);
                    }

                    var changeToken = fileProvider.Watch(fileName);

                    tokenList.Add(changeToken);
                }
            }

            ChangeToken = new CompositeChangeToken(tokenList);
        }
示例#5
0
    internal static void Composite()
    {
        // <Composites>
        CancellationTokenSource firstCancellationTokenSource = new();
        CancellationChangeToken firstCancellationChangeToken = new(firstCancellationTokenSource.Token);

        CancellationTokenSource secondCancellationTokenSource = new();
        CancellationChangeToken secondCancellationChangeToken = new(secondCancellationTokenSource.Token);

        CancellationTokenSource thirdCancellationTokenSource = new();
        CancellationChangeToken thirdCancellationChangeToken = new(thirdCancellationTokenSource.Token);

        var compositeChangeToken =
            new CompositeChangeToken(
                new IChangeToken[]
        {
            firstCancellationChangeToken,
            secondCancellationChangeToken,
            thirdCancellationChangeToken
        });

        Action <object?> callback = state => Console.WriteLine($"The {state} callback was invoked.");

        // 1st, 2nd, 3rd, and 4th.
        compositeChangeToken.RegisterChangeCallback(callback, "1st");
        compositeChangeToken.RegisterChangeCallback(callback, "2nd");
        compositeChangeToken.RegisterChangeCallback(callback, "3rd");
        compositeChangeToken.RegisterChangeCallback(callback, "4th");

        // It doesn't matter which cancellation source triggers the change.
        // If more than one trigger the change, each callback is only fired once.
        Random random = new();
        int    index  = random.Next(3);

        CancellationTokenSource[] sources = new[]
        {
            firstCancellationTokenSource,
            secondCancellationTokenSource,
            thirdCancellationTokenSource
        };
        sources[index].Cancel();

        Console.WriteLine();

        // Outputs:
        //     The 4th callback was invoked.
        //     The 3rd callback was invoked.
        //     The 2nd callback was invoked.
        //     The 1st callback was invoked.
        // </Composites>
    }
示例#6
0
        public CompositeDispatcherDataSource(IEnumerable <DispatcherDataSource> dataSources)
        {
            if (dataSources == null)
            {
                throw new ArgumentNullException(nameof(dataSources));
            }

            _dataSources = dataSources.ToArray();

            var changeTokens = new IChangeToken[_dataSources.Length];

            for (var i = 0; i < _dataSources.Length; i++)
            {
                changeTokens[i] = _dataSources[i].ChangeToken;
            }

            ChangeToken = new CompositeChangeToken(changeTokens);
        }
示例#7
0
        public CacheDependency(string filePath)
        {
            var lastSlash = filePath.LastIndexOf('/');

            var fileName = filePath.Substring(lastSlash + 1);

            if (fileName.StartsWith(fileProvider.Root))
            {
                fileName = fileName.Substring(fileProvider.Root.Length);
            }

            var changeToken = fileProvider.Watch(fileName);

            ChangeToken = new CompositeChangeToken(new List <IChangeToken>()
            {
                changeToken
            });
        }
        public CompositeConfigurationProvider(IReadOnlyCollection <IConfigurationProvider> providers)
        {
            if (providers == null)
            {
                throw new ArgumentNullException(nameof(providers));
            }

            int index = 0;

            _providers = new IConfigurationProvider[providers.Count];

            foreach (var provider in providers)
            {
                _providers[index++] = provider;
            }

            _reloadToken = new CompositeChangeToken(providers.Select(p => p.GetReloadToken()).ToArray());
        }
示例#9
0
        public bool IsFeatureActive(string featureName)
        {
            var cacheKey = CacheKey.With(GetType(), nameof(IsFeatureActive), featureName);

            return(_memoryCache.GetOrCreateExclusive(cacheKey, cacheEntry =>
            {
                var changeToken = ThemeEngineCacheRegion.CreateChangeToken();
                var watchChangeToken = _themeBlobProvider.Watch(CurrentThemeSettingPath);
                var tokens = new[]
                {
                    changeToken, watchChangeToken
                };
                var compositeChangeToken = new CompositeChangeToken(tokens);
                cacheEntry.AddExpirationToken(compositeChangeToken);

                var settingJObject = InnerGetAllSettings(_themeBlobProvider, CurrentThemeSettingPath);
                var result = _featuresAgent.IsActive(featureName, settingJObject);
                return result;
            }));
        }