App Engine数据存储中的命名空间有什么用?

18
在开发管理控制台中,当我查看我的数据时,它显示“选择不同的命名空间”。
命名空间是什么,我应该如何使用它们?
3个回答

13

命名空间允许您在多租户应用程序中实现数据隔离。官方文档链接到一些示例项目,以便您了解如何使用它。


1

命名空间用于在Google应用引擎中创建多租户应用程序。在多租户应用程序中,应用程序的单个实例在服务器上运行,为多个客户组织(租户)提供服务。通过这种方式,可以设计应用程序以虚拟分区其数据和配置(业务逻辑),并且每个客户组织都使用定制的虚拟应用程序实例。您可以通过为每个租户指定唯一的命名空间字符串来轻松地将数据分区跨租户。

命名空间的其他用途:

  1. 隔离用户信息
  2. 将管理员数据与应用程序数据分开
  3. 为测试和生产创建单独的数据存储实例
  4. 在单个应用引擎实例上运行多个应用程序

有关更多信息,请访问以下链接:

http://www.javacodegeeks.com/2011/12/multitenancy-in-google-appengine-gae.html
https://developers.google.com/appengine/docs/java/multitenancy/
http://java.dzone.com/articles/multitenancy-google-appengine

http://www.sitepoint.com/multitenancy-and-google-app-engine-gae-java/

-1

看向这个问题,没有得到太多好的评价和答案,所以尝试回答一下。

当使用命名空间时,我们可以在给定的命名空间中实现键和值的最佳实践。以下是完整提供命名空间信息的最佳示例。

from google.appengine.api import namespace_manager
from google.appengine.ext import db
from google.appengine.ext import webapp

class Counter(db.Model):
   """Model for containing a count."""
   count = db.IntegerProperty()


def update_counter(name):
   """Increment the named counter by 1."""
def _update_counter(name):
   counter = Counter.get_by_key_name(name)
   if counter is None:
       counter = Counter(key_name=name);
       counter.count = 1
   else:
       counter.count = counter.count + 1
   counter.put()
# Update counter in a transaction.
db.run_in_transaction(_update_counter, name)

class SomeRequest(webapp.RequestHandler):
 """Perform synchronous requests to update counter."""
 def get(self):
    update_counter('SomeRequest')
    # try/finally pattern to temporarily set the namespace.
    # Save the current namespace.
    namespace = namespace_manager.get_namespace()
    try:
        namespace_manager.set_namespace('-global-')
        update_counter('SomeRequest')
    finally:
        # Restore the saved namespace.
        namespace_manager.set_namespace(namespace)
    self.response.out.write('<html><body><p>Updated counters')
    self.response.out.write('</p></body></html>')

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接