[英]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. One approach which I have used in the past is to use a client-side cookie to maintain a basket session, this means 我过去使用的一种方法是使用客户端cookie来维护购物篮会话,这意味着
There are a couple of small caveats with this approach as well 这种方法也有一些小的警告
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:
与将其存储在会话中相比,您将拥有一些很大的优势:
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.