Headless cache in MediaReference
From v 11.12
To avoid unnecessary roundtrips to the ImageVault service to get an url to the selected media, the media reference object will now contain the urls for the selected media and their conversions. Not all possible urls will be stored in the property but the ones that can be requested based on channel and property settings will be stored.
If you do custom code based on the id and Effects in the MediaReference object, these urls are not stored in the media reference object and will be requested from the ImageVault service.
Inner workings
The MediaReference object has an added property, ChannelDisplayMediaItem that contains the cached data. This property is populated when the Optimizely page is saved and can be reused until the property settings are changed. The property contains all urls for all channels and settings. The property itself is used in the render template but can be used anywere after the page is saved. Below follows a small example.
@model MediaReference
@using ImageVault.EPiServer
var client = ImageVaultClient.GetPageClient();
ChannelDisplayMediaItem item = null;
if(Model != null)
{
var currentSettings = ViewContext.GetSettings<PropertyMediaSettings>();
// If we have cached data, and settings are unchanged, reuse cache
if (Model.ChannelDisplayMediaItem != null && Model.PropertyMediaSettings.Equals(currentSettings)) {
item = Model.ChannelDisplayMediaItem;
} else {
// Get media
var list = new List<MediaReference> {Model};
item = client.GetChannelMedia<ChannelDisplayMediaItem>(list, ViewContext).FirstOrDefault();
}
}
if(item != null)
{
<img src="@item.Url" />
}
ChannelDisplayMediaItem
The ChannelDisplayMediaItem itself contains the information for the selected media and it's channels. It contains three media properties, WebMedia, Thumbnail and Original.
The WebMedia property is the most often used and contains the url to the media with the selected effects.
The Thumbnail property contains the url to the thumbnail of the media, (always an image, even if the WebMedia refers to a video or something else).
The Original property contains the url to the original media.
To get the media for a channel, use the GetChannelMedia and pass the channel name to the method. The return value for the GetChannelMedia is also a ChannelDisplayMediaItem (it does not contain any sub channels).