-4

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.

  • 1
    What exactly are you asking here? – maccettura Nov 14 '17 at 22:09
  • I need to know how to pass the data stored in db each time i run the application. That is in one of my controller i want the data already stored through it to be passed each time i run the restart the application. – Muthu Raman Nov 14 '17 at 22:13
  • 1
    pass the data to where ? – Shyju Nov 14 '17 at 22:17
  • public ActionResult Create(Model xxx) { i create my dataset here using LiteDB and it gets stored in DB. Now inside this i have two methods called to act on the created dataset as soon as they are created in the DB. That data works on it only on that particular run. I want the data in the DB to pass through those methods each time i run the application. } – Muthu Raman Nov 14 '17 at 22:31

1 Answers1

1

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();
    }
}
zaitsman
  • 8,984
  • 6
  • 47
  • 79