Google App Engine: Memcaching for dummies example

Here’s a very simple code written in Python 2.7 and webapp2 on Google App Engine that does memcaching. It is possibly the easiest example I could think of, that I could implement memcaching on Google App Engine.

The main confusion which arises when one starts to look into memcaching is that Google fails to show that memecaching has to be done after instantiating a memcache client object.

The source code for this application is available here and is self explanatory.

Questions, comments, doubts are welcome.

Advertisement

Google App Engine: Geo location identification and reverse geo-coding example

Today, I developed an application that asks users for their location (be it PC or cell phones) and then it displays the latitude and longitude (Geo-Location identification), it even looks up the address (Reverse Geocoding) from Google Maps API. I also store HTTP headers so that this information can be used to identify the device used (I’m working on it right now!)

Okay! This might not involve a lot of App Engine Code, just some basic code to store user location (datastore) and Users API from Google.

These things which are stored can be viewed in an Administrator Area which shows the last 100 peoples’ information.

But this has a lot of jQuery and JavaScript code in it! I know jQuery sounds fancy but just visit W3CSchools jQuery tutorial and finish it (it’ll not take you more than 5-10 minutes), you’ll understand (almost) every line of code.

What I’ve used:

  • Google App Engine (webapp2, Python 2.7)
  • jQuery
  • Google Maps V3 API

The code can be found here. It’s well commented and I expect you to know a bit of GAE (templates as well), jQuery is used a lot so a bit of that as well. Lookup Google Maps documentation if you don’t understand the code written for Google Maps.

Google App Engine: Sessions

With the advent of Google App Engine (Python 2.7) and WebApp2, there have been many changes in the way people code on Google App Engine. WebApp2 includes a Session Management script in the module ‘webapp2_extras

This is a simple sessions example with Google App Engine with Python 2.7 and WebApp2. The code is self explanatory. I haven’t implemented exceptions and errors as I won’t be using this snippet of code anymore. But figuring out something is the fun part isn’t it?

Session Module:

#Import sessions for session handling
import webapp2
from webapp2_extras import sessions

#This is needed to configure the session secret key
#Runs first in the whole application
myconfig_dict = {}
myconfig_dict['webapp2_extras.sessions'] = {
    'secret_key': 'my-super-secret-key-somemorearbitarythingstosay',
}

#Session Handling class, gets the store, dispatches the request
class BaseSessionHandler(webapp2.RequestHandler):
    def dispatch(self):
        # Get a session store for this request.
        self.session_store = sessions.get_store(request=self.request)

        try:
            # Dispatch the request.
            webapp2.RequestHandler.dispatch(self)
        finally:
            # Save all sessions.
            self.session_store.save_sessions(self.response)

    @webapp2.cached_property
    def session(self):
        # Returns a session using the default cookie key.
        return self.session_store.get_session()
#End of BaseSessionHandler Class

Main Module:

import webapp2
from webapp2_extras import sessions
import session_module

#MainHandler class where we write code for ourselves
class MainHandler(session_module.BaseSessionHandler):
 def get(self):

  if self.session.get('counter'):
   self.response.out.write('Session is in place')
   counter = self.session.get('counter')
   self.session['counter'] = counter + 1
   self.response.out.write('Counter = ' + str(self.session.get('counter')))
  else:
   self.response.out.write('Fresh Session')
   self.session['counter'] = 1
   self.response.out.write('Counter = ' + str(self.session.get('counter')))
#End of MainHandler Class

#The application starts running after this is interpreted
app = webapp2.WSGIApplication([('/', MainHandler),], config = session_module.myconfig_dict)