1

I'm building a web app which is selecting a lot of information from the database, and returns it. I'd like to store the returned value from the method which returns the tables from the database for 15 minutes, but it doesn't work using a OutputCache Filter:

[OutputCache(duration=900)]
private DataTable[] GetInfoTables() {...}

the method does work, just find, but when I add anything to one of the tables (after first logging into site and view them) it refreshes immediately and I'd like it to cache for 15 minutes. I cannot cache the public ActionResult Index() because it checks for Sessions value. Thanks

mason
  • 31,774
  • 10
  • 77
  • 121
A.naam
  • 375
  • 1
  • 5
  • 16
  • 2
    Did you look at the documentation for [OutputCache](https://learn.microsoft.com/en-us/dotnet/api/system.web.caching.outputcache?view=netframework-4.7.2)? It doesn't say that it can just cache the results of any method. You should probably look into the [Cache](https://learn.microsoft.com/en-us/dotnet/api/system.web.caching.cache?view=netframework-4.7.2) object instead. – mason Mar 20 '19 at 20:55
  • According to [this](https://learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/controllers-and-routing/improving-performance-with-output-caching-cs) article it does work for string methods – A.naam Mar 20 '19 at 21:01
  • No, it doesn't say that. An action methods on a controller is not like any regular method. The framework calls the action method, and can check before it does so whether there is cached content or not. If you're calling a random method, then the responsibility is on you to implement the cache handling. They help you out with a place to store the objects you with to cache, but it's up to you to wire it up properly. Reread all of that documentation more carefully. – mason Mar 20 '19 at 22:23
  • Do you still need help ? – Mohamed Elrashid Mar 21 '19 at 10:45

2 Answers2

4

Summary :

  • I'm not aware of a way to :

  • make OutputCache work with normal C# method

  • but you can hack around it :

  • you can use MemoryCache for normal C# method

Solution

  • Import System.Runtime.Caching (or use VS Intense )

  • Then:

     private DataTable[] GetInfoTables()
        {

            ObjectCache cache = MemoryCache.Default;
            var dataTable = cache["DataTable"] as DataTable[];
            if (dataTable == null)
            {
                CacheItemPolicy policy = new CacheItemPolicy();
                policy.AbsoluteExpiration = DateTimeOffset.UtcNow.AddMinutes(5);
                dataTable =  getDataTable() ;
                cache.Set("DataTable", dataTable , policy);
            }

            return dataTable ;
        }

Screenshot

I test the Solution using EF and sql server

enter image description here

enter image description here

References

Community
  • 1
  • 1
Mohamed Elrashid
  • 8,125
  • 6
  • 31
  • 46
0

Output Cache stores the rendered HTML output. Therefore, you cannot use it for functions, methods etc.

I started using CacheManager library, which gives a unique interface for caching data in different cache providers. I'd suggest to have a look.

Other direct options are discussed in this question

garo cm
  • 61
  • 5