Using Memcache with Google App Engine (GAE/J)

A simple beginning — Fragman iPhone Application

Memcache provides convenience to store a frequent query in memory, so that you don’t have to make the same execution over and over again. For the iPhone application I build (Fragman), I build a GAE/J backend for managing movie names, IMDB URLs, thumb images, video files, etc. It interacts with the Obj-c code via calls encoded in JSON.

Since the categories are updated so frequently (new trailer, top 250 , soap opera trailers), every time the user starts the application from her iPhone, the corresponding categories Id(key) lists are  created which includes all the aspects mentioned above,

what I see, after people start to use the application that, most of the time, the application got stuck with the time constraint of GAE and this causes inconvenience, so I decided to hold the category lists’s JSON in memory. Following is the code ,

this is the servlet for creating the JSON which is called after say upcoming movie list is changed. This servlet updates the memcached list

        List keyList = new ArrayList();
	static Cache cache;
	static
	{
		try
		{
			CacheFactory cacheFactory =
CacheManager.getInstance().getCacheFactory();
			cache = cacheFactory.createCache(
Collections.emptyMap());
		}
		catch (CacheException e)
		{
			// ...
		}
		//.....
                public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
	        {
                       //................
                       if(cache.containsKey(mode))
		       {
			     cache.remove(mode);
		       }
		       cache.put(mode, keyList);
	        }
                //........
          }

And this is the servlet that fetch the Fragman beans from the datastore with the given keys (key list is got from the memecached list)

          keyList = (List)cache.get(mode);
By doing this, I reduce the time that the servlet that creates the JSON takes.

Post a Comment

Your email is never shared. Required fields are marked *

*
*