protected void SetItem(TKey key, TItem item, DateTime expiration) { CacheInformation <TItem> cache = new CacheInformation <TItem> { Item = item, ExpirationInfo = new ExpirationInformation(expiration) }; try { _instanceLock.EnterWriteLock(); _cache[key] = cache; // Cache Size Controrl if (_cache.Count > this.MaxCacheSize) { _cache.Clear(); // Not very sophisticated but does the job of keeping a limit on the cache _cache[key] = cache; } } finally { _instanceLock.ExitWriteLock(); } }
public object Create(object parent, object configContext, XmlNode xmlInConfig) { if (_configuration == null) { RefreshConfiguration(); } var cache = Configuration.ServiceProvider.GetConfigCache(); //Cache and return by section name return(cache.GetCache(xmlInConfig.Name, () => { //Parse the section for details about where it is? //TODO should the service provider be specified per redirect?? and then you could have config in more than one place? var redirectInformation = this.Parser.ParseRedirectDetails(xmlInConfig.OuterXml); Type expectedType = Type.GetType(redirectInformation.Type, true); //Results for cache result storage and information CacheInformation cacheResults = new CacheInformation() { Duration = new TimeSpan(0, redirectInformation.CacheDurationInMinutes == 0 ? CacheDurationUnDefinedUseMinutesInADay : redirectInformation.CacheDurationInMinutes, 0), }; //TODO ignore name and system name when in config section and has ID - needs to be implemented using (var storage = Configuration.ServiceProvider.GetStorage()) { //The whole purpose is to handle standard config sections designed in System.Configuration if (redirectInformation.Mode == Mode.Standard) { var configurationValue = storage.GetConfigurationSection(redirectInformation.Name, redirectInformation.SystemName); cacheResults.Value = this.Parser.StandardConfigSectionParse(configurationValue.XML, expectedType); } //Handle regular POCO calls else if (redirectInformation.Mode == Mode.Poco) { if (redirectInformation.PocoBody != null && !string.IsNullOrWhiteSpace(redirectInformation.PocoBody.Value)) { cacheResults.Value = this.Parser.POCOConfigSectionParse(redirectInformation.PocoBody.Value, expectedType); } else { var configurationValue = storage.GetConfigurationSection(redirectInformation.Name, redirectInformation.SystemName); cacheResults.Value = this.Parser.POCOConfigSectionParse(configurationValue.XML, expectedType); } } else { throw new InAnotherCastleInvalidConfigurationException(string.Format("Invalid Mode type, must be within the Enum ({0})", typeof(Mode).FullName)); } } return cacheResults; }, //Important, if this is not called the section will remain cached by ASP.Net... in a way we do not need are own cache, but we do for fall back purposes (name, value) => { ConfigurationManager.RefreshSection(name); } )); }
public object Cache(string sectionName, CacheInformation configurationValue, Action <string, CacheInformation> cacheExpired) { return(Value); }