Database Reference
In-Depth Information
machines processing your full contact list to refer back to that memory pool instead of local
memory.
Tutorial Links
The spymemcached project has a handful of examples using its API available on its wiki .
Example Code
Let's say we need to keep track of which reviewers have already reviewed which movies, so
we don't ask a reviewer to review the same movie twice. Because there is no single, offi-
cially supported Java client for Memcached, we'll use the popular spymemcached client.
We'll start by defining a client and pointing it at our Memcached servers:
MemcachedClient client = new
new MemcachedClient (
AddrUtil . getAddresses ( "server1:11211 server2:11211" ));
Now we'll start loading data into our cache. We'll use the popular OpenCSV library to read
our reviews file and write an entry to our cache for every reviewer and title pair we find:
CSVReader reader = new
new CSVReader ( new
new FileReader ( "reviews.csv" ));
String [] line ;
while
while (( line = reader . readNext ()) != null
null ) {
//Merge the reviewer name and the movie title
//into a single value (ie: KevinDune)
//that we'll use as a key
String reviewerAndTitle = line [ 0 ] + line [ 1 ];
//Write the key to our cache and store it for 30 minutes
//(188 seconds)
client . set ( reviewerAndTitle , 1800 , true
true );
}
Once we have our values loaded into the cache, we can quickly check the cache from a
MapReduce job or any other Java code:
Object myObject = client . get ( aKey );
Search WWH ::




Custom Search