GAE's Memcache
if value: #Data still cached … else: #Data without cached …
class MainHandler(webapp2.RequestHandler): def get(self): memcache.set(“name”, “LevelUp”)
value = memcache.get("name")
self.response.write(value + "<br/>")
memcache.set_multi({"name": "LevelUp", "url": "httlp://www.dotblogs.com.tw/larrynung"})
values = memcache.get_multi(["name", "url"])
self.response.write(values["name"] + "<br/>")
self.response.write(values["url"] + "<br/>")
application = webapp2.WSGIApplication([ (’/’, MainHandler) ])
class Article(db.Model): id = db.StringProperty(required=True) name = db.StringProperty() content = db.TextProperty()
class Blog(db.Model): id = db.StringProperty(required=True) name = db.StringProperty(required=True) url = db.LinkProperty(required=True) articleIDs = db.StringListProperty()
class MainHandler(webapp2.RequestHandler): def get(self): self.response.write("<html><body>")
blogs = db.GqlQuery("select * from Blog Limit 1")
if not blogs or blogs.count() == 0:
blog = Blog(id=str(uuid.uuid1()), name="LevelUp", url="http://www.dotblogs.com.tw/larrynung")
articleID = str(uuid.uuid1())
Article(id=articleID, name="hello", content="hello world").put()
blog.articleIDs = [articleID]
blog.put()
self.response.write("Blog data saved...<br/><br/>")
self.redirect("/")
return
blog = memcache.get("blog")
if blog:
self.response.write("Cached blog data...<br/><br/>")
else:
self.response.write("Blog data loading...<br/><br/>")
blog = Blog.gql("Limit 1")[0]
self.response.write("Save blog data to memcache...<br/><br/>")
memcache.set("blog", blog, 5)
self.response.write(blog.name + "<br/>")
self.response.write(blog.url + "<br/>")
for articleID in blog.articleIDs:
article = Article.gql("where id = :1", articleID)[0]
self.response.write(article.name + "</br>")
self.response.write(article.content + "</br>")
self.response.write("<br/>")
self.response.write("</body></html>")
application = webapp2.WSGIApplication([ (’/’, MainHandler) ])