/// <summary> /// Static constructor /// </summary> static CacheFactory() { try { foreach (CacheConfigElement cacheConfigElement in ConfigurationSettings.GetCacheConfigurationSection().Caches) { _cacheConfigDictionary[cacheConfigElement.CacheName] = cacheConfigElement; } } catch (Exception ex) { // Don't allow exceptions to leave static constructor Diagnostics.EventLog.Application.WriteError("An error occurred in CacheFactory. {0}", ex.ToString()); } }
/// <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); }