How to cache data in a MVC application?

0 votes
I have read lots of information about page caching and partial page caching in an MVC application. However, I would like to know how you would cache data.

In my scenario, I will be using LINQ to Entities (entity framework). On the first call to GetNames (or whatever the method is) I want to grab the data from the database. I want to save the results in cache and on the second call to use the cached version if it exists.

Can anyone show an example of how this would work, where this should be implemented (model?) and if it would work.

I have seen this done in traditional ASP.NET apps typically for very static data.
Oct 25, 2018 in Database by Neha
• 6,280 points
124 views

1 answer to this question.

0 votes

Reference the System.Web dll in your model and use System.Web.Caching.Cache

    public string[] GetNames()
    {
      string[] names = Cache["names"] as string[];
      if(names == null) //not in cache
      {
        names = DB.GetNames();
        Cache["names"] = names;
      }
      return names;
    }

A bit simplified but I guess that would work. This is not MVC specific and I have always used this method for caching data.

answered Oct 25, 2018 by Frankie
• 9,810 points

Related Questions In Database

0 votes
1 answer

How to do a batch insert in MySQL

You can try out the following query: INSERT ...READ MORE

answered Sep 10, 2018 in Database by darklord
• 6,140 points
132 views
+2 votes
1 answer

How to export data from MySql to a CSV file?

If you are using MySql workbench then ...READ MORE

answered Jan 4 in Database by Priyaj
• 56,160 points
62 views
0 votes
1 answer

How to display the queries executed by a particular user in MySQL?

From the version 5.1.7 onward, MySQL allows ...READ MORE

answered Mar 7 in Database by Mishti
• 480 points
27 views
0 votes
2 answers
0 votes
1 answer

Authenticate on an ASP.Net Forms Authorization website from a console app

Essentially, we need to record a regular ...READ MORE

answered Sep 20, 2018 in IoT (Internet of Things) by Annie97
• 2,190 points
30 views
0 votes
1 answer

powerbi embeded in .net core 1.1

See if it helps: https://www.nuget.org/packages/PowerBI.NetStandard ...READ MORE

answered Nov 28, 2018 in Power BI by Upasana
• 8,410 points
68 views
0 votes
1 answer

Power BI Embedded : Importing Packages

This should help you out. https://www.nuget.org/packages/PowerBI.Ne ...READ MORE

answered Apr 3 in Power BI by Shubham
• 13,110 points
14 views
0 votes
1 answer

How to schedule a job for SQL query to run daily?

down voteaccepted Expand the SQL Server Agent node ...READ MORE

answered Oct 25, 2018 in Database by Frankie
• 9,810 points
40 views
0 votes
1 answer

How to connect to MySQL Database?

using MySql.Data; using MySql.Data.MySqlClient; namespace Data { ...READ MORE

answered Oct 12, 2018 in Database by Frankie
• 9,810 points
68 views