简体   繁体   English

我应该如何维护购物车以避免频繁前往服务器?

[英]How should I maintain the cart to avoid frequent trips to the server?

My project in MVC is an order carting project. 我在MVC中的项目是订单购物项目。 The carting is done with the following structure. 通过以下结构完成运输。 There is an order object which holds: 有一个订单对象,其中包含:

cOrder -> List<cOrderItem>

cOrderItem-> List<dressing>, List<topping>, List<specialInst>

I am holding it in Session . 我在Session拿着它。 Is it the right approach to hold it in Session without losing performance. 将其保存在Session而不损失性能是正确的方法吗? Or should I follow an alternate approach? 还是应该采用其他方法?

I presume what you mean by "trips to the server" you really mean "trips to the DB" because whenever are dealing with the Session you are at the server anyway. 我假设您所说的“到服务器的行程”实际上是“到数据库的行程 ”的意思因为无论何时处理Session您无论如何都在服务器上。

The Session will do the job, however, there are a couple of caveats that might be a problem Session将完成这项工作,但是,有一些警告可能是一个问题

  • Session is not persistent, it's all stored in memory. Session不是持久性的,它全部存储在内存中。 This means that there is the potential for you to lose the session at any point eg app pool recycle. 这意味着您有可能在任何时候丢失会话,例如,应用程序池回收。
  • Given it's not persistent, this means that a users cart is only maintained within a single session ie if the users session expires they will effectively lose their cart. 鉴于它不是持久性的,这意味着用户购物车仅在单个会话中维护,即,如果用户会话过期,则他们将有效地丢失其购物车。

One approach which I have used in the past is to use a client-side cookie to maintain a basket session, this means 我过去使用的一种方法是使用客户端cookie来维护购物篮会话,这意味着

  1. It's persistent storage so it's retained across sessions (not just a single session) 它是永久性存储,因此可以会话保留(而不仅仅是单个会话)
  2. Nothing is stored on the server 什么都没有存储在服务器上
  3. It's very quick 很快

There are a couple of small caveats with this approach as well 这种方法也有一些小的警告

  • Browsers need to have cookies enabled 浏览器需要启用Cookie
  • Users can clear their cookies (as such, losing their basket) 用户可以清除其Cookie(因此,会丢失其购物篮)

However, I've found that these issues are very rarely a problem as most people enable cookies in their browser and most users very rarely clear their cookies (and the chances of them doing it during a session is relatively low). 但是,我发现这些问题很少出现,因为大多数人在浏览器中启用cookie,而大多数用户很少清除cookie(并且会话期间这样做的机会相对较低)。

Yes and no. 是的,没有。 :) :)

Placing stuff that needs to survive across requests in the server session is fine for smaller websites, but it does limit scaling; 对于较小的网站,可以将需要在请求中生存的内容放置在服务器会话中,这对于较小的网站来说很好,但是这样做确实限制了扩展性。 it will be more difficult to distribute the load over multiple server machines, because either the session would need to be replicated to each machine, or all requests from a particular user would need to be routed to the same machine. 由于将会话需要复制到每台计算机,或者来自特定用户的所有请求都需要路由到同一台计算机,因此将负载分配到多台服务器计算机上将更加困难。 There are out-of-the-box solutions for this however, it is not that big a deal. 这有出的现成解决方案,但是,这不是什么大不了的事。

But holding stuff in the session has other downsides. 但是,在会议中保留内容还有其他弊端。 The session timeout for example will kill all the products that were in the cart if the user decides to for for lunch in the middle of his shopping spree. 例如,如果用户决定在购物狂潮中午餐,会话超时将杀死购物车中的所有产品。

Ideally, these days, I would be looking at storing the cart locally, in localStorage . 理想情况下,如今,我希望将购物车本地存储在localStorage中 Support for this is increasing rapidly. 对此的支持正在迅速增加。 You would have some big advantages over storing it in the session: 与将其存储在会话中相比,您将拥有一些很大的优势:

  1. Less memory needed on the server(s). 服务器上所需的内存较少。
  2. More scalable as there is no server state; 由于没有服务器状态,因此更具可扩展性; each request may be routed to any server that has capacity at that moment. 每个请求都可以路由到当时具有容量的任何服务器。
  3. No session timeouts killing your shopping cart. 没有会话超时会杀死您的购物车。
  4. Local code (Javascript) can access the shopping cart without having to make trips to the server and back. 本地代码(Javascript)可以访问购物车,而不必往返服务器。

Option 4 is becoming more important as of late. 备选方案4最近变得越来越重要。 If you have your data available locally there are many things you can do with it later on that would be more difficult otherwise. 如果您的数据在本地可用,那么以后可以做很多事情,否则将更加困难。 Working offline for example could be done that way. 例如,可以通过这种方式离线工作。 You can fill your cart offline and only need to be connected to actually place the order with the webshop. 您可以离线填写购物车,只需要连接即可在网上商店实际下订单。

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

相关问题 在开发XNA应用程序时,如何避免频繁的int / float / double强制转换? - How can I avoid frequent int/float/double casts when developing an XNA application? 如果我像这样频繁地运行查询,我应该如何处理 MySQL 连接? - How should I be handling MySQL connections if I am running queries as frequent as this? 如何缓存数据集以停止往返数据库的旅程? - How do I cache a dataset to stop round trips to db? 如何在单个语句中加载两个导航属性,以避免数据库往返(如果有) - How to load two navigation property in a single statement in order to avoid database round trips(if any) 如何避免频繁 HttpRequest 导致的 DbContext 线程问题? - How to avoid DbContext threading issues caused by frequent HttpRequests? 我应该拨打更频繁,更小的电话; 或者不那么频繁的大呼? - Should I make more frequent, smaller calls; or less frequent larger calls? 我应该始终保持内部异常吗? - Should I always maintain the inner exceptions? 如何避免服务定位器模式? 我是不是该? - How do I avoid the service locator pattern? Should I? 将文件上传到远程服务器,我该怎么办? - Upload file to a remote server, how should I? 未打开客户端而打开服务器时如何保持连接? - How to maintain a connection when the client is not opened and the server is?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM