NamespaceManager.#set("a-namespace");
MemcacheService memcache = MemcacheServiceFactory.getMemcacheService();
// Store record in namespace "a-namespace"
memcache.put("key1", "value1");
NamespaceManager.#set("other-namespace");
// Store record in namespace "other-namespace"
memcache.put("key2", "value2");
MemcacheService boundMemcache =
MemcacheServiceFactory.getMemcacheService("specific-namespace");
NamespaceManager.#set("whatever-namespace");
// The record is still stored in namespace "specific-namespace".
boundMemcache.put("key3", "value3");
MemcacheService memcache (in the above example) uses the current
namespace and key1 will be stored in namespace "a-namespace",
while key2 is stored in namespace "other-namespace". It is
possible to override the current namespace and store data in specific
namespace. In the above example key3 is stored in namespace
"specific-namespace".
The Task Queue com.google.appengine.api.taskqueue.Queue#add
methods will forward the NamespaceManager settings into the task
being added causing the added task to be executed with the same current
namespace as the task creator. The exception is that an unset current
namespace (i.e. NamespaceManager#get() returns null) will be
forwarded as an empty ("") namespace to the created task's requests.
See Also: Multitenancy and the Namespaces Java API. In Google App Engine Developer's Guide.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[[["\u003cp\u003eNamespaceManager manages the current namespace used by App Engine APIs, including Datastore, Memcache, and Task Queue.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eget()\u003c/code\u003e method returns the current namespace, and if it returns null, APIs default to using an empty string ("") namespace.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eset()\u003c/code\u003e method allows you to change the namespace, which will affect all subsequent operations that rely on the current namespace, unless the namespace is specifically defined, as seen in the memcacheService example with \u003ccode\u003ekey3\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eTask Queue's \u003ccode\u003eQueue#add\u003c/code\u003e method propagates the current NamespaceManager settings to the added task, ensuring it executes with the same namespace, with an unset namespace being forwarded as an empty namespace.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003egetGoogleAppsNamespace()\u003c/code\u003e method retrieves the Google Apps domain associated with the current request, returning an empty string if no domain is found.\u003c/p\u003e\n"]]],[],null,["# Class NamespaceManager (2.0.0)\n\n public final class NamespaceManager\n\nProvides functions for manipulating the current namespace used for\nApp Engine APIs.\n\nThe \"current namespace\" is the string that is returned by\n[#get()](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.NamespaceManager#com_google_appengine_api_NamespaceManager_get__) and used by a number of APIs including Datatore,\nMemcache and Task Queue.\n\nWhen a namespace aware class (e.g.,\n[com.google.appengine.api.datastore.Key](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Key),\n[com.google.appengine.api.datastore.Query](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Query) and\n[com.google.appengine.api.memcache.MemcacheService](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.memcache.MemcacheService)) is constructed, it\ndetermines which namespace will be used by calling\n[NamespaceManager#get()](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.NamespaceManager#com_google_appengine_api_NamespaceManager_get__) if it is otherwise unspecified. If\n[NamespaceManager#get()](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.NamespaceManager#com_google_appengine_api_NamespaceManager_get__) returns null, the current namespace is unset\nand these APIs will use the empty (\"\") namespace in its place.\n\nExample: \n\n```\n NamespaceManager.#set(\"a-namespace\");\n MemcacheService memcache = MemcacheServiceFactory.getMemcacheService();\n // Store record in namespace \"a-namespace\"\n memcache.put(\"key1\", \"value1\");\n /appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.NamespaceManager./appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.NamespaceManager#com_google_appengine_api_NamespaceManager_set_(\"other-namespace\");\n // Store record in namespace \"other-namespace\"\n memcache.put(\"key2\", \"value2\");\n MemcacheService boundMemcache =\n MemcacheServiceFactory.getMemcacheService(\"specific-namespace\");\n /appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.NamespaceManager./appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.NamespaceManager#com_google_appengine_api_NamespaceManager_set_(\"whatever-namespace\");\n // The record is still stored in namespace \"specific-namespace\".\n boundMemcache.put(\"key3\", \"value3\");\n \n```\n\n\u003cbr /\u003e\n\nMemcacheService `memcache` (in the above example) uses the current\nnamespace and `key1` will be stored in namespace `\"a-namespace\"`,\nwhile `key2` is stored in namespace `\"other-namespace\"`. It is\npossible to override the current namespace and store data in specific\nnamespace. In the above example `key3` is stored in namespace\n`\"specific-namespace\"`.\n\nThe Task Queue com.google.appengine.api.taskqueue.Queue#add\nmethods will forward the [NamespaceManager](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.NamespaceManager) settings into the task\nbeing added causing the added task to be executed with the same current\nnamespace as the task creator. The exception is that an unset current\nnamespace (i.e. [NamespaceManager#get()](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.NamespaceManager#com_google_appengine_api_NamespaceManager_get__) returns null) will be\nforwarded as an empty (\"\") namespace to the created task's requests.\nSee Also: [Multitenancy and the Namespaces Java API. In *Google App Engine Developer's Guide*](http://cloud.google.com/appengine/docs/java/multitenancy/). \n\nInheritance\n-----------\n\n[java.lang.Object](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html) \\\u003e NamespaceManager \n\nInherited Members\n-----------------\n\n[Object.clone()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#clone--) \n[Object.equals(Object)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-) \n[Object.finalize()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#finalize--) \n[Object.getClass()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#getClass--) \n[Object.hashCode()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--) \n[Object.notify()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notify--) \n[Object.notifyAll()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notifyAll--) \n[Object.toString()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--) \n[Object.wait()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait--) \n[Object.wait(long)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-) \n[Object.wait(long,int)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-int-)\n\nStatic Methods\n--------------\n\n### get()\n\n public static String get()\n\nReturns the current namespace setting or either `null` or \"\" (empty) if not set.\n\nIf the current namespace is unset, callers should assume\nthe use of the \"\" (empty) namespace in all namespace-aware services.\n\n### getGoogleAppsNamespace()\n\n public static String getGoogleAppsNamespace()\n\nReturns the Google Apps domain referring this request or\notherwise the empty string (\"\").\n\n### set(String newNamespace)\n\n public static void set(String newNamespace)\n\nSet the value used to initialize the namespace of namespace-aware services.\n\n### validateNamespace(String namespace)\n\n public static void validateNamespace(String namespace)\n\nValidate the format of a namespace string."]]