Reputation: 39524
On a MVC 4 / SQL 2012 / Entity Framework 5 application I have all files uploaded to the database into a single table.
I then have an action Get in a FileController as follows:
public partial class FileController : Controller {
public virtual ActionResult Get(Guid key) {
// Get file from database and return File
}
}
What cache strategy do I have available, in MVC, for these files?
Should I use ETags? How?
Can someone, please, help me out with this?
Thank You,
Miguel
Upvotes: 1
Views: 2764
Reputation: 2851
I do not think using etags is a wise option for identifying your filenames. As using them has its own pros and cons, read this article for more on etags.
There are several ways to cache the resultset, files in your case. But you need to be careful with this, when dealing with caching ORM objects, as generally the cached items are either removed or is not updated when there is some change in your files, You need to handle this part yourself. Either way, you can set the duration of the cached data upon which you need to refresh.
I have yet to come across a built-in support in .NET MVC4 for caching data, anyhow, you can always write your own customised API or use/modify existing ones to accomplish the required tasks. You might find this post helpful in this case. As for the case where you are using GUID keys for filenames, there is no such rule that states etags and uid's go side by side. You can always cache the data in the ways you want to. Take a look into Steve's blog as well, he is using entity framework with MVC.
EDIT:
Caching on clients browser is also a thin ice to walk on. It all depends on the type, duration, sensitivity and most importantly the users, let alone the performance of your application. As my very good friend once said: "Don't eat till you are hungry"...
For setting caches in clients browsers, you can do something like this in asp:
From the example of the article mentioned below:
<%@ OutputCache Duration='120' Location='Client' VaryByParam='none' %>
'This would save the cache for 120 seconds and cached data should not be saved on the server, it should be stored only on the client browser.'
More on exploring caches in this article and this one.
Upvotes: 2