Exemplo n.º 1
0
 public T GetOrCreate <T>(string key, Func <T> factory, ICacheOptions cacheOptions)
 {
     return(GetOrCreate(key, factory, new MemoryCacheEntryOptions
     {
         AbsoluteExpirationRelativeToNow = cacheOptions.AbsoluteExpirationRelativeToNow
     }));
 }
Exemplo n.º 2
0
 public CachedCrudRepository(ICrudStore <TEntity, TKey> store,
                             ICacheService cacheService,
                             ICacheOptions options)
     : base(store, cacheService, options)
 {
     Store = store;
 }
Exemplo n.º 3
0
 public AssetRepository(IAssetStore store,
                        ICacheService cacheService,
                        IUserIdAccesor userIdAccesor,
                        ICacheOptions options)
     : base(store, cacheService, options)
 {
     _userIdAccesor = userIdAccesor;
 }
Exemplo n.º 4
0
 public TransactionRepository(ITransactionStore store,
                              ICacheService cacheService,
                              IUserIdAccesor userIdAccesor,
                              ICacheOptions options)
     : base(store, cacheService, options)
 {
     _userIdAccesor = userIdAccesor;
 }
Exemplo n.º 5
0
 public GroupRepository(IGroupStore store,
                        ICacheService cacheService,
                        IUserIdAccesor userIdAccesor,
                        ICacheOptions options)
     : base(store, cacheService, options)
 {
     _userIdAccesor = userIdAccesor;
 }
 public CachedReadOnlyRepository(IStore <TEntity, TKey> store,
                                 ICacheService cacheService,
                                 ICacheOptions options)
     : base(store)
 {
     CacheService = cacheService;
     Options      = options;
 }
Exemplo n.º 7
0
 /// <summary>
 /// Set the builder and size parameter for the specified cache type
 /// </summary>
 /// <param name="cacheType">The cache type</param>
 /// <param name="options">An <see cref="ICacheOptions"/> object that
 /// specifies a cache builder and size to use when constructing the
 /// specified cache type</param>
 /// <returns>The <see cref="DataSetBuilder"/></returns>
 internal T ConfigureCache(CacheType cacheType, ICacheOptions options)
 {
     if (_cacheConfiguration.ContainsKey(cacheType))
     {
         _cacheConfiguration.Remove(cacheType);
     }
     _cacheConfiguration.Add(cacheType, options);
     return((T)this);
 }
Exemplo n.º 8
0
 internal static AsyncCircuitBreakerPolicy GetCircuitBreakerPolicy(
     ICacheOptions options,
     bool debugLoggingEnabled,
     ILogger logger,
     string onBreakMessage    = " ",
     string onResetMessage    = " ",
     string onHalfOpenMessage = " "
     )
 {
     return(Policy
            .Handle <CacheException>(ex =>
                                     ex.ErrorCode ==
                                     ErrorCodes.Common.NO_SERVER_AVAILABLE
                                     )
            .CircuitBreakerAsync(
                exceptionsAllowedBeforeBreaking:
                options.ExceptionsAllowedBeforeBreaking < 0 ?
                1 :
                options.ExceptionsAllowedBeforeBreaking,
                durationOfBreak:
                TimeSpan.FromSeconds(
                    options.DurationOfBreakInSeconds < 0 ?
                    30 :
                    options.DurationOfBreakInSeconds),
                onBreak: (ex, ts) =>
     {
         if (debugLoggingEnabled)
         {
             logger.LogDebug(
                 $"{onBreakMessage}\n" +
                 $"Reason:{ex.Message}");
         }
     },
                onReset: () =>
     {
         if (debugLoggingEnabled)
         {
             logger.LogDebug(onResetMessage);
         }
     },
                onHalfOpen: () =>
     {
         if (debugLoggingEnabled)
         {
             logger.LogDebug(onHalfOpenMessage);
         }
     }));
 }
        public static IIdentityServerBuilder AddNCacheCaching(
            this IIdentityServerBuilder builder,
            Action <ICacheOptions> ncacheOptionsAction = null)
        {
            var options = new ICacheOptions();

            ncacheOptionsAction?.Invoke(options);
            var logger =
                builder
                .Services
                .BuildServiceProvider()
                .GetRequiredService <ILogger <CacheHandle> >();

            builder.Services.AddSingleton <CacheHandle>(
                new CacheHandle(options, logger));
            builder.Services.AddSingleton(typeof(ICache <>), typeof(Cache <>));

            return(builder);
        }
