Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Use Memcached for Java enterprise performance, Part 1: Architecture and setup

How is Memcached different from traditional Java caching?

  • Print
  • Feedback

Page 4 of 4

You can connect to your Memcached server in a variety of ways. If you're using a Java client, as we'll do in the second half of this tutorial, you'll be able to access a simple API for storing and getting objects from the cache. Alternately, you could use a Telnet client to connect to the server directly. Knowing how to use the Telnet client to communicate with the Memcached server is important for debugging the Java client, so we'll start there.

Telnet commands

First you'll need to use the Telnet client of your choice to connect to the Memcached server. On a Windows XP machine, you can simply execute telnet localhost 11211 assuming the Memcached server is running on the same machine and listening on the default 11211 port. The following commands are essential for working with Memcached via Telnet:

  • set adds a new item to the cache. The call is: Set <keyName> <flags> <expiryTime> <bytes>. You can type the actual value that should be stored on the next line. If you dont want the cache entry to expire then enter 0 as the value.
  • get returns the value of the cache key. Use get <keyName> to get the value of the keyName.
  • add adds a new key only if it does not already exist. For instance: add <keyName> <flags> <expiryTime> <bytes>
  • replace will replace a value only if the key exists. For instance: replace <keyName> <flags> <expiryTime> <bytes>
  • delete deletes the cache entry for the key. You can use the call delete <keyName> to delete value of the keyName.

The screenshot in Figure 4 represents a sample interaction with the Memcached server via Telnet. As you can see, the Memcached server provides feedback to each command, such as STORED, NOT_STORED, and so on.

Figure 4. Sample telnet client interaction with Memcached server (click to enlarge)

Conclusion to Part 1

So far we've briefly discussed the differences between Memcached's distributed architecture and more traditional Java cache systems. We've also set up a Memcached implementation in your development environment, and you've practiced connecting to Memcached via Telnet. In the next part of this tutorial we'll use the Java client spymemcached to set up a distributed caching solution for a sample Java application. In the process, you'll learn a lot more about Memcached and how it can improve the performance of your Java EE applications.

About the author

Sunil Patil is a Java EE Architect working for Avnet Technology in San Francisco, California. He is the author of Java Portlets 101 (SourceBeat, April 2007) and has written numerous articles published by JavaWorld, IBM developerWorks, and O'Reilly Media. In addition to being an IBM Certified WebSphere Portal Server Application Developer and Administer, he is a Sun Microsystems Certified Java Programmer, a Web component developer, and a business component developer. You can view Sunil's blog at http://www.webspherenotes.com.

Read more about Enterprise Java in JavaWorld's Enterprise Java section.

  • Print
  • Feedback

Resources