简体   繁体   English

ASP.Net MVC会话缓存

[英]ASP.Net MVC Session Cache

I'm looking to improve performance by using some king of cache. 我希望通过使用一些缓存之王来提高性能。 For example users login and access an appointment system, I would like to Cache this data, query this data from the cache and then update the cache should new appointments be added 例如,用户登录并访问约会系统,我想缓存此数据,从缓存中查询此数据,然后在添加新约会时更新缓存

For example, I was writing this but I think this is application caching and not session (dont want other users seeing other peoples data) 例如,我写这篇文章,但我认为这是应用程序缓存而不是会话(不希望其他用户看到其他人的数据)

public IEnumerable<ScheduledItem> GetCache(bool reload, DateTime start, DateTime end, Account account)
        {
            if (System.Web.HttpContext.Current.Cache["ScheduleData"] == null)
            {
                var model = _eventService.FindAllEventsForAccountId(account.Id, start.Date, end.Date)
               .Where(IsAppointmentNotCancelled)
               .Select(a => new Models.ScheduledItem()
               {
                   id = a.Id,
                   start = a.StartTime.ToString("H:mm:ss").Length < 8 ? string.Format(a.StartTime.ToString("yyyy-MM-dd {0}H:mm:ss"), 0) : a.StartTime.ToString("yyyy-MM-dd H:mm:ss"),
                   end = a.EndTime.ToString("H:mm:ss").Length < 8 ? string.Format(a.EndTime.ToString("yyyy-MM-dd {0}H:mm:ss"), 0) : a.EndTime.ToString("yyyy-MM-dd H:mm:ss"),
                   backgroundColor = GetColour(a),
                   title = GetTitle(a, account),
                   textColor = "#fff",
                   AppointmentType = GetType(a),
                   resourceId = GetResource(a)

               }).ToList();


                return model;
            }

            return null;
        }

Any pointers, examples would be great 任何指针,例子都会很棒

There are different caching strategies. 有不同的缓存策略。 You must decide, which one suits better for your application. 您必须决定哪一种更适合您的应用。 The easiest way to enable cache is to set up page output caching (maybe varied by param/header/etc). 启用缓存的最简单方法是设置页面输出缓存(可能由param / header / etc改变)。 This is done by special ASP.NET MVC OutputCache attribute. 这是通过特殊的ASP.NET MVC OutputCache属性完成的。

If you need to do more specific caching, then you can implement it by yourself. 如果您需要进行更具体的缓存,那么您可以自己实现它。 There are different ways how to do it. 有不同的方法如何做到这一点。 First of all, you have to decide, where and how to store cached data. 首先,您必须决定,存储缓存数据的位置和方式。 Would it be common for all users? 对所有用户来说都是常见的吗? You can put some cache data into a Session to cache it for a single user and for this particular Session . 您可以将一些缓存数据放入Session中,以便为单个用户和此特定Session缓存它。 Please notice, if you have a lot of users, your app would not be able to handle this load. 请注意,如果您有很多用户,您的应用将无法处理此负载。 I actually would not recommend you to do it. 我实际上不建议你这样做。

If you have a common data for all users, you could use standard HttpRuntime.Cache object to store data there. 如果您拥有所有用户的公共数据,则可以使用标准HttpRuntime.Cache对象在其中存储数据。 It is just a key-value storage as well. 它只是一个键值存储。 You could store user-specific data in it by composing special key like SomeData_{UserID} . 您可以通过组合SomeData_{UserID}等特殊键来存储用户特定的数据。 Problem - it makes your application stateful, when normally your application should be stateless and all the state should be in storages like SQL or Cache Server or Session Server etc. If your app is stateless - you can easily scale it to many instances to accommodate high-load. 问题 - 它使您的应用程序有状态,通常您的应用程序应该是stateless并且所有状态应该在SQL或缓存服务器或会话服务器等存储中。如果您的应用程序是stateless - 您可以轻松地将其扩展到许多实例以适应高-加载。

There are a lot of NoSQL key-value stores that could be used as an external cache. 有很多NoSQL键值存储可以用作外部缓存。 It could be shared across different applications and instances. 它可以在不同的应用程序和实例之间共享。 I personally like Redis . 我个人喜欢Redis This one is recommended by Microsoft for high-load projects. Microsoft建议将此项用于高负载项目。

Sometimes caching leads to data denormalization. 有时缓存会导致数据非规范化。 You would have to always keep cache up-to-date. 您必须始终保持缓存最新。 So before developing caching strategy I'd recommend you to think, what could be optimized in your application. 因此,在开发缓存策略之前,我建议您考虑一下,在您的应用程序中可以优化什么。 Usually the bottle-neck is database interactions. 通常瓶颈是数据库交互。 Try to analyze database queries and see what could be improved there. 尝试分析数据库查询,看看哪些可以改进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM