Skip to content

Commit a10d40a

Browse files
Adds response cache.
This gives the provider a much needed performance boost.
1 parent 14d0552 commit a10d40a

File tree

4 files changed

+39
-20
lines changed

4 files changed

+39
-20
lines changed

src/UmbracoFileSystemProviders.Azure/AzureFileSystem.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,6 @@ internal class AzureFileSystem : IFileSystem
5757
/// </summary>
5858
private readonly string containerName;
5959

60-
/// <summary>
61-
/// The maximum number of days to cache blob items for in the browser.
62-
/// </summary>
63-
private readonly int maxDays;
64-
6560
/// <summary>
6661
/// The root url.
6762
/// </summary>
@@ -118,7 +113,7 @@ private AzureFileSystem(string containerName, string rootUrl, string connectionS
118113

119114
this.rootUrl = rootUrl + containerName + "/";
120115
this.containerName = containerName;
121-
this.maxDays = maxDays;
116+
this.MaxDays = maxDays;
122117

123118
this.LogHelper = new WrappedLogHelper();
124119
this.MimeTypeResolver = new MimeTypeResolver();
@@ -139,6 +134,11 @@ private AzureFileSystem(string containerName, string rootUrl, string connectionS
139134
/// </summary>
140135
public bool DisableVirtualPathProvider { get; set; }
141136

137+
/// <summary>
138+
/// Gets or sets the maximum number of days to cache blob items for in the browser.
139+
/// </summary>
140+
public int MaxDays { get; set; }
141+
142142
/// <summary>
143143
/// Returns a singleton instance of the <see cref="AzureFileSystem"/> class.
144144
/// </summary>
@@ -207,7 +207,7 @@ public void AddFile(string path, Stream stream, bool overrideIfExists)
207207
blockBlob.Properties.ContentType = contentType;
208208
}
209209

210-
blockBlob.Properties.CacheControl = string.Format("public, max-age={0}", this.maxDays * 86400);
210+
blockBlob.Properties.CacheControl = string.Format("public, max-age={0}", this.MaxDays * 86400);
211211
blockBlob.SetProperties();
212212

213213
if (created == DateTimeOffset.MinValue)

src/UmbracoFileSystemProviders.Azure/FileSystemVirtualFile.cs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ namespace Our.Umbraco.FileSystemProviders.Azure
1212
{
1313
using System;
1414
using System.IO;
15+
using System.Web;
1516
using System.Web.Hosting;
1617

18+
using global::Umbraco.Core.IO;
19+
1720
/// <summary>
1821
/// Represents a file object in a virtual file.
1922
/// </summary>
@@ -27,24 +30,21 @@ internal class FileSystemVirtualFile : VirtualFile
2730
/// <summary>
2831
/// Initializes a new instance of the <see cref="FileSystemVirtualFile"/> class.
2932
/// </summary>
30-
/// <param name="virtualPath">
31-
/// The virtual path.
32-
/// </param>
33-
/// <param name="stream">
34-
/// The stream.
35-
/// </param>
33+
/// <param name="virtualPath">The virtual path.</param>
34+
/// <param name="fileSystem">The lazy file system implementation.</param>
35+
/// <param name="fileSystemPath">The modified file system path.</param>
3636
/// <exception cref="ArgumentNullException">
37-
/// Thrown if <paramref name="stream"/> is null.
37+
/// Thrown if <paramref name="fileSystem"/> is null.
3838
/// </exception>
39-
public FileSystemVirtualFile(string virtualPath, Func<Stream> stream)
39+
public FileSystemVirtualFile(string virtualPath, Lazy<IFileSystem> fileSystem, string fileSystemPath)
4040
: base(virtualPath)
4141
{
42-
if (stream == null)
42+
if (fileSystem == null)
4343
{
44-
throw new ArgumentNullException("stream");
44+
throw new ArgumentNullException("fileSystem");
4545
}
4646

47-
this.stream = stream;
47+
this.stream = () => fileSystem.Value.OpenFile(fileSystemPath);
4848
}
4949

5050
/// <summary>
@@ -66,6 +66,18 @@ public override bool IsDirectory
6666
/// </returns>
6767
public override Stream Open()
6868
{
69+
// Set the response headers here. It's a bit hacky.
70+
HttpCachePolicy cache = HttpContext.Current.Response.Cache;
71+
cache.SetCacheability(HttpCacheability.Public);
72+
cache.VaryByHeaders["Accept-Encoding"] = true;
73+
74+
IFileSystem azureBlobFileSystem = FileSystemProviderManager.Current.GetUnderlyingFileSystemProvider("media");
75+
int maxDays = ((AzureBlobFileSystem)azureBlobFileSystem).FileSystem.MaxDays;
76+
77+
cache.SetExpires(DateTime.Now.ToUniversalTime().AddDays(maxDays));
78+
cache.SetMaxAge(new TimeSpan(maxDays, 0, 0, 0));
79+
cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
80+
6981
return this.stream();
7082
}
7183
}

src/UmbracoFileSystemProviders.Azure/FileSystemVirtualPathProvider.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,10 @@ public override VirtualFile GetFile(string virtualPath)
142142
}
143143

144144
string fileSystemPath = this.RemovePathPrefix(path);
145-
return new FileSystemVirtualFile(virtualPath, () => this.fileSystem.Value.OpenFile(fileSystemPath));
146-
}
147145

146+
return new FileSystemVirtualFile(virtualPath, this.fileSystem, fileSystemPath);
147+
}
148+
148149
/// <summary>
149150
/// Correctly formats the virtual path prefix.
150151
/// </summary>

src/UmbracoFileSystemProviders.Azure/app.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,10 @@
1212
</dependentAssembly>
1313
</assemblyBinding>
1414
</runtime>
15+
<appSettings>
16+
<!--
17+
Enables the development mode for testing. Addition changes to the FileSystemProviders.config are also required
18+
-->
19+
<add key="AzureBlobFileSystem.UseStorageEmulator" value="true" />
20+
</appSettings>
1521
</configuration>

0 commit comments

Comments
 (0)