Memcache API
http://code.google.com/appengine/docs/memcache/
ã¯ããã«
Memcacheãµã¼ãã¹ã¯åã®ã¢ããªã±ã¼ã·ã§ã³ã«è¤æ°ã®ã¢ããªã±ã¼ã·ã§ã³ããã¢ã¯ã»ã¹å¯è½ã§ãã¤ããã©ã¼ãã³ã¹ãªãªã³ã¡ã¢ãªkey-valueãã£ãã·ã¥ãæä¾ãã¦ããããMemcacheã¯ä¸æãã¼ã¿ã®ããã«ãã¼ã¿ã¹ãã¢ã®ãããªæ°¸ç¶æ§ããã©ã³ã¶ã¯ã·ã§ã³ãä¸è¦ãªãã¼ã¿ããé«éã¢ã¯ã»ã¹ã®ããã«ãã¼ã¿ã¹ãã¢ããã³ãã¼ãã¦ãããããªãã¼ã¿ã«åãã¦ãããMemcache APIã¯Danga Interactiveã®memcachedã¨åæ§ã®æ©è½ãåããäºææ§ãä¿ã£ã¦ããã
Memcache APIãå©ç¨ããã¨æ¬¡ã®çç±ããã¢ããªã±ã¼ã·ã§ã³ã®ããã©ã¼ãã³ã¹ããããããã¼ã¿ã¹ãã¢ã®è² è·ã軽æ¸ããã:
- ãã¼ã¿ã¹ãã¢ã¸ã®ã¯ã¨ãªãåçã«æ¸å°ãã
- ã¨ã¦ã人æ°ã®ãããã¼ã¸ã§ããã¼ã¿ã¹ãã¢å²ãå½ã¦ã®ä½¿ç¨éãåæ¸ãã
- é«è² è·ãªã¯ã¨ãªãæä½ã®çµæããã£ãã·ã¥ãã
- ä¸æçãªã«ã¦ã³ã¿ã®ä½¿ç¨ãå¯è½ã«ãã
Memcache APIã使ãã¨ã¢ããªã±ã¼ã·ã§ã³ã§ä¸è²«æ§ã®ãããã£ãã·ã¥ãä½æã§ããããã£ãã·ã¥ã¯ã¢ããªã±ã¼ã·ã§ã³ã®ãã¹ã¦ã®ã¤ã³ã¹ã¿ã³ã¹ããå©ç¨ã§ããã¡ã¢ãªã®å§åã«ãã£ã¦(ã¤ã¾ãããã£ãã·ã¥ã«ããããã®ãã¼ã¿ãæºã¾ããããã¨ã)ããããã¯éçºè ã«ãã£ã¦è¨å®ãããããªã·ã¼ãããã°ããã«å¾ã£ã¦ãã®ãã¼ã¿ã¯ç ´æ£ãããããã£ãã·ã¥ã®ããªã·ã¼ã¯ãã£ãã·ã¥å ã«ä¿æãããããããã®key-valueãã¢ã«å¯¾ãã¦è¨å®ã§ãããã¾ãããã£ãã·ã¥ããã¹ã¦ç ´æ£ãããããã¼ã¿ã®ä¸é¨ã«æå¹æéãè¨å®ãããã¨ãå¯è½ã ã
from google.appengine.api import memcache # ãã£ãã·ã¥ã«åå¨ããªããã°å¤ã追å ã1æéã§ãã£ãã·ã¥ã¯ç ´æ£ããã memcache.add(key="weather_USA_98105", value="raining", time=3600) # ããã¤ãã®å¤ãè¨å®ããããã®ãã¼ã«å¯¾å¿ããå¤ããã§ã«åå¨ããã¨ãã¯ä¸æ¸ãããã memcache.set_multi({ "USA_98105": "raining", "USA_94105": "foggy", "USA_94043": "sunny" }, key_prefix="weather_", time=3600) # æ´æ°å¤ãã¢ãããã¯ã«ã¤ã³ã¯ãªã¡ã³ããã memcache.set(key="counter", 0) memcache.incr("counter") memcache.incr("counter") memcache.incr("counter")
Memcacheã使ã
Memcacheã¯ãã¤ããã©ã¼ãã³ã¹ãªåæ£ã¡ã¢ãªãªãã¸ã§ã¯ããã£ãã·ã¥ã·ã¹ãã ã§ããã¼ã¿ã¹ãã¢ã®è² è·ã軽æ¸ãã¦åçãªã¦ã§ãã¢ããªã±ã¼ã·ã§ã³ãé«éåãããã¨ãç®çã¨ãã¦ããããã¼ã¿ã¹ãã¢ã¸ã®ã¯ã¨ãªã®çµæããã¦ã§ããµã¤ãã®ä¸é¨ã§ã¬ã³ããªã³ã°ãããHTMLããã£ãã·ã¥ããã®ãMemcacheã®å ¸åçãªä½¿ç¨ä¾ã«ãªãã
Memcacheãã¿ã¼ã³
Memcacheã®ãã¿ã¼ã³ã¯ãããã¦ã·ã³ãã«ã :
- ã¦ã¼ã¶ã¾ãã¯ã¢ããªã±ã¼ã·ã§ã³ããã¯ã¨ãªãæãåºã
- ã¯ã¨ãªãæºããã®ã«å¿
è¦ãªãã¼ã¿ãmemcacheå
ã«ãããã©ãã確èªãã
- ãã¼ã¿ãmemcacheã«ããã°ããããè¿ã
- ãã¼ã¿ãmemcacheã«ãªããã°ãã¯ã¨ãªããã¼ã¿ã¹ãã¢ã«æãã¦ãã®çµæããã£ãã·ã¥ã«è¨å®ãã
以ä¸ã®æ¬ä¼¼ã³ã¼ãã¯å ¸åçãªmemcacheãªã¯ã¨ã¹ããç解ãã¦ãããããã®ãã®ã :
def get_data(): data = memcache.get("key") if data is not None: return data else: data = self.query_for_data() memcache.add("key", data, 60) return data
guestbook.pyãæ¹è¯ãã¦Memcacheã使ãããã«ãã
Getting Started Guideã«ããã²ã¹ãããã¯ã¢ããªã±ã¼ã·ã§ã³ã¯æ¯ãªã¯ã¨ã¹ããã¼ã¿ã¹ãã¢ã«ã¯ã¨ãªãæãã¦ããããã®ã²ã¹ãããã¯ã¢ããªã±ã¼ã·ã§ã³ãæ¹è¯ãã¦ããã¼ã¿ã¹ãã¢ã®ã¯ã¨ãªã«é ¼ãåã«memcacheã使ãããã«ãã¦ã¿ããã
ã¾ãã¯ããã«ãmemcacheã¢ã¸ã¥ã¼ã«ãã¤ã³ãã¼ããã¦ãã¯ã¨ãªå®è¡åã«memcacheã確èªããã¡ã½ãããä½æããã
from google.appengine.api import memcache def get_greetings(self): """get_greetings() greetingsããã£ãã·ã¥ããã¦ãããã©ãã確èªããã ãããªããã°render_greetingsãå¼ãã§ããã£ãã·ã¥ãè¨å®ããã æ»ãå¤: greetingsãå«ãHTMLæåå """ greetings = memcache.get("greetings") if greetings is not None: return greetings else: greetings = self.render_greetings() if not memcache.add("greetings", greetings, 10): logging.error("Memcache set failed.") return greetings
次ã«ãã¼ã¸ã®ããã®ã¯ã¨ãªã®çºè¡ã¨HTMLã®çæãåé¢ããããã£ãã·ã¥ããããããªãã¨ãã¯ããã®ã¡ã½ãããå¼ãã§ãã¼ã¿ã¹ãã¢ã«ã¯ã¨ãªãçºè¡ããHTMLæååãä½æããå¾ããããmemcacheã«ä¿åããã
def render_greetings(self): """render_greetings() ãã¼ã¿ã¹ãã¢ã«greetingsãåãåãããçµæãã¤ãã¬ã¼ãã㦠HTMLãä½æãã æ»ãå¤: greetingsãå«ãHTMLæåå """ results = db.GqlQuery("SELECT * " "FROM Greeting " "ORDER BY date DESC").fetch(10) output = StringIO.StringIO() for result in results: if result.author: output.write("<b>%s</b> wrote:" % result.author.nickname()) else: output.write("An anonymous person wrote:") output.write("<blockquote>%s</blockquote>" % cgi.escape(result.content)) return output.getvalue()
æå¾ã«MainPageãã³ãã©ãå¤æ´ãã¦get_greetings()ã¡ã½ãããå¼ã¶ããã«ãããã£ãã·ã¥ãä½åº¦ããããããã¯ãã¹ãããã¨ããç¶æ³ã表示ããã
import cgi import datetime import wsgiref.handlers import logging import StringIO from google.appengine.ext import db from google.appengine.api import users from google.appengine.ext import webapp from google.appengine.api import memcache logging.getLogger().setLevel(logging.DEBUG) class Greeting(db.Model): author = db.UserProperty() content = db.StringProperty(multiline=True) date = db.DateTimeProperty(auto_now_add=True) class MainPage(webapp.RequestHandler): def get(self): self.response.out.write("<html><body>") greetings = self.get_greetings() stats = memcache.get_stats() self.response.out.write("<b>Cache Hits:%s</b><br>" % stats['hits']) self.response.out.write("<b>Cache Misses:%s</b><br><br>" % stats['misses']) self.response.out.write(greetings) self.response.out.write(""" <form action="/sign" method="post"> <div><textarea name="content" rows="3" cols="60"></textarea></div> <div><input type="submit" value="Sign Guestbook"></div> </form> </body> </html>""") def get_greetings(self): """ get_greetings() greetingsããã£ãã·ã¥ããã¦ãããã©ãã確èªããã ãããªããã°render_greetingsãå¼ãã§ããã£ãã·ã¥ãè¨å®ããã æ»ãå¤: greetingsãå«ãHTMLæåå """ greetings = memcache.get("greetings") if greetings is not None: return greetings else: greetings = self.render_greetings() if not memcache.add("greetings", greetings, 10): logging.error("Memcache set failed.") return greetings def render_greetings(self): """ render_greetings() ãã¼ã¿ã¹ãã¢ã«greetingsãåãåãããçµæãã¤ãã¬ã¼ãã㦠HTMLãä½æãã æ»ãå¤: greetingsãå«ãHTMLæåå """ results = db.GqlQuery("SELECT * " "FROM Greeting " "ORDER BY date DESC").fetch(10) output = StringIO.StringIO() for result in results: if result.author: output.write("<b>%s</b> wrote:" % result.author.nickname()) else: output.write("An anonymous person wrote:") output.write("<blockquote>%s</blockquote>" % cgi.escape(result.content)) return output.getvalue() class Guestbook(webapp.RequestHandler): def post(self): greeting = Greeting() if users.get_current_user(): greeting.author = users.get_current_user() greeting.content = self.request.get('content') greeting.put() self.redirect('/') application = webapp.WSGIApplication([ ('/', MainPage), ('/sign', Guestbook) ], debug=True) def main(): wsgiref.handlers.CGIHandler().run(application) if __name__ == '__main__': main()