/// <summary> /// Gets cache information for the specified domain operation entry. /// </summary> /// <param name="method">The domain operation entry to get cache information for.</param> /// <returns>Cache information.</returns> private static OutputCacheAttribute GetOutputCacheInformation(DomainOperationEntry method) { OutputCacheAttribute cacheAttribute = method.Attributes.OfType <OutputCacheAttribute>().FirstOrDefault(); if (cacheAttribute != null) { if (!String.IsNullOrEmpty(cacheAttribute.CacheProfile)) { if (QueryOperationInvoker.cacheProfiles == null) { lock (QueryOperationInvoker.syncRoot) { if (QueryOperationInvoker.cacheProfiles == null) { OutputCacheSettingsSection outputCacheSettings = (OutputCacheSettingsSection)WebConfigurationManager.GetWebApplicationSection("system.web/caching/outputCacheSettings"); QueryOperationInvoker.cacheProfiles = outputCacheSettings.OutputCacheProfiles; } } } OutputCacheProfile profile = QueryOperationInvoker.cacheProfiles[cacheAttribute.CacheProfile]; OutputCacheAttribute cacheInfo = new OutputCacheAttribute(QueryOperationInvoker.GetCacheLocation(profile.Location), profile.Duration); cacheInfo.VaryByHeaders = profile.VaryByHeader; cacheInfo.SqlCacheDependencies = profile.SqlDependency; return(cacheInfo); } return(cacheAttribute); } return(null); }
public FeedControllerFixture() { _userRepository = new Mock <IUserRepository>(); _categoryRepository = new Mock <ICategoryRepository>(); _tagRepository = new Mock <ITagRepository>(); _storyRepository = new Mock <IStoryRepository>(); var cacheProfile = new OutputCacheProfile("FeedCache") { Duration = 360 }; var cacheSection = new OutputCacheSettingsSection(); cacheSection.OutputCacheProfiles.Add(cacheProfile); _configurationManager = new Mock <IConfigurationManager>(); _configurationManager.Setup(c => c.GetSection <OutputCacheSettingsSection>(It.IsAny <string>())).Returns(cacheSection); _controller = new FeedController(_configurationManager.Object, _categoryRepository.Object, _tagRepository.Object, _storyRepository.Object) { Settings = settings.Object, UserRepository = _userRepository.Object }; _controller.MockHttpContext("/Kigg", null, null); RouteTable.Routes.Clear(); new RegisterRoutes(settings.Object).Execute(); }
public PartialCacheAttribute(string cacheProfileName) { OutputCacheSettingsSection outputCacheSettingsSection = (OutputCacheSettingsSection)WebConfigurationManager.GetSection("system.web/caching/outputCacheSettings"); OutputCacheProfile outputCacheProfile = outputCacheSettingsSection.OutputCacheProfiles[cacheProfileName]; Duration = outputCacheProfile.Duration; VaryByParam = outputCacheProfile.VaryByParam; }
public CustomizeCacheAttribute(string cacheProfileName) { OutputCacheSettingsSection cacheSettings = (OutputCacheSettingsSection)WebConfigurationManager .GetSection("system.web/caching/outputCacheSettings"); OutputCacheProfile cacheProfile = cacheSettings.OutputCacheProfiles[cacheProfileName]; Duration = cacheProfile.Duration; VaryByParam = cacheProfile.VaryByParam; VaryByCustom = cacheProfile.VaryByCustom; }
public int GetCacheDuration() { int duration = 0; StackFrame frame = new StackFrame(2, false); MethodBase method = frame.GetMethod(); // First try the Method attribute OutputCacheAttribute[] attributes = (OutputCacheAttribute[])method.GetCustomAttributes(typeof(OutputCacheAttribute), true); // If not found, then try class if (attributes.Length == 0) { attributes = (OutputCacheAttribute[])GetType().GetCustomAttributes(typeof(OutputCacheAttribute), true); } if (attributes.Length > 0) { OutputCacheAttribute cacheAttribute = attributes[0]; if (cacheAttribute.Duration > 0) { duration = cacheAttribute.Duration; } else { if (!string.IsNullOrEmpty(cacheAttribute.CacheProfile)) { OutputCacheSettingsSection settings = _configurationManager.GetSection <OutputCacheSettingsSection>("system.web/caching/outputCacheSettings"); if ((settings != null) && (settings.OutputCacheProfiles.Count > 0)) { OutputCacheProfile profile = settings.OutputCacheProfiles.Get(cacheAttribute.CacheProfile); if ((profile != null) && (profile.Duration > 0)) { duration = profile.Duration; } } } } } if (duration > 0) { duration = Convert.ToInt32(duration / 60); } return(duration); }
private void CacheSetting(string settingKey, string settingValue, Cache cache) { settingValue = settingValue.NullSafe(); OutputCacheSettingsSection outputCacheSection = _configurationAdapter.GetSection <OutputCacheSettingsSection>("system.web/caching/outputCacheSettings"); if (outputCacheSection.OutputCacheProfiles.Count > 0) { OutputCacheProfile profile = outputCacheSection.OutputCacheProfiles.Get("SettingsCacheProfile"); if (null != profile) { cache.Add(settingCacheKey + settingKey, settingValue, null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(profile.Duration), CacheItemPriority.Normal, null); } } }
public CachingParameterInspector(string cacheProfileName) { if (string.IsNullOrEmpty(cacheProfileName)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR2.CacheProfileNameNullOrEmpty)); } OutputCacheSettingsSection cacheSettings = AspNetEnvironment.Current.UnsafeGetConfigurationSection("system.web/caching/outputCacheSettings") as OutputCacheSettingsSection; if (cacheSettings == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR2.GetString(SR2.CacheProfileNotConfigured, cacheProfileName))); } this.cacheProfile = cacheSettings.OutputCacheProfiles[cacheProfileName]; if (this.cacheProfile == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR2.GetString(SR2.CacheProfileNotConfigured, cacheProfileName))); } // Validate the cacheProfile if (this.cacheProfile.Location != OutputCacheLocation.None) { // Duration must be set; Duration default value is -1 if (this.cacheProfile.Duration == -1) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR2.GetString(SR2.CacheProfileValueMissing, this.cacheProfile.Name, "Duration"))); } if (this.cacheProfile.VaryByParam == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR2.GetString(SR2.CacheProfileValueMissing, this.cacheProfile.Name, "VaryByParam"))); } } if (string.Equals(this.cacheProfile.SqlDependency, "CommandNotification", StringComparison.OrdinalIgnoreCase)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR2.CommandNotificationSqlDependencyNotSupported)); } if (!string.IsNullOrEmpty(this.cacheProfile.SqlDependency)) { ParseSqlDependencyString(cacheProfile.SqlDependency); } }
public void Validate(OperationDescription operationDescription) { /* if (!ServiceHostingEnvironment.AspNetCompatibilityEnabled) * { * throw new NotSupportedException("WebCacheAttribute is supported only in AspNetCompatibility mode."); * } */ if (operationDescription.Behaviors.Find <WebGetAttribute>() == null) { throw new InvalidOperationException("The WebCacheAttribute can only be used with GET operations."); } if (!string.IsNullOrEmpty(this.CacheProfileName)) { OutputCacheProfile cacheProfile = null; OutputCacheSettingsSection cacheSettings = (OutputCacheSettingsSection)WebConfigurationManager.GetSection("system.web/caching/outputCacheSettings"); if (cacheSettings == null) { throw new InvalidOperationException(String.Format("Cache profile with name '{0}' is not configured.", this.CacheProfileName)); } cacheProfile = cacheSettings.OutputCacheProfiles[this.CacheProfileName]; if (cacheProfile == null) { throw new InvalidOperationException(String.Format("Cache profile with name '{0}' is not configured.", this.CacheProfileName)); } if (!cacheProfile.Enabled) { throw new InvalidOperationException(String.Format("Cache profile with name '{0}' is disabled.", this.CacheProfileName)); } this.CacheProfile = cacheProfile; } if (string.Equals(this.SqlDependency, "CommandNotification", StringComparison.OrdinalIgnoreCase)) { throw new NotSupportedException("CommandNotification is not supported as a valid sql dependency currently"); } // validate that the dependency has been properly configured in sql if (!string.IsNullOrEmpty(this.SqlDependency)) { foreach (SqlCacheDependency dependency in CachingParameterInspector.CreateSqlDependencies(this.SqlDependency)) { dependency.Dispose(); } } }
public ChildActionOutputCacheAttribute(string cacheProfile) { // get output cache section of web config OutputCacheSection settings = (OutputCacheSection)WebConfigurationManager.GetSection($"{_cachingSection}{_outputCacheSection}"); // check section exists and caching is enabled if (settings != null && settings.EnableOutputCache) { // if caching enabled, get profile OutputCacheSettingsSection profileSettings = (OutputCacheSettingsSection)WebConfigurationManager.GetSection($"{_cachingSection}{_profileSection}"); OutputCacheProfile profile = profileSettings.OutputCacheProfiles[cacheProfile]; if (profile != null && profile.Enabled) { // if profile exits set profile params Duration = profile.Duration; VaryByParam = profile.VaryByParam; VaryByCustom = profile.VaryByCustom; _profileEnabled = true; // set profile enable to true as output cache is turned on and there is a profile } } }
static void Main(string[] args) { string inputStr = String.Empty; // Define a regular expression to allow only // alphanumeric inputs that are at most 20 character // long. For instance "/iii:". Regex rex = new Regex(@"[^\/w]{1,20}"); // Parse the user's input. if (args.Length < 1) { // No option entered. Console.Write("Input parameters missing."); return; } else { // Get the user's options. inputStr = args[0].ToLower(); if (!(rex.Match(inputStr)).Success) { // Wrong option format used. Console.Write("Input format not allowed."); return; } } // <Snippet1> // Get the Web application configuration. System.Configuration.Configuration configuration = WebConfigurationManager.OpenWebConfiguration( "/aspnetTest"); // Get the <caching> section group. SystemWebCachingSectionGroup cachingSectionGroup = (SystemWebCachingSectionGroup)configuration.GetSectionGroup( "system.web/caching"); // </Snippet1> try { switch (inputStr) { case "/cache": // <Snippet2> // Get the <cache> section. CacheSection cache = cachingSectionGroup.Cache; // Display one of its properties. msg = String.Format( "Cache disable expiration: {0}\n", cache.DisableExpiration); Console.Write(msg); // </Snippet2> break; case "/outcache": // <Snippet3> // Get the .<outputCache> section OutputCacheSection outputCache = cachingSectionGroup.OutputCache; // Display one of its properties. msg = String.Format( "Enable output cache: {0}\n", outputCache.EnableOutputCache.ToString()); Console.Write(msg); // </Snippet3> break; case "/outcacheset": // <Snippet4> // Get the .<outputCacheSettings> section OutputCacheSettingsSection outputCacheSettings = cachingSectionGroup.OutputCacheSettings; // Display the number of existing // profiles. int profilesCount = outputCacheSettings.OutputCacheProfiles.Count; msg = String.Format( "Number of profiles: {0}\n", profilesCount.ToString()); Console.Write(msg); // </Snippet4> break; case "/sql": // <Snippet5> // Get the .<sqlCacheDependency> section SqlCacheDependencySection sqlCacheDependency = cachingSectionGroup.SqlCacheDependency; // Display one of its attributes. msg = String.Format( "Sql cache dependency enabled: {0}\n", sqlCacheDependency.Enabled.ToString()); Console.Write(msg); // </Snippet5> break; // case "/all": // <Snippet6> // Not in use anymore. // StringBuilder allSections = new StringBuilder(); // Get the section collection. // ConfigurationSectionCollection sections= // cachingSectionGroup.Sections; // Get the number of sections. // int sectionsNumber = sections.Count; // System.Collections.IEnumerator ienum = // sections.AllKeys.GetEnumerator(); // int i = 0; // allSections.AppendLine(); // foreach (Object section in sections) // { // msg = String.Format( // "Section{0}: {1}\n", // i, section.ToString()); // allSections.AppendLine(msg); // i++; // } // </Snippet6> // Console.Write(allSections.ToString()); // break; default: // Option is not allowed.. Console.Write("Input not allowed."); break; } } catch (ArgumentException e) { // Never display this. Use it for // debugging purposes. msg = e.ToString(); } }