Media files caching problem

George Tselios asked on March 24, 2020 18:38

Dear All,

In our current project (Kentico 12.0.51 MVC dev model), each time we need to store a reference to Media Library's file in our Page Types, we create a field of Text Data Type and Media Selection Form Control.

Also, in the Settings Application, we have enabled the "Use permanent URLs" for media, under Content > Media section.

In this way, when we access the field in the API, all media file references are in the form: "/getmedia/dbc64d58-de28-4769-984c-cc55a13ab337/image.jpg?width=1136&height=639&ext=.jpg".

Instead of using the above value (url) directly on our views, we extract the GUID and run the following query in order to get the entire MediaFileInfo:

var file = MediaFileInfoProvider
           .GetMediaFiles()
           .OnSite(<siteName>)
           .WhereEquals("FileGUID", fileGUID)
           .FirstOrDefault();

We needed to get the whole MediaFileInfo structure instead of only the media file url, so as to introduce the alt attribute (MediaFileInfo.FileTitle) in the img tag.

Moreover in the Settings Application, under System > Performance in the "Server file caching" section there is the "Cache files (minutes):" setting set to 10 minutes. We expected that this setting would automatically cache queries against the MediaFileInfoProvider but it does not.

We have the following questions:

  1. Is there any helper method that would provide the FileTitle that is associated with a Media File (e.g. an Image) directly on a View? In this way we would not have to make a query against the MediaFileInfoProvider.

  2. What does the "Server file caching" do, and how can we make use of it?

  3. What other way do we have in order to cache the requests for MediaFileInfo structures?

Thanks in advance,
George

Correct Answer

Matt Nield answered on March 25, 2020 00:44

  1. Is there any helper method that would provide the FileTitle that is associated with a Media File (e.g. an Image) directly on a View? In this way we would not have to make a query against the MediaFileInfoProvider. As far as I know, you're always going to need to make that initial call to the MediaFileInfoProvider and there is no shortcut there. What you're doing sounds fine, but you just need to add caching to improve performance.

  2. What does the "Server file caching" do, and how can we make use of it? This is a setting predominantly used in the portal engine for caching pages. The MVC application is not configured to look at this value unless you tell it to and use it to drive your own cache logic. In general, cache in the MVC application is a roll your own affair, but there is a guide to it in the Kentico docs Caching on MVC sites

  3. What other way do we have in order to cache the requests for MediaFileInfo structures? Perhaps create a MediFileInfoRepository in your solution which you cha use to do this work for you and the implement a caching strategy in here. Sean Write has a bunch of great articles on caching that may be of help here.

1 votesVote for this answer Unmark Correct answer

Recent Answers


George Tselios answered on March 26, 2020 12:18 (last edited on March 26, 2020 12:39)

Hi Matt,

Thank you for your answers.

What I don't really know is if we apply caching on each individual call to the MediaFileInfoProvider will result in large memory consumption on the Web Server. Imagine that in our instance we have 3 different Kentico sites with about 250 - 300 images each. I believe that the MediaFileInfo structure has a small memory footprint but I don't really know.

Also, is there any other picker (form Control) for Media Library items that would return a GUID instead of a string in order to remove the string parsing code that we use to "extract" the GUID for paths like: "/getmedia/dbc64d58-de28-4769-984c-cc55a13ab337/image.jpg?width=1136&height=639&ext=.jpg"?

Thanks in advance,
G. Tselios

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.