(system.webServer/staticContent/clientCache is for static resources (files))
you can create your own implementation of CachedPartial, something like this:
using System;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using Umbraco.Web;
using System.Web;
using System.Runtime.Caching;
public static class CachedPartialExtensions
{
//(From version 7 there is an overload for CachedPartial that allows the key to be passed in)
public static IHtmlString CachedPartial(
this HtmlHelper htmlHelper,
string partialViewName,
object model,
int cachedSeconds,
bool cacheByPage = false,
bool cacheByMember = false,
ViewDataDictionary viewData = null,
Func<object, ViewDataDictionary, string> contextualKeyBuilder = null);
{
var newCacheKey = "fpc-"; //prefix to know which keys to clear on page publish (in Bootstraper.cs file)
newCacheKey += partialViewName;
if (cacheByPage)
{
newCacheKey += "page-" + UmbracoContext.Current.PageId;
}
if (!string.IsNullOrEmpty(cacheKey))
{
newCacheKey += "key-" + cacheKey;
}
var result = MemoryCache.Default.Get(newCacheKey) as MvcHtmlString;
if(result == null)
{
result = htmlHelper.Partial(partialViewName, model, viewData);
MemoryCache.Default.Add(new CacheItem(newCacheKey, result), new CacheItemPolicy
{
AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(cachedSeconds)
});
}
return result;
}
}
Thanks, it looks interesting I will definitely try it. Is it possible to manage all static elements? For example, I want ALL the images(.png or .jpg) on the site to be stored in the cache for 3 days.
(For example, cache-control: max-age=120 means that the returned resource is valid for 120 seconds, after which the browser has to request a newer version)
Static files caching Umbraco 7/8
Hello! Have anyone good practice with static files in Umbraco? Right now I have only two methods.
1) use
@Html.ChachedPartial()
2) add in web.config. (here I very much doubt that this is the best method )
I'll be greateful for any help.
Hello llyas,
(system.webServer/staticContent/clientCache is for static resources (files))
you can create your own implementation of CachedPartial, something like this:
The use case for this would be:
you will be able to provide your own keys for cache:
Thanks, it looks interesting I will definitely try it. Is it possible to manage all static elements? For example, I want ALL the images(.png or .jpg) on the site to be stored in the cache for 3 days.
Try to use Client side caching Easy way to add client side caching to site - add this section to your Web.config:
(For example, cache-control: max-age=120 means that the returned resource is valid for 120 seconds, after which the browser has to request a newer version)
is working on a reply...