示例#1
0
        public DataSourceDependentMatcher(
            EndpointDataSource dataSource,
            Func <MatcherBuilder> matcherBuilderFactory)
        {
            _matcherBuilderFactory = matcherBuilderFactory;

            _cache = new DataSourceDependentCache <Matcher>(dataSource, CreateMatcher);
            _cache.EnsureInitialized();
        }
 public AuthorizationPolicyCache(EndpointDataSource dataSource)
 {
     // We cache AuthorizationPolicy instances per-Endpoint for performance, but we want to wipe out
     // that cache if the endpoints change so that we don't allow unbounded memory growth.
     _policyCache = new DataSourceDependentCache <ConcurrentDictionary <Endpoint, AuthorizationPolicy> >(dataSource, (_) =>
     {
         // We don't eagerly fill this cache because there's no real reason to.
         return(new ConcurrentDictionary <Endpoint, AuthorizationPolicy>());
     });
     _policyCache.EnsureInitialized();
 }
示例#3
0
        public DataSourceDependentMatcher(
            EndpointDataSource dataSource,
            Lifetime lifetime,
            Func <MatcherBuilder> matcherBuilderFactory)
        {
            _matcherBuilderFactory = matcherBuilderFactory;

            _cache = new DataSourceDependentCache <Matcher>(dataSource, CreateMatcher);
            _cache.EnsureInitialized();

            // This will Dispose the cache when the lifetime is disposed, this allows
            // the service provider to manage the lifetime of the cache.
            lifetime.Cache = _cache;
        }
示例#4
0
    public void Cache_Initializes_WhenEnsureInitializedCalled()
    {
        // Arrange
        var called = false;

        var dataSource = new DynamicEndpointDataSource();
        var cache      = new DataSourceDependentCache <string>(dataSource, (endpoints) =>
        {
            called = true;
            return("hello, world!");
        });

        // Act
        cache.EnsureInitialized();

        // Assert
        Assert.True(called);
        Assert.Equal("hello, world!", cache.Value);
    }
示例#5
0
    public void Cache_CanDispose_WhenInitialized()
    {
        // Arrange
        var count = 0;

        var dataSource = new DynamicEndpointDataSource();
        var cache      = new DataSourceDependentCache <string>(dataSource, (endpoints) =>
        {
            count++;
            return($"hello, {count}!");
        });

        cache.EnsureInitialized();
        Assert.Equal("hello, 1!", cache.Value);

        // Act
        cache.Dispose();

        // Assert
        dataSource.AddEndpoint(null);
        Assert.Equal("hello, 1!", cache.Value); // Ignores update
    }
示例#6
0
    public void Cache_Reinitializes_WhenDataSourceChanges()
    {
        // Arrange
        var count = 0;

        var dataSource = new DynamicEndpointDataSource();
        var cache      = new DataSourceDependentCache <string>(dataSource, (endpoints) =>
        {
            count++;
            return($"hello, {count}!");
        });

        cache.EnsureInitialized();
        Assert.Equal("hello, 1!", cache.Value);

        // Act
        dataSource.AddEndpoint(null);

        // Assert
        Assert.Equal(2, count);
        Assert.Equal("hello, 2!", cache.Value);
    }
示例#7
0
 internal TemplateBinder GetTemplateBinder(RouteEndpoint endpoint) => _cache.EnsureInitialized().GetOrAdd(endpoint, _createTemplateBinder);