在开发管理控制台中,当我查看我的数据时,它显示“
命名空间是什么,我应该如何使用它们?
选择不同的命名空间
”。命名空间是什么,我应该如何使用它们?
选择不同的命名空间
”。命名空间用于在Google应用引擎中创建多租户应用程序。在多租户应用程序中,应用程序的单个实例在服务器上运行,为多个客户组织(租户)提供服务。通过这种方式,可以设计应用程序以虚拟分区其数据和配置(业务逻辑),并且每个客户组织都使用定制的虚拟应用程序实例。您可以通过为每个租户指定唯一的命名空间字符串来轻松地将数据分区跨租户。
命名空间的其他用途:
有关更多信息,请访问以下链接:
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/
看向这个问题,没有得到太多好的评价和答案,所以尝试回答一下。
当使用命名空间时,我们可以在给定的命名空间中实现键和值的最佳实践。以下是完整提供命名空间信息的最佳示例。
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>')