Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System.Diagnostics;
using System.IO;
using System.Web.Caching;
using Newtonsoft.Json;;
namespace Sitecore.StaticAssets.Infrastructure
{
public class StaticAssetResolver
{
private readonly string _assetsJsonPath;
private readonly Cache _cache;
private const string CacheKey = "assetsJsonDictionary";
public StaticAssetResolver(string assetsJsonPath, Cache cache)
{
_assetsJsonPath = assetsJsonPath;
_cache = cache;
}
public string GetActualPath(string assetPath)
{
var assets = _cache.Get(CacheKey) as AssetCollection;
if (assets == null)
{
assets = GetAssetsFromFile();
_cache.Insert(CacheKey, assets, new CacheDependency(assetsJsonPath));
Trace.TraceInformation("Assets cache miss");
}
else
{
Trace.TraceInformation("Assets cache hit");
}
return assets[assetPath];
}
private AssetCollection GetAssetsFromFile()
{
return JsonConvert.DeserializeObject<AssetCollection>(File.ReadAllText(_assetsJsonPath));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment