某些 Meteor 数据源能存活多久? [英] How long do certain Meteor data sources live for?

查看:51
本文介绍了某些 Meteor 数据源能存活多久?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MiniMongo 集合通常会同步到 Mongo 集合,因此它的生命基本上是永远的.

A MiniMongo Collection usually gets synced to the Mongo Collection so its life is basically forever.

我想存储一个 Shopping Cart 对象.用户登录后,可以轻松存储和管理购物车数据的持续时间.但是当用户没有登录时呢?您不希望他们导航到网站的其他部分,甚至不希望刷新页面并彻底清除他们的购物车.但您也不想将购物车存放太久.将内容存储一天或几个小时就足够了.

I would like to store a Shopping Cart object. When a user is logged in, storing and managing how long that shopping cart data lasts for is easy. But what about when a user is not logged in? You don't want them to navigate to other parts of the website or even refresh the page and completely wipe out their shopping cart. But you also don't want to store the shopping cart for too long. Storing the contents for a day or a few hours should suffice.

一个 miniMongo 本地集合 ( new Mongo.Collection("null"); ) 持续多久?我可以将购物车内容存储在本地集合中还是会在页面刷新时擦除集合?

How long does a miniMongo Local Collection ( new Mongo.Collection("null"); ) last? Could I store the shopping cart contents in a Local Colection or does the collection get wiped on page refresh?

同样,Session 变量持续多长时间?

Likewise, how long does a Session variable last?

--- 答案摘要---

--- Summary of Answers ---

  • 会话变量 - 页面刷新后死亡,响应式
  • 本地集合 - 页面刷新后死亡,反应性
  • LocalStorage API - https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage,持续不同的时间段,从永远到选项卡关闭时,不是反应性的,不同的浏览器有不同的存储和从 LocalStorage 中检索,代码非常复杂.
  • LocalStorage 使用 Amplify - http://amplifyjs.com/ - 基本上是使用 LocalStorage API 的一种更简单的方法.
  • 将数据存储到带有时间戳的 Mongo 集合中.使用该时间戳运行一个 cronjob,它会在一段时间后定期删除数据 - 反应性,在所有计算机和浏览器中持续存在,让您完全控制数据到期的方式和时间,需要更多的编码和设置
  • Session Variable - dies after a page refresh, reactive
  • Local Collection - dies after a page refresh, reactive
  • LocalStorage API - https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage, persists for different periods of time ranging from forever to when a tab is closed, NOT reactive, different browsers have different ways of storing and retrieving from LocalStorage, so complex to code.
  • LocalStorage using Amplify - http://amplifyjs.com/ - basically an easier way to use the LocalStorage API.
  • Store data into a Mongo Collection with a timestamp. Use that timestamp to run a cronjob that periodically deletes the data after a set amount of time - reactive, persists across all computers and browsers, gives you complete control over how and when the data expires, takes a bit more coding and setup

推荐答案

会话变量和本地集合将在页面刷新后被删除,因此您可以从可能性列表中消除它们.

Session variables and local collections will be erased after a page refresh, so you can eliminate those from your list of possibilities.

另一种方法是将购物车数据存储在 localStorage 中.您可以通过直接调用 localStorage API 或使用像 amplify.js (amplify.jshttps://atmospherejs.com/meteor/amplify" rel="nofollow">meteor 包)如果您需要支持旧浏览器.这种方法的唯一问题是它不能跨浏览器/计算机工作,也不会在选项卡之间产生反应.

An alternative is to store the cart data in localStorage. You can do so with direct calls to the localStorage API, or with a polyfill like amplify.js (meteor package) if you need to support older browsers. The only problem with this approach is that it won't work across browsers/computers, and it won't be reactive between tabs.

我的建议是将购物车数据存储在一个维护 updatedAt 时间戳的集合中.然后添加一个 cron 作业,它会定期删除 updatedAt 较旧的所有购物车超过 X 小时.这具有跨选项卡/浏览器/计算机响应式工作的优势,并且您可以完全控制购物车何时到期(包括添加奇特的规则,例如如果用户碰巧连接,则购物车不过期).

My recommendation would be to store the cart data in a collection which maintains an updatedAt timestamp. Then add a cron job which periodically removes all carts where updatedAt is older than X hours. This has the advantage of working reactively across tabs/browsers/computers, and you'll have full control over when the carts expire (including adding fancy rules like not expiring the cart if the user happens to be connected).

这篇关于某些 Meteor 数据源能存活多久?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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