流星会话替换? [英] Meteor Session Replacement?

查看:38
本文介绍了流星会话替换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在最新的 Meteor 版本(0.5.8 版)中,Session 已从服务器端代码中删除.

In the latest Meteor release (version 0.5.8), Session has been removed from the server-side code.

以前我使用 Session 为服务器存储 客户端特定 变量;此功能的替代品是什么?

Previously I've used Session to store client-specific variables for the server; what is the replacement for this functionality?

示例:用户一打开浏览器,用户二打开浏览器.一个调用服务器上的方法设置一些令牌,另一个调用服务器上的方法做同样的事情.然后,当客户端请求某些内容时,我需要访问它.如何区分两者?

Example case: User One opens a browser, User Two opens a browser. One calls a method on the server setting some token, the other calls a method on the server doing the same. I then need to access this when the client requests something. How do I differentiate between the two?

推荐答案

您需要将令牌保存到数据库中的集合中.

You'll want to save your tokens to a collection in the database.

如果您想简单地将 session 包复制到应用程序的 packages 目录并更改它的目录,则可以在服务器上使用 Sessionpackage.js 也加载到服务器上.但是 Session 是内存中的数据结构,因此如果您有多个服务器实例,它将无法工作;并且您将无法在不丢失用户令牌的情况下重新启动服务器.

You could use a Session on the server if you wanted to simply by copying the session package into your application's packages directory and changing its package.js to also load on the server. But a Session is an in-memory data structure, and so won't work if you have multiple server instances; and you wouldn't be able to restart the server without losing your user's tokens.

如果您将令牌存储在数据库中,它们将在服务器重启后持续存在,并且将与未来版本的 Meteor 一起使用,该版本 能够通过在需要时添加更多服务器实例来扩展应用程序.

If you store your tokens in the database they'll persist across server restarts, and will work with a future version of Meteor which is able to scale an application by adding more server instances when needed.

如果您需要使您的令牌过期(以便您的集合不会无限增长),您可以在您的令牌集合中添加一个lastUsed"日期字段,并定期删除未使用时间超过您选择的有效期.

If you need to expire your tokens (so that your collection doesn't grow without bound), you could add a "lastUsed" Date field to your token collection, and periodically remove tokens that haven't been used for longer than your chosen expiration period.

这篇关于流星会话替换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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