How to pass already stored data in the database each time the website is run in ASP.net MVC?? I have a controller with methods, i want it to be executed for the already stored data each time i run the application. Also is there any way to change in such a way that this stored data get passed into the controller in a timely manner. Say i want this stored data to be passed into the methods in controller every five minutes once.
What you need is something like a cache
singleton.
e.g.
public interface IMyStuff{
string VeryImportantStringHere {get;}
}
public MyStuff :IMyStuff {
public string VeryImportantStringHere => {
var cached = HttpRuntime.Cache.Get(nameof(VeryImportantStringHere));
if(cached != null) // might need to store something else if you could have nulls in db
{
return cached ;
}
cached = .... // retrieved from DB however you do it - ADO.net, Linq2Sql, EF etc...
Cache.Insert(nameof(VeryImportantStringHere), cached , null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(5));
}
}
Then in your controller, using any DI:
public controller MyController : Controller{
private IMyStuff _ms;
public MyController(IMyStuff ms)
{
_ms = ms;
}
[HttpGet]
public ActionResult Whatever(){
var cached = _ms.VeryImportantStringHere; // cached now has the value.
return View();
}
}