使用playframework实现购物车的建议 [英] advice on implementing a shopping cart using playframework

查看:124
本文介绍了使用playframework实现购物车的建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习使用 playframework 编写代码来实现 webstore 来销售物品。我已经实现了 Admin 区域使用 crud 安全 modules.Now,我想创建一个购物车,用户可以添加商品并继续结帐。

I am learning to use playframework by writing code to implement a webstore for selling items.I have implemented the Admin area using the crud and secure modules.Now, I want to create a shopping cart to which a user can add items and proceed to checkout .

我的知识电子商务很少,但我已经阅读了一些教科书,这些教科书使用 servlets 来实现购物车和一些网店功能。在书中,购物车用来保持设置 CartItem ,每个包含 Product 的实例和数量。之后,用户将商品添加到购物车,购物车存储在用户会话

My knowledge of ecommerce is minimal,but I had gone through some textbooks which implement shopping carts and some webshop functionality using servlets.In the books ,the cart used to keep a Set of CartItems ,each of which contained an instance of the Product and quantity.After,the user added items to cart,the cart was stored in user session.

因此,只要用户进入购物车详细信息页面,它就会显示所有添加的项目。只有当会话被清除时(由于服务器中定义的会话超时或者订单被放置) CartItem 已从 ShoppingCart 中删除​​。

So,anytime the user went to the cart details page,it showed all the added items.Only when the session was cleared,(either due to session timeout as defined in the server,or when the order was placed)the CartItems were removed from the ShoppingCart.

我想,我可以使用playframework中的Cache来执行上述操作。将CartItem添加到ShoppingCart实例后。我可以

I guess ,I can use the Cache in playframework to do the above.After adding a CartItem to ShoppingCart instance .I can

shopcart.add(mycartItem);
Cache.set(session.getId(), shopcart);
..

以后,在另一页,我可以检索,购物车及其内容,处理它们并清除购物车。

and later,in another page ,I can retrieve,the cart and its contents,process them and clear the cart.

ShopCart cart = Cache.get(session.getId(),ShopCart.class);
Set<CartItem> items = cart.getCartItems();
processOrder(items,userinfo);
...
cart.clearItems();

这是正确的方法吗?如果我的想法不正确,请帮助我提出建议。

Is this the right way to go about this?If the way I am thinking is not correct,please help me with suggestions.

推荐答案

错了。很抱歉这么严厉,但你正在做的事情可能会失败。

Wrong. Sorry for being so harsh, but what you are doing may fail horribly.

游戏是无共享和无国籍的。所有相关数据必须存储在数据库中。这意味着您必须在用户添加新项目时保留购物车的数据,并在交易完成后将其删除。

Play is share nothing and stateless. All relevant data MUST be stored in the database. That means that you must persist the data of the shopping cart when the user adds a new item, and remove it once the transaction finishes.

为什么不能使用缓存?因为缓存是易变的,并且可以在没有先前警告的情况下移除其中的元素。这意味着您的缓存可能随时返回null(找不到项),并且您的代码中出现NullPointerException,并且您的用户丢失了购物车。

Why can't you use the cache? Because the cache is volatile and the elements in it may be removed without previous warning. That means that at any moment your cache may return a null (item not found) and you get a NullPointerException in your code and your user has lost the cart.

缓存是只是为了帮助表现,但你不能指望它总是拥有你需要的所有物品。

Cache is there only to help performance, but you can't expect it to have all the items you needs always.

你应该将你的ShopCart变成一个实体(如果它不是一个实体) )并添加如下内容:

You should turn your ShopCart into an entity (if it's not one) and add to it something like this:

public static ShopCart findCart(String sessionId){
   ShopCart sc = Cache.get(session.getId(),ShopCart.class);
   if(sc == null){
      sc = ShopCart.findCurrentUserCart(); //implement to find the cart of the current user
      Cache.set(sessionId,sc);
   }
   return sc; 
}

然后在处理之前使用此方法检索购物车。

Then use this method to retrieve the cart before processing.

这篇关于使用playframework实现购物车的建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