Last active
June 25, 2020 00:26
-
-
Save soen/48c215e3bfd934c967139438eca19c37 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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