Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9529728

Browse files
author
Jijie Chen
committedSep 12, 2017
Added output cache for static resources to reduce page flashes during loading.
1 parent a3b2640 commit 9529728

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed
 

‎src/Papercut.Module.WebUI/Controllers/StaticContentController.cs

+20-1
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ namespace Papercut.Module.WebUI.Controllers
1010
using System.Net.Http.Headers;
1111
using System.Reflection;
1212
using System.Web.Http;
13-
13+
using System.Web.Http.Controllers;
14+
using WebApi.OutputCache.V2;
1415

1516
public class StaticContentController: ApiController
1617
{
1718

19+
[CacheOutput(ClientTimeSpan=300, ServerTimeSpan=86400, CacheKeyGenerator=typeof(PapercutResourceKeyGenerator))]
1820
public HttpResponseMessage Get()
1921
{
2022
var resourceName = GetRequetedResourceName(Request.RequestUri);
@@ -88,5 +90,22 @@ static string GetMimeType(string filename)
8890
{ "woff2", "application/font-woff2" },
8991
};
9092

93+
class PapercutResourceKeyGenerator : DefaultCacheKeyGenerator
94+
{
95+
static string AssemblyVersion;
96+
static PapercutResourceKeyGenerator()
97+
{
98+
AssemblyVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
99+
}
100+
101+
public override string MakeCacheKey(HttpActionContext context, MediaTypeHeaderValue mediaType, bool excludeQueryString = false)
102+
{
103+
var requstUri = context.Request.RequestUri;
104+
int hashCode = string.Concat("PapercutResource", AssemblyVersion, requstUri).GetHashCode();
105+
106+
return (hashCode ^ 0x10000).ToString("x2");
107+
}
108+
}
109+
91110
}
92111
}

‎src/Papercut.Module.WebUI/Papercut.Module.WebUI.csproj

+7
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
<Reference Include="System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
6565
<HintPath>..\..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll</HintPath>
6666
</Reference>
67+
<Reference Include="System.Runtime.Caching" />
6768
<Reference Include="System.Security" />
6869
<Reference Include="System.ServiceModel" />
6970
<Reference Include="System.Web.Http, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
@@ -73,6 +74,12 @@
7374
<HintPath>..\..\packages\Microsoft.AspNet.WebApi.SelfHost.5.2.3\lib\net45\System.Web.Http.SelfHost.dll</HintPath>
7475
</Reference>
7576
<Reference Include="System.Xml" />
77+
<Reference Include="WebApi.OutputCache.Core, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
78+
<HintPath>..\..\packages\Strathweb.CacheOutput.WebApi2.0.10.0\lib\net45\WebApi.OutputCache.Core.dll</HintPath>
79+
</Reference>
80+
<Reference Include="WebApi.OutputCache.V2, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
81+
<HintPath>..\..\packages\Strathweb.CacheOutput.WebApi2.0.10.0\lib\net45\WebApi.OutputCache.V2.dll</HintPath>
82+
</Reference>
7683
</ItemGroup>
7784
<ItemGroup>
7885
<Compile Include="Controllers\MessageController.cs" />

‎src/Papercut.Module.WebUI/Properties/AssemblyInfo.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,4 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.0.0.0")]
36-
[assembly: AssemblyFileVersion("1.0.0.0")]
35+
[assembly: AssemblyVersion("5.0.0.17")]

‎src/Papercut.Module.WebUI/packages.config

+1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111
<package id="MimeKit" version="1.16.2" targetFramework="net45" />
1212
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net45" />
1313
<package id="Serilog" version="2.5.0" targetFramework="net45" />
14+
<package id="Strathweb.CacheOutput.WebApi2" version="0.10.0" targetFramework="net45" />
1415
</packages>

0 commit comments

Comments
 (0)