简体   繁体   English

本地存储与 Session 存储需要建议

[英]Local Storage Vs Session Storage Advice needed

I am building a mini-swiggy application for selling pizza only with basic functionalities.我正在构建一个小型 swiggy 应用程序,仅用于销售具有基本功能的披萨。 The basic app is available here .基本应用程序可在此处获得。 This app doesn't have a login/register feature which I want to add to this app.此应用程序没有我想添加到此应用程序的登录/注册功能。 I want to learn session storage while implementing this.我想在实现这个的同时学习 session 存储。

My question is:我的问题是:

  • should I make an API call for every time the user clicks on the add button, and update the cart at the database?每次用户单击添加按钮时,我是否应该调用 API 并在数据库中更新购物车? (Currently being handled by react context API using local storage, there is no database as such for now. On placing the order I am simply flushing the local storage). (目前由使用本地存储的反应上下文 API 处理,目前没有这样的数据库。在下订单时,我只是刷新本地存储)。

  • Now the database is required for features现在功能需要数据库

    • login/register登录/注册
    • order Summary for each user每个用户的订单摘要
    • session management session管理
    • admin roles etc.管理员角色等
  • In could just keep using local storage for updating the carts and only make an API call when the user places an order. In 可以继续使用本地存储来更新购物车,并且只在用户下订单时调用 API。 And I can flush the cart from local storage once the order is placed.(This is I feel is more optimal since it will be better for performance and reduce the server load as well.)一旦下订单,我就可以从本地存储中刷新购物车。(我觉得这是更优化的,因为它会更好地提高性能并减少服务器负载。)

Can all this be achieved without using redux?如果不使用 redux,这一切都可以实现吗?

Kindly help, considering I want to make it an app with best practices, Also if you have some good resources please share, I am kinda newbie here.请帮忙,考虑到我想让它成为一个具有最佳实践的应用程序,另外,如果你有一些好的资源,请分享,我在这里有点新手。

In case you could think of any other features please let me know:)如果您能想到任何其他功能,请告诉我:)

I suggest using NextAuth for session management and authentication with combination of Strapi CMS it will be strong and easy for your project.我建议将NextAuth用于 session 管理和身份验证,并结合Strapi CMS ,它对您的项目来说既强大又容易。

  1. You should update the database every time the user clicks on the add button, and update the cart at the database.每次用户单击添加按钮时,您都应该更新数据库,并更新数据库中的购物车。
  2. You can use different user authentication methods like using the google sign-in method or you can create a custom user authentication using JWT(JSON Web Token).您可以使用不同的用户身份验证方法,例如使用 google 登录方法,也可以使用 JWT(JSON Web 令牌)创建自定义用户身份验证。
  3. Having user cart info at local storage until the user places the order wouldn't be a good idea, cart info is critical user data, and you can't update the user cart to all the other devices where the user has logged in, and the cart data will also get vanished once user deletes the browser data, which could be a signification loss to the business.在用户下订单之前将用户购物车信息保存在本地存储并不是一个好主意,购物车信息是关键的用户数据,您无法将用户购物车更新到用户登录的所有其他设备,并且一旦用户删除浏览器数据,购物车数据也将消失,这可能对业务造成重大损失。
  4. Instead of redux you can use react reducer with react context.您可以使用带有反应上下文的反应减速器,而不是 redux。

Very interesting question, I will answer only one part of it, otherwise it will get too long to read:)很有意思的问题,我只回答一部分,不然看的太长了:)

Whether you need to make an API call to persist the cart server-side or just do it only once the order has been placed is a question regarding the business logic of your app itself.您是否需要进行 API 调用以将购物车服务器端持久化,或者仅在下订单后才执行此操作,这是关于您的应用程序本身的业务逻辑的问题。

Ask a question, "Do I want my users to be able to add items to the cart and watch/update/edit its' content even if they log in from a different machine/browser?"问一个问题,“我是否希望我的用户能够将商品添加到购物车并观看/更新/编辑其内容,即使他们从不同的机器/浏览器登录?”

If the answer is yes then for sure you have to synchronize your cart with the server so it can be consistent always.如果答案是肯定的,那么您肯定必须将您的购物车与服务器同步,这样它才能始终保持一致。 For this purpose I would suggest having sort of a queue, which will have the last version of the locally modified cart, once the user did no action for 2000ms to your cart, you can safely make an API call and persist the information of the cart in your DB.为此,我建议建立一个队列,其中将包含本地修改的购物车的最新版本,一旦用户在 2000 毫秒内对您的购物车没有任何操作,您可以安全地进行 API 调用并保留购物车的信息在您的数据库中。

This will reduce the load to your server, because people can increment the number of items you have in the cart by clicking 20 times on the add button, in this case, instead of sending 20 requests to the server one at a time, it simply delays the process of persistent the cart, until the user stops interacting with your app for some X amount of milliseconds.这将减少服务器的负载,因为人们可以通过单击add按钮 20 次来增加购物车中的商品数量,在这种情况下,不是一次向服务器发送 20 个请求,而是简单地延迟持久化购物车的过程,直到用户停止与您的应用程序交互 X 毫秒。

to begin with, I want to say I really liked your CSS :).首先,我想说我真的很喜欢你的CSS :)。

You don't have to use a database or redux for making a cart system, but as you can guess they have good opportunities.您不必使用数据库redux来制作购物车系统,但您可以猜到他们有很好的机会。


DATABASE ADVANTAGES :数据库优势

  1. You can show the user's cart on all his devices.您可以在他的所有设备上显示用户的购物车。
  2. User's cart will be always here as long as he did not delete it.只要他没有删除它,用户的购物车就会一直在这里。
  3. You can reach the data on all components.您可以访问所有组件的数据。 (hard) (难的)
  4. It's very good practice for learning.这是非常好的学习实践。

DATABASE DISADVANTAGES :数据库缺点

  1. You need an API so, it's hard to set up.你需要一个 API 所以很难设置。
  2. Your code will be more complicated than other choices.您的代码将比其他选择更复杂。

REDUX ADVANTAGES REDUX 优势

  1. It's kinda easy to set up.设置起来很容易。 You don't need a new project.你不需要一个新项目。
  2. You can reach the data on all components.您可以访问所有组件的数据。 (medium) (中等的)

REDUX DISADVANTAGES REDUX 缺点

  1. Actually I really don't know any disadvantages of redux:) (pls tell me if you know)其实我真的不知道redux有什么缺点:)(如果你知道请告诉我)

LOCALSTORAGE ADVANTAGES本地存储优势

  1. Very easy很容易
  2. You can reach the data on all components.您可以访问所有组件的数据。 (easy) (简单的)

LOCALSTORAGE DISADVANTAGES本地存储的缺点

  1. You can't show a user's cart on another device您无法在其他设备上显示用户的购物车

I didn't separate session storage because it's the same as local storage .我没有分离session 存储,因为它与本地存储相同。 The only difference is session storage is per-tab .唯一的区别是 session 存储是per-tab

In my opinion , you should use a database .在我看来,你应该使用数据库 I know it's a basic app but you can learn a lot of things by doing it this way.我知道这是一个基本的应用程序,但你可以通过这种方式学习很多东西。

And these are my recommendations:这些是我的建议:

  1. cookies vs local vs session storage cookies vs 本地 vs session 存储

  2. ECommerce site with backend 带后端的电子商务网站

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

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