Skip to content

Instantly share code, notes, and snippets.

@soen
Last active June 25, 2020 00:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soen/48c215e3bfd934c967139438eca19c37 to your computer and use it in GitHub Desktop.
Save soen/48c215e3bfd934c967139438eca19c37 to your computer and use it in GitHub Desktop.
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