Skip to content

Instantly share code, notes, and snippets.

@Daniel15
Created July 23, 2013 22:07
Show Gist options
  • Save Daniel15/6066589 to your computer and use it in GitHub Desktop.
Save Daniel15/6066589 to your computer and use it in GitHub Desktop.
Cassette per-site cache directory
/// <summary>
/// Handles custom configuration of Cassette
/// </summary>
public class CassetteSettings : IConfiguration<Cassette.CassetteSettings>
{
/// <summary>
/// Base cache path
/// </summary>
private const string CACHE_BASE_PATH = @"c:\temp\Cassette\";
/// <summary>
/// Configures Cassette's settings
/// </summary>
/// <param name="settings">The settings.</param>
public void Configure(Cassette.CassetteSettings settings)
{
settings.CacheDirectory = new FileSystemDirectory(GenerateUniquePerSiteCacheDirectory());
}
/// <summary>
/// Generate a cache directory name unique to this website.
/// </summary>
/// <returns>Cache directory name</returns>
private static string GenerateUniquePerSiteCacheDirectory()
{
// Application ID is something like "/LM/W3SVC/2000/ROOT/Trunk/PageUpPeople"
// This is unique per application in IIS. Use a hash of it for the cache path
string hashedAppId;
using (var hasher = SHA1.Create())
{
var bytes = hasher.ComputeHash(Encoding.Default.GetBytes(HostingEnvironment.ApplicationID));
hashedAppId = BitConverter.ToString(bytes).Replace("-", string.Empty);
}
return Path.Combine(CACHE_BASE_PATH, hashedAppId);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment