简体   繁体   English

App Engine服务的适当范围

[英]Proper scope for App Engine services

What is the proper scope for App Engine services when creating a servlet: static, instance, or local? 创建servlet时App Engine服务的适当范围是:静态,实例还是本地? And what are the implications of each ? 每个人的含义是什么? It seems like you should want to use them in as wide a scope as possible, to avoid the overhead of re-creating (or re-retrieving) them, but I wonder as to whether this will cause improper reuse of data, especially if <threadsafe>true</threadsafe> . 看起来您应该在尽可能宽的范围内使用它们,以避免重新创建(或重新检索)它们的开销,但我想知道这是否会导致数据的不正确重用,尤其是<threadsafe>true</threadsafe>


Examples of each scope are provided below. 以下提供每个范围的示例。 MemcacheService will be used in the examples below, but my question applies to any and all services (though I'm not sure if the answer depends on the service being used). MemcacheService将在下面的示例中使用,但我的问题适用于任何和所有服务(虽然我不确定答案是否取决于所使用的服务)。 I commonly use MemcacheService , DatastoreService , PersistenceManager , ChannelService , and UserService . 我通常使用MemcacheServiceDatastoreServicePersistenceManagerChannelServiceUserService

Static scope: 静态范围:

public class MyServlet extends HttpServlet {
    private static MemcacheService memcacheService = MemcacheServiceFactory.getMemcacheService();

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) {
        memcacheService.get("x");
    }
}

Instance member: 实例成员:

public class MyServlet extends HttpServlet {
    private MemcacheService memcacheService = MemcacheServiceFactory.getMemcacheService();

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) {
        memcacheService.get("x");
    }
}

Local scope: 当地范围:

public class MyServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) {
        MemcacheService memcacheService = MemcacheServiceFactory.getMemcacheService();
        memcacheService.get("x");
    }
}

GAE is a distributed system where all of it's services run on separate servers. GAE是一个分布式系统,其所有服务都在不同的服务器上运行。 So when you invoke a service it internally serializes the request (afaik with protocol buffers) sends it to the server running the service, retrieves the result and deserializes it. 因此,当您调用服务时,它会在内部序列化请求(afaik with protocol buffers)将其发送到运行服务的服务器,检索结果并对其进行反序列化。

So all of *Service classes are basically pretty thin wrappers around serialization/deserialization code. 因此,所有*Service类基本上都是围绕序列化/反序列化代码的非常薄的包装器。 See for example source of MemcacheService . 请参阅例如MemcacheService的源代码

About scope: there is no need to optimize on *Service classes as they are pretty thin wrappers and creating them should take negligible time compared to whole service roundtrip. 关于范围:没有必要优化*Service类,因为它们是非常薄的包装器,与整个服务往返相比,创建它们应该花费的时间可以忽略不计。

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

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