Exemplo n.º 10
0
        public CacheHandle(
            ICacheOptions options,
            ILogger <CacheHandle> logger)
        {
            this.options = options ??
                           throw new ArgumentNullException(nameof(options));
            _logger = logger ??
                      throw new ArgumentNullException(nameof(logger));
            cache = Utilities.CreateCache(options);

            var circuitBreaker = Utilities
                                 .GetCircuitBreakerPolicy(
                options,
                logger.IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug),
                logger,
                $"ICache<T> is open circuited.\n " +
                $"Duration: {options.DurationOfBreakInSeconds}",
                $"ICache<T> circuit breaker is reset",
                $"ICache<T> circuit breaker is half-open");

            getFallBack = GetFallBackPolicy(circuitBreaker);
            setFallBack = SetFallBackPolicy(circuitBreaker);
        }
Exemplo n.º 11
0
        private static async Task <bool> CreateUpdateItem(object cacheKey, object cacheObject, string region, CacheItemPolicy cacheItemPolicy, ICacheOptions cacheOptions)
        {
            var key       = MemoryUtilities.CombinedKey(cacheKey, region);
            var cacheItem = new CacheObject
            {
                Item = await MemoryStreamHelper.SerializeObject(cacheObject)
            };

            bool results;

            lock (Sync)
            {
                results = _cache.Add(key, cacheItem, cacheItemPolicy);
            }

            return(results);
        }
Exemplo n.º 12
0
        /// <summary>
        ///     Add an item to the cache and will need to be removed manually
        /// </summary>
        /// <param name="cacheKey">The cache key.</param>
        /// <param name="cacheObject">The cache object.</param>
        /// <param name="region">If region is supported by cache , it will seperate the lookups</param>
        /// <param name="options">Options that can be set for the cache</param>
        /// <returns>true or false</returns>
        public override async Task <bool> AddPermanent(object cacheKey, object cacheObject, string region, ICacheOptions options)
        {
            if (!_isEnabled)
            {
                return(true);
            }

            if (await Get(cacheKey, region) != null)
            {
                await Remove(cacheKey, region);
            }

            return(await CreateUpdateItem(cacheKey, cacheObject, region, new CacheItemPolicy(), options));
        }
Exemplo n.º 13
0
        /// <summary>
        ///     Add to cache.
        /// </summary>
        /// <param name="cacheKey">The cache key.</param>
        /// <param name="cacheObject">The cache object.</param>
        /// <param name="region">If region is supported by cache , it will seperate the lookups</param>
        /// <param name="options">Options that can be set for the cache</param>
        /// <returns>True if successful else false.</returns>
        public override async Task <bool> Add(object cacheKey, object cacheObject, string region, ICacheOptions options)
        {
            if (!_isEnabled)
            {
                return(true);
            }

            if (await Get(cacheKey, region) != null)
            {
                await Remove(cacheKey, region);
            }

            options.ExpirationInMinutes = options.ExpirationInMinutes == 15 ? _cacheExpirationTime : options.ExpirationInMinutes;

            var cacheItemPolicy = new CacheItemPolicy();

            if (options.AllowSliddingTime)
            {
                cacheItemPolicy.SlidingExpiration = TimeSpan.FromMinutes(options.ExpirationInMinutes);
            }
            else
            {
                cacheItemPolicy.AbsoluteExpiration = DateTimeOffset.UtcNow.AddMinutes(options.ExpirationInMinutes);
            }
            return(await CreateUpdateItem(cacheKey, cacheObject, region, cacheItemPolicy, options));
        }
Exemplo n.º 14
0
 public Task <bool> AddPermanent(object cacheKey, object cacheObject, string region, ICacheOptions options)
 {
     return(Task.FromResult(true));
 }
Exemplo n.º 15
0
        /// <summary>
        ///     Add an item to the cache and will need to be removed manually
        /// </summary>
        /// <param name="cacheKey">The cache key.</param>
        /// <param name="cacheObject">The cache object.</param>
        /// <param name="region">If region is supported by cache , it will seperate the lookups</param>
        /// <param name="options">Options that can be set for the cache</param>
        /// <returns>true or false</returns>
        public override async Task <bool> AddPermanent(object cacheKey, object cacheObject, string region, ICacheOptions options)
        {
            if (!_isEnabled)
            {
                return(true);
            }

            if (await Get(cacheKey, region) != null)
            {
                await Remove(cacheKey, region);
            }

            var expireTime = DateTime.UtcNow.AddYears(100);
            var item       = new MongoCachModel
            {
                CacheKey    = cacheKey.ToString(),
                Expires     = expireTime,
                CacheObject = new CacheObject
                {
                    Item = await MemoryStreamHelper.SerializeObject(cacheObject)
                },
                CacheOptions = options
            };

            return(await CreateUpdateItem(region, item));
        }
Exemplo n.º 16
0
 /// <summary>
 ///     Add an item to the cache and will need to be removed manually
 /// </summary>
 /// <param name="cacheKey">The cache key.</param>
 /// <param name="cacheObject">The cache object.</param>
 /// <param name="region">If region is supported by cache , it will seperate the lookups</param>
 /// <param name="options">Options that can be set for the cache</param>
 /// <returns>true or false</returns>
 public abstract Task <bool> AddPermanent(object cacheKey, object cacheObject, string region, ICacheOptions options);
Exemplo n.º 17
0
 public AppOnlyManager(IDistributedCache store, ICacheOptions storeOptions)
 {
     this.store           = store;
     this.storeOptions    = storeOptions;
     this.SiteInformation = new List <SiteInformation>();
 }
Exemplo n.º 18
0
        /// <summary>
        ///     Add an item to the cache and will need to be removed manually
        /// </summary>
        /// <param name="cacheKey">The cache key.</param>
        /// <param name="cacheObject">The cache object.</param>
        /// <param name="region">If region is supported by cache , it will seperate the lookups</param>
        /// <param name="options">Options that can be set for the cache</param>
        /// <returns>true or false</returns>
        public override async Task <bool> AddPermanent(object cacheKey, object cacheObject, string region, ICacheOptions options)
        {
            if (!_isEnabled)
            {
                return(true);
            }

            var returnValue = true;

            foreach (var cp in CacheProviders)
            {
                if (Array.Exists(options.SkipProvider, e => e == cp.GetType().Name))
                {
                    continue;
                }
                var wasAdded = await cp.AddPermanent(cacheKey, cacheObject, region, options);

                if (!wasAdded)
                {
                    returnValue = false;
                }
            }

            return(returnValue);
        }
Exemplo n.º 19
0
 public CurrencyRepository(ICurrencyStore store, ICacheService cacheService, ICacheOptions options)
     : base(store, cacheService, options)
 {
 }
Exemplo n.º 20
0
 internal void SetCacheOptions(ICacheOptions cacheSettings)
 {
     _cacheSettings = cacheSettings;
 }
Exemplo n.º 21
0
 public async Task <T> GetOrCreateAsync <T>(string key, Func <Task <T> > factory, ICacheOptions cacheOptions)
 {
     return(await GetOrCreateAsync(key, factory, new MemoryCacheEntryOptions
     {
         AbsoluteExpirationRelativeToNow = cacheOptions.AbsoluteExpirationRelativeToNow
     }));
 }