Click here to Skip to main content
15,899,314 members

Chace Daggers - Professional Profile



Summary

    Blog RSS
3
Debator
4
Organiser
303
Participant
0
Author
0
Authority
0
Editor
0
Enquirer
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Reputation

Weekly Data. Recent events may not appear immediately. For information on Reputation please see the FAQ.

Privileges

Members need to achieve at least one of the given member levels in the given reputation categories in order to perform a given action. For example, to store personal files in your account area you will need to achieve Platinum level in either the Author or Authority category. The "If Owner" column means that owners of an item automatically have the privilege. The member types column lists member types who gain the privilege regardless of their reputation level.

ActionAuthorAuthorityDebatorEditorEnquirerOrganiserParticipantIf OwnerMember Types
Have no restrictions on voting frequencysilversilversilversilver
Bypass spam checks when posting contentsilversilversilversilversilversilvergoldSubEditor, Mentor, Protector, Editor
Store personal files in your account areaplatinumplatinumSubEditor, Editor
Have live hyperlinks in your profilebronzebronzebronzebronzebronzebronzesilverSubEditor, Protector, Editor
Have the ability to include a biography in your profilebronzebronzebronzebronzebronzebronzesilverSubEditor, Protector, Editor
Edit a Question in Q&AsilversilversilversilverYesSubEditor, Protector, Editor
Edit an Answer in Q&AsilversilversilversilverYesSubEditor, Protector, Editor
Delete a Question in Q&AYesSubEditor, Protector, Editor
Delete an Answer in Q&AYesSubEditor, Protector, Editor
Report an ArticlesilversilversilversilverSubEditor, Mentor, Protector, Editor
Approve/Disapprove a pending ArticlegoldgoldgoldgoldSubEditor, Mentor, Protector, Editor
Edit other members' articlesSubEditor, Protector, Editor
Create an article without requiring moderationplatinumSubEditor, Mentor, Protector, Editor
Approve/Disapprove a pending QuestionProtector
Approve/Disapprove a pending AnswerProtector
Report a forum messagesilversilverbronzeProtector, Editor
Approve/Disapprove a pending Forum MessageProtector
Have the ability to send direct emails to members in the forumsProtector
Create a new tagsilversilversilversilver
Modify a tagsilversilversilversilver

Actions with a green tick can be performed by this member.


 
NewsThe Memcache Pattern Pin
Chace Daggers28-Oct-08 6:53
Chace Daggers28-Oct-08 6:53 
Cry | :((
Memcache is typically used with the following pattern:

The application receives a query from the user or the application.
The application checks whether the data needed to satisfy that query is in memcache.
If the data is in memcache, the application uses that data.
If the data is not in memcache, the application queries the datastore and stores the results in memcache for future requests.
The pseudocode below represents a typical memcache request:

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 dataModifying guestbook.py to use Memcache
The guestbook application in the Getting Started Guide queries the datastore on every request. You can modify the Guestbook application to use memcache before resorting to querying the datastore.

First we'll import the memcache module and create the method that checks memcache before running a query.

from google.appengine.api import memcachedef get_greetings(self): """get_greetings() Checks the cache to see if there are cached greetings. If not, call render_greetings and set the cache Returns: A string of HTML containing greetings. """ 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 greetingsNext we'll separate out the querying and creation of the HTML for the page. When we don't hit the cache, we'll call this method to query the datastore and build the HTML string that we'll store in memcache.

def render_greetings(self): """render_greetings() Queries the database for greetings, iterate through the results and create the HTML. Returns: A string of HTML containing greetings """ 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()Finally we will update the MainPage handler to call the get_greetings() method and display some stats about the number of times the cache was hit or missed.

import cgiimport datetimeimport loggingimport StringIOfrom google.appengine.ext import dbfrom google.appengine.api import usersfrom google.appengine.ext import webappfrom google.appengine.ext.webapp.util import run_wsgi_appfrom google.appengine.api import memcachelogging.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() Checks the cache to see if there are cached greetings. If not, call render_greetings and set the cache Returns: A string of HTML containing greetings. """ 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() Queries the database for greetings, iterate through the results and create the HTML. Returns: A string of HTML containing greetings """ 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(): run_wsgi_app(application)if __name__ == '__main__': main()

Chace [‡] Daggers

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.