Is outputcache only valid at controller level?

Asked

Viewed 169 times

2

I’ve been reading the article: http://www.asp.net/mvc/overview/older-versions-1/controllers-and-routing/improving-performance-with-output-caching-cs

And I started using the:

   [OutputCache(Duration = 60, VaryByParam = "none")]

When I put it on the controller, I can clearly tell the difference.

But when I’m using it in a library that’s called by mine controller, it seems that the cache does not happen.

The code is as follows::

    [OutputCache(Duration = 60, Location = OutputCacheLocation.ServerAndClient, VaryByParam = "none")]
    internal IEnumerable<CPUStats> ProcessarStatsSQL()
    {
        var DateQuery = db.servidorSQL.ToList().Select(o => new CPUStats
        {
            Data = DateTime.Parse(o.Data, new CultureInfo("en-US")),
            CPU = Double.Parse(o.CPU,new CultureInfo("en-US")),
            RAM = Double.Parse(o.RAM, new CultureInfo("en-US")),
            Disco = Double.Parse(o.Disco, new CultureInfo("en-US"))
        });

        return DateQuery;
    }

 [OutputCache(Duration = 60, VaryByParam = "none")]
        public DateTime Data_SQL()
        {
            var DateQuery = ProcessarStatsSQL();

            var Data = DateQuery.OrderByDescending(x => x.Data).Take(1).ToList();

            return (DateTime)Data.FirstOrDefault().Data;
        }
 [OutputCache(Duration = 60, VaryByParam = "none")]
        public Double IO_SQL()
        {
            var DateQuery = ProcessarStatsSQL();

            var IO = DateQuery.OrderByDescending(x => x.Data).Take(5).ToList();

            return (double)IO.Average(a => a.Disco);
        }

Namely the IO_SQL() and Data_SQL() should not use the cache previously made?

But in debug I see that it processes the query in Database for each IO_SQL() and Data_SQL()

I can use in an Htmlhelper?

  [OutputCache(Duration = 60, VaryByParam = "none")]
    public static IHtmlString VerificaRAM(this HtmlHelper helper)
  • Have you ever taken the test using a Duration longer and no debug? Debug forces the expiration of cache.

  • 1

    already...cacheoutput does not work in a class outside the controller

1 answer

2


Like the name says, it’s an output cache. This means that if you receive the same request that has been made before, instead of processing all this, it takes the final result - which is already in the cache - and sends it to the requester.

Obviously, if the request is different or if the result is no longer in the cache for any reason, it will not be used. The cache is a mechanism of controller, not of model. He is bound by actions, no to auxiliary methods.

The question does not make clear but it seems to me that first the request is made one way and then another way (need to call another method), so there is no cache.

The tutorial shown clearly indicates that the cache is something else and should be used differently. Just read it.

It is not database cache. Even if it was, in darlings different ones would also not have cache. Anyway the question does not give enough information even to know if this statement is true.

Cache is not something magical. Alias cache solves problems of high traffic sites, in all other cases, although nothing critical, the cache arrives to disturb.

  • made an edit on the question, but if I have a class that returns me a number, example 50 (based on seconds), if I put this class with outputcache it should not always return 50 until the cache time is over?

  • @Dorathoto depends on how it is used. As I said, the question does not make clear the exact context of how it is being used.

  • @Randrade is, I’ll settle this, it’s been vacant

  • @bigown as he said in the question "But when I’m using it in a library that is called by my controller, it seems that the cache doesn’t happen." I have a library and call it in my controller, in an html.helper, etc and it seems q does not cache..

  • @Dorathoto I edited to talk about it. It seems that you think the cache is in model and it’s not.

  • so I can only use it in Controller? then I use html.Helper to create and so it won’t work.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.