Java Reference
In-Depth Information
Memcache Service
App Engine provides a distributed in-memory data cache in front of the robust,
persistent storage that you can use for certain transient tasks. The Memcache API
supports the JCache interface. JCache is a draft standard that provides a map-like
interface to the cached data store. Through JCache you store objects of any type or
class in key/value pairs. This makes it very quick and simple to store and retrieve
session data, query results, and subsets of data that will be reused throughout the
If you're running the same set of data-store queries multiple times in the same
user's session, you should consider using the memory cache to speed the response
time of the application. For example, consider a web site where users are browsing
for a phonebook-type service in their area. If multiple users were all searching for
Denver, CO, querying the data store on each request would become extremely
inefficient. For queries with the same parameters, where the data is relatively static,
you can store the results in the memory cache and have your query check there first.
If the cache is expired or the results are no longer accessible, then the application can
query the data store and refresh the cache with the new results. You can configure
data to expire in two ways. You can provide an expiration time as a number of
seconds relative to when the value is added, or as an absolute Unix epoch time in the
future (for example, the number of seconds from midnight January 1, 1970).
No matter how you decide to approach expiration, there is one important design
aspect to consider when using Memcache in your application. The data is not
reliable, so make sure you store a copy of the data in the data store if the application
requires the data to function properly. Data is not reliable because App Engine can
expire the Memcache data at any time, even before the expiration deadline. That may
make you a bit nervous, but don't worry too much. Memcache will try to keep the
data for as long as possible. It will evict the data if the application is under pressure
for memory resources, if you've coded the application to explicitly remove it, or if
some sort of outage or restart has occurred. If you'd like to expire the data yourself,
you have the option either to expire it after an amount of time has passed since the
data has been set or at an absolute date and time. In all cases, your application
should not assume that the data in the cache will be available.
Let's take a look at a sample application that uses the Memcache API to store
data, retrieve data, and report usage statistics about the use of the cache. When you
created the application for this chapter, the Google Plugin for Eclipse created some
default files in the project. One of these is a servlet, which you'll be extending to
exercise the Memcache API. Open the servlet in the src/com.kyleroche.gaeservices
directory in your Eclipse project. Replace the default code with the code from
Listing 8-1.
Search WWH ::

Custom Search