OutputCache only valid at controller level?

2

I have read the article: link

And I started using the:

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

When I put it in the controller I notice the difference clearly.

But when I'm using a library that is called by my controller , it does not seem to be the case.

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);
        }

Is the IO_SQL() and Data_SQL() should not use the cache already done previously?

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

Can I use an HtmlHelper?

  [OutputCache(Duration = 60, VaryByParam = "none")]
    public static IHtmlString VerificaRAM(this HtmlHelper helper)
    
asked by anonymous 18.03.2016 / 15:02

1 answer

2

As the name itself is an output cache. This means that if you receive the same request as before, instead of processing all of 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 the exploit will not occur. The cache is a controller mechanism, not the model mechanism. It is bound to actions , not to helper methods.

The question does not make it 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 caching. Even if it were, in different queries would also not cache. Anyway the question does not give enough information to even know if this statement is true.

Cache is not something magic. Alias cache solves problems of very high traffic sites. In other cases, although nothing critical, the cache gets in the way.

    
18.03.2016 / 15:18