{"id":107,"date":"2009-10-14T16:33:15","date_gmt":"2009-10-14T16:33:15","guid":{"rendered":"http:\/\/shirishranjit.com\/blog1\/?p=107"},"modified":"2015-07-25T09:18:04","modified_gmt":"2015-07-25T13:18:04","slug":"google-app-engine-memcache-how-can-you-create-a-multiple-memcache","status":"publish","type":"page","link":"https:\/\/shirishranjit.com\/blog1\/entertainment\/google-app-engine-memcache-how-can-you-create-a-multiple-memcache","title":{"rendered":"Google App Engine: Memcache &#8211; How can you create a multiple Memcache?"},"content":{"rendered":"<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; text-align: justify; font: 12.0px Helvetica;\"><span style=\"letter-spacing: 0.0px;\">(http:\/\/code.google.com\/appengine\/docs\/java\/memcache\/overview.html)<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; text-align: justify; font: 12.0px Helvetica;\"><span style=\"letter-spacing: 0.0px;\">Google App Engine provides Memcache as a part of App Engine. Memcache implements the JCache interface (javax.cache). <\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; text-align: justify; font: 12.0px Helvetica;\"><span style=\"letter-spacing: 0.0px;\">The App Engine provides <\/span><span style=\"font: 10.0px Monaco; letter-spacing: 0.0px;\">MemcacheService<\/span><span style=\"letter-spacing: 0.0px;\"> and implementation of jCache API. There are a lot of useful features that the jCache does not provide. <\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; text-align: justify; font: 12.0px Helvetica;\"><span style=\"letter-spacing: 0.0px;\">Here is how one can create a multiple Memcache in App Engine.<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; text-align: justify; font: 10.0px Monaco;\"><span style=\"letter-spacing: 0.0px;\">MemcacheService <\/span><span style=\"letter-spacing: 0.0px color;\">cacheA<\/span><span style=\"letter-spacing: 0.0px;\"> = MemcacheServiceFactory.getMemcacheService();<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; text-align: justify; font: 10.0px Monaco;\"><span style=\"letter-spacing: 0.0px color;\">cacheLock<\/span><span style=\"letter-spacing: 0.0px;\">.setNamespace( <\/span><span style=\"letter-spacing: 0.0px color;\">&#8220;A&#8221;<\/span><span style=\"letter-spacing: 0.0px;\"> );<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; text-align: justify; font: 10.0px Monaco;\"><span style=\"letter-spacing: 0.0px;\">MemcacheService <\/span><span style=\"letter-spacing: 0.0px color;\">cacheB<\/span><span style=\"letter-spacing: 0.0px;\"> = MemcacheServiceFactory.getMemcacheService();<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; text-align: justify; font: 10.0px Monaco;\"><span style=\"letter-spacing: 0.0px color;\">cacheTxnLocked<\/span><span style=\"letter-spacing: 0.0px;\">.setNamespace( <\/span><span style=\"letter-spacing: 0.0px color;\">&#8220;B&#8221;<\/span><span style=\"letter-spacing: 0.0px;\"> );<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; text-align: justify; font: 10.0px Monaco;\"><span style=\"letter-spacing: 0.0px;\">MemcacheService <\/span><span style=\"letter-spacing: 0.0px color;\">cacheC<\/span><span style=\"letter-spacing: 0.0px;\"> = MemcacheServiceFactory.getMemcacheService();<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; text-align: justify; font: 10.0px Monaco;\"><span style=\"letter-spacing: 0.0px color;\">cacheAllTxns<\/span><span style=\"letter-spacing: 0.0px;\">.setNamespace( <\/span><span style=\"letter-spacing: 0.0px color;\">&#8220;C&#8221;<\/span><span style=\"letter-spacing: 0.0px;\"> );<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; text-align: justify; font: 11.0px Helvetica;\"><span style=\"letter-spacing: 0.0px;\">Once you create the Memcache using the <\/span><span style=\"font: 10.0px Monaco; letter-spacing: 0.0px color;\">MemcacheServiceFactory<\/span><span style=\"letter-spacing: 0.0px;\">, you need to set the Namespace. The \u201cNamespace\u201d causes the cache to be different. <\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; text-align: justify; font: 11.0px Helvetica;\"><span style=\"letter-spacing: 0.0px;\">So, if you do not set the \u201cNamespace\u201d on the cache, then all the cache that you get from the <\/span><span style=\"font: 10.0px Monaco; letter-spacing: 0.0px color;\">MemcacheService<\/span><span style=\"letter-spacing: 0.0px;\"> will be the same.<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; text-align: justify; font: 11.0px Helvetica;\"><span style=\"letter-spacing: 0.0px;\">A pattern to use Memcache in the App Engine (or in any application server) is to use a Singleton to hold the cache. Singleton allows to initialize the different caches and works as a Facade to the Memcache. <\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; text-align: justify; font: 11.0px Helvetica;\"><span style=\"letter-spacing: 0.0px;\">This design has many useful features. Few of those are:<\/span><\/p>\n<ul>\n<li style=\"margin: 0.0px 0.0px 0.0px 0.0px; text-align: justify; font: 11.0px Helvetica;\"><span style=\"letter-spacing: 0.0px;\">hides the actual implementation of Memcache which allows to change cache without impacting the user of the cache.<\/span><\/li>\n<li style=\"margin: 0.0px 0.0px 0.0px 0.0px; text-align: justify; font: 11.0px Helvetica;\"><span style=\"letter-spacing: 0.0px;\">allows data access objects to be cached such that the cached persistence objects are managed by same persistence context. <\/span><\/li>\n<\/ul>\n<div class=\"twttr_buttons\"><div class=\"twttr_twitter\">\n\t\t\t\t\t<a href=\"http:\/\/twitter.com\/share?text=Google+App+Engine%3A+Memcache+-+How+can+you+create+a+multiple+Memcache%3F\" class=\"twitter-share-button\" data-via=\"\" data-hashtags=\"\"  data-size=\"default\" data-url=\"https:\/\/shirishranjit.com\/blog1\/entertainment\/google-app-engine-memcache-how-can-you-create-a-multiple-memcache\"  data-related=\"\" target=\"_blank\">Tweet<\/a>\n\t\t\t\t<\/div><div class=\"twttr_followme\">\n\t\t\t\t\t\t<a href=\"https:\/\/twitter.com\/shiranjit\" class=\"twitter-follow-button\" data-size=\"default\"  data-show-screen-name=\"false\"  target=\"_blank\">Follow me<\/a>\n\t\t\t\t\t<\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>(http:\/\/code.google.com\/appengine\/docs\/java\/memcache\/overview.html) Google App Engine provides Memcache as a part of App Engine. Memcache implements the JCache interface (javax.cache). The App Engine provides MemcacheService and implementation of jCache API. There are a lot of useful features that the jCache does not &hellip; <a href=\"https:\/\/shirishranjit.com\/blog1\/entertainment\/google-app-engine-memcache-how-can-you-create-a-multiple-memcache\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":51,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-107","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/pages\/107"}],"collection":[{"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/comments?post=107"}],"version-history":[{"count":8,"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/pages\/107\/revisions"}],"predecessor-version":[{"id":1007,"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/pages\/107\/revisions\/1007"}],"up":[{"embeddable":true,"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/pages\/51"}],"wp:attachment":[{"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/media?parent=107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}