EDC.Cache.ICache <string, string> CreateCache(DummyCacheFactory innerFactory, TimeSpan expire)
        {
            var factory = new DelayedInvalidateCacheFactory {
                Inner = innerFactory, ExpirationInterval = expire
            };

            return(factory.Create <string, string>("Test Cache"));
        }
        EDC.Cache.ICache <string, string> CreateCache(DummyCacheFactory innerFactory, TimeSpan expire, TimeSpan recent)
        {
            var factory = new DelayedInvalidateCacheFactory {
                Inner = innerFactory, ExpirationInterval = expire, GlobalThresholdTicks = recent
            };

            return(factory.Create <string, string>("Test Cache"));
        }
Esempio n. 3
0
        /// <summary>
        /// Create cache, according to the configuration of the factory.
        /// </summary>
        /// <typeparam name="TKey">Type of cache key.</typeparam>
        /// <typeparam name="TValue">Type of cache value.</typeparam>
        /// <param name="cacheName">Name of the cache.</param>
        /// <returns>
        /// A cache.
        /// </returns>
        /// <exception cref="System.InvalidOperationException"></exception>
        public ICache <TKey, TValue> Create <TKey, TValue>(string cacheName)
        {
            ICacheFactory fact = null;

            //
            // The first caches encountered will be lowest in the stack
            //
            if (Redis)
            {
                if (ConfigurationSettings.GetCacheConfigurationSection().RedisCacheSettings.Enabled)
                {
                    fact = new RedisCacheFactory {
                        Inner = null, CompressKey = RedisKeyCompression, CompressValue = RedisValueCompression, KeyExpiry = RedisKeyExpiry
                    };
                }
                else
                {
                    Diagnostics.EventLog.Application.WriteWarning("Cache '{0}' requested a Redis layer but was denied due to the SoftwarePlatform.config settings.", cacheName);
                }
            }

            if (Dictionary)
            {
                fact = new DictionaryCacheFactory {
                    Inner = fact
                };
            }

            if (MaxCacheEntries > 0)
            {
                // Attempt to get the maximum value from the configuration settings
                int configMaxCacheSize = GetMaximumCacheSize(cacheName);
                if (configMaxCacheSize > 0)
                {
                    MaxCacheEntries = configMaxCacheSize;
                }

                if (Lru)
                {
                    fact = new LruCacheFactory {
                        Inner = fact, MaxSize = MaxCacheEntries, EvictionFrequency = LruEvictionFrequency
                    };
                }
                else
                {
                    fact = new StochasticCacheFactory {
                        Inner = fact, MaxSize = MaxCacheEntries
                    };
                }
            }
            if (ExpirationInterval != TimeSpan.Zero)
            {
                fact = new TimeoutCacheFactory {
                    Inner = fact, Expiration = ExpirationInterval, EvictionFrequency = TimeoutEvictionFrequency
                };
            }
            if (TransactionAware)
            {
                fact = new TransactionAwareCacheFactory {
                    Inner = fact
                };
            }
            if (Logging)
            {
                fact = new LoggingCacheFactory {
                    Inner = fact, MaxSize = MaxCacheEntries
                };
            }
            if (fact != null && (ThreadSafe && !fact.ThreadSafe))
            {
                fact = new ThreadSafeCacheFactory {
                    Inner = fact
                };
            }
            if (BlockIfPending || DelayedInvalidates)
            {
                fact = new BlockIfPendingCacheFactory {
                    Inner = fact
                };
            }
            if (DelayedInvalidates)
            {
                // Must be below BlockIfPending
                fact = new DelayedInvalidateCacheFactory {
                    Inner = fact, ExpirationInterval = new TimeSpan(0, 1, 0)
                };
            }
            if (MetadataCache)
            {
                fact = new MetadataCacheFactory {
                    Inner = fact
                };
            }
            if (IsolateTenants)
            {
                fact = new PerTenantNonSharingCacheFactory {
                    Inner = fact
                };
            }


            if (Distributed)
            {
                fact = new RedisPubSubCacheFactory <TKey>(fact, IsolateTenants);
            }

            // Final safety check
            if (fact == null || (ThreadSafe && !fact.ThreadSafe))
            {
                throw new InvalidOperationException();
            }
            var cache = fact.Create <TKey, TValue>(cacheName);

            return(cache);
        }