Permalink
Browse files

Refactoring SetServerPageCacheDuration

  • Loading branch information...
napernik committed Oct 3, 2018
1 parent 1cd5373 commit 546737d705a0b65170f6a5a84fa12bd74e486086
@@ -1,17 +1,16 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Composite.Functions;
using System.Web;
using Composite.Functions;
using Composite.Plugins.Functions.FunctionProviders.StandardFunctionProvider.Foundation;
using Composite.Core.ResourceSystem;
namespace Composite.Plugins.Functions.FunctionProviders.StandardFunctionProvider.Web.Response
{
internal sealed class SetServerPageCacheDuration : StandardFunctionBase
{
private const string ParameterName_MaxSeconds = "MaxSeconds";
public SetServerPageCacheDuration(EntityTokenFactory entityTokenFactory)
: base("SetServerPageCacheDuration", "Composite.Web.Response", typeof(void), entityTokenFactory)
{
@@ -21,32 +20,31 @@ protected override IEnumerable<StandardFunctionParameterProfile> StandardFunctio
{
get
{
WidgetFunctionProvider textboxWidget = StandardWidgetFunctions.TextBoxWidget;
yield return new StandardFunctionParameterProfile(
"MaxSeconds", typeof(int), true, new ConstantValueProvider(0), textboxWidget);
ParameterName_MaxSeconds, typeof(int), true, new ConstantValueProvider(0), StandardWidgetFunctions.TextBoxWidget);
}
}
public override object Execute(ParameterList parameters, FunctionContextContainer context)
{
if (HttpContext.Current != null && HttpContext.Current.Response != null)
var httpContext = HttpContext.Current;
if (httpContext?.Response == null) return null;
var cache = httpContext.Response.Cache;
int maxSeconds = parameters.GetParameter<int>(ParameterName_MaxSeconds);
if (maxSeconds <= 0)
{
cache.SetCacheability(HttpCacheability.NoCache);
cache.SetNoServerCaching();
cache.SetNoStore();
}
else
{
int maxSeconds = parameters.GetParameter<int>("MaxSeconds");
if (maxSeconds < -1)
{
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.Cache.SetNoServerCaching();
HttpContext.Current.Response.Cache.SetNoStore();
}
else
{
HttpContext.Current.Response.Cache.SetExpires(DateTime.Now.AddSeconds(maxSeconds));
HttpContext.Current.Response.Cache.SetMaxAge(new TimeSpan(0, 0, maxSeconds));
}
cache.SetExpires(DateTime.Now.AddSeconds(maxSeconds));
cache.SetMaxAge(TimeSpan.FromSeconds(maxSeconds));
}
return null;

0 comments on commit 546737d

Please sign in to comment.