[英]“Sessions” with Google Cloud Endpoints
This question is only to confirm that I'm clear about this concept. 这个问题只是为了证实我对这个概念很清楚。
As far as I understand, Google Cloud Endpoints are kind of Google's implementation of REST services, so that they can't keep any "session" data in memory, therefore: 据我所知, Google Cloud Endpoints是Google实施的REST服务,因此无法将任何“会话”数据保留在内存中,因此:
Is this correct? 它是否正确? And if so, is this actually good in terms of performance?
如果是这样,这在性能方面是否真的很好?
Yes you can use session, only put another Paramether in your API method with HttpServlet: 是的,你可以使用session,只在你的API方法中使用HttpServlet放入另一个Paramether:
@ApiMethod
public MyResponse getResponse( HttpServletRequest req, @Named("infoId") String infoId ) {
// Use 'req' as you would in a servlet, e.g.
String ipAddress = req.getRemoteAddr();
...
}
The datastore is pretty quick especially if you do a key lookup (as apposed to query). 数据存储区非常快,特别是如果您进行密钥查找(与查询相关)。 if you use NDB then you will have the benefit of auto memache your lookups.
如果您使用NDB,那么您将获得自动memache查找的好处。
Yes, your Cloud Endpoints API backend code (Java or Python) is still running on App Engine, so you have the same access to all resources you would have on App Engine. 是的,您的Cloud Endpoints API后端代码(Java或Python)仍在App Engine上运行,因此您可以访问App Engine上的所有资源。
Though you can't set client-side cookies for sessions, you still can obtain a user for a request and store user-specific data in the datastore. 虽然您无法为会话设置客户端cookie,但您仍然可以获取请求的用户并在数据存储中存储特定于用户的数据。 As @Shay Erlichmen mentioned, if you couple the datastore with memcache and an in-context cache (as ndb does), you can make these lookups very quick.
正如@Shay Erlichmen所提到的,如果你将数据存储区与memcache和一个上下文缓存(如ndb那样)相结合,你可以非常快速地进行这些查找。
To do this in either Python or Java, either allowed_client_ids
or audiences
will need to be specified in the annotation/decorator on the API and/or on the method(s). 要在Python或Java中执行此操作,需要在API上的注释/装饰器和/或方法中指定
allowed_client_ids
或者audiences
。 See the docs for more info. 有关详细信息,请参阅文档 。
If you want to get a user in Python, call 如果你想用Python获取用户,请致电
endpoints.get_current_user()
from within a request that has been annotated with allowed_client_ids
or audiences
. 来自已使用
allowed_client_ids
或audiences
注释的请求中。 If this returns None
, then there is no valid user (and you should return a 401). 如果返回
None
,则没有有效用户(并且应该返回401)。
To get a user, on an annotated method (or method contained in an annotated API), simply specify a user object in the request: 要获取用户,请在带注释的方法(或带注释的API中包含的方法)上,在请求中指定用户对象:
import com.google.appengine.api.users.User;
...
public Model insert(Model model, User user) throws
OAuthRequestException, IOException {
and as in Python, check if user
is null
to determine if a valid OAuth 2.0 token was sent with the request. 和在Python中一样,检查
user
是否为null
以确定是否随请求一起发送了有效的OAuth 2.0令牌。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.