I'm looking for some advice on the best ways to cache strings returned from an API, local file contents and/or other sources external to the database.
I've already achieved what I set out to do and works flawlessly. The only thing now is, I'm left wondering if there could have been a far better and potentially native way of doing this. So basically I'm using a combination of DevTrends MvcDonutCaching and the Client Dependency version ID. See my code below:
var clientDependencyVersion = ClientDependency.Core.Config.ClientDependencySettings.Instance.Version;
var cacheManager = new DevTrends.MvcDonutCaching.OutputCacheManager();
var cacheName = $"CacheExample-{clientDependencyVersion}";
var cacheItem = cacheManager.GetItem(cacheName);
string value;
if (cacheItem == null)
{
value = System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath(path));
cacheManager.AddItem(cacheName, new DevTrends.MvcDonutCaching.CacheItem() { Content = value }, DateTime.Now.AddHours(2));
}
else
{
value = cacheItem.Content;
}
return value;
Obviously, it goes without saying, changing the Client Dependency version ID produces another unique cache key and retrieves some fresh new data from our source.
Any tips or advice would certainly be appreciated.
I think your looking for AppCaches.RuntimeCache (Umbraco.Core.Cache)
string fromCache = AppCaches.RuntimeCache.GetCacheItem<string>("stringfromcache", () =>
{
return "This string is from cache"; // Your string, object, etc you want to cache
}, TimeSpan.FromMinutes(30)); //clear cache after 30min
You can set the amount of time you would like the data to be cached but this is optional.
Caching strings/objects & best practices
Hi There,
I'm looking for some advice on the best ways to cache strings returned from an API, local file contents and/or other sources external to the database.
I've already achieved what I set out to do and works flawlessly. The only thing now is, I'm left wondering if there could have been a far better and potentially native way of doing this. So basically I'm using a combination of DevTrends MvcDonutCaching and the Client Dependency version ID. See my code below:
Obviously, it goes without saying, changing the Client Dependency version ID produces another unique cache key and retrieves some fresh new data from our source.
Any tips or advice would certainly be appreciated.
Thanks,
Andy
Hi Andy,
I think your looking for AppCaches.RuntimeCache (Umbraco.Core.Cache)
You can set the amount of time you would like the data to be cached but this is optional.
Best regards,
iNETZO
Great! Looks like a much cleaner, simpler and the native solution that I was originally searching for. Thanks for that
is working on a reply...