In this article I will explain how you can:
- Install and Configure Memcached on Mac OS X
- Use Memcached in your Java Application
I won’t go in too much detail about the benefits of using a distributed cache in your applications, but let’s at least provide some use cases for applications that are running in the context of an enterprise portal, eXo Platform in my case - surprising isn’t? And I will show this in another post.
We have many reasons to use a cache (distributed or not), in the context of enterprise portal, let’s take a look to some of these reasons:
- A portal is used to aggregate data in a single page. These data could come from different sources : Web Services, Database, ERP, ….. and accessing the data in real time could be costly. So it will be quite interesting to cache the result of the call when possible.
- If the portal is used to aggregate many data from many sources, it is sometime necessary to jump into another application to continue some operation. A distributed and shared cache could be used to manage some context between different applications running in different processes (JVM or even technologies) These are two example where a shared cache could be interesting for your portal based applications, we can find many other reason.
Installation and Configuration
Installing Memcached from sources
You can find some information about Memcached installation on the Memcached Wiki. The following steps are the steps that I have used on my environment.
As far as I know, Memached is not available as package for Mac OS X. I am still on Snow Leopard (10.6.8), and I have installed XCode and all development tools. I have use the article “Installing memcached 1.4.1 on Mac OS X 10.6 Snow Leopard” from wincent.com. For simplicity reason I have duplicate the content and updated to the latest releases.
1- Create a working directory :
2- Install libevent that is mandatory for memcached
1 2 3 4 5 6 7
3- Install memcached
Go back to your install directory (memcachedbuild)
1 2 3 4 5 6 7
You are now ready to use memcached that is available at
This allows you to avoid changing to the pre-installed memcached located in /usr/bin, if you want to replace it instead of having you own install, just run the configure command with the following parameter:
Starting and testing Memcached
Start the memcached server, using the following command line:
This command starts the memcached server as demon (-d parameter), on the TCP port 11211 (this is the default value). You can find more about the memcached command using
It is possible to connect and test your server using a telnet connection. Once connected you can set and get object in the cache, take a look to the following paragraph.
1 2 3 4 5 6 7 8 9 10 11
set command allows you to put a new value in the cache using the following syntax:
1 2 3
- key : the key used to store the data in the cache
- flags : a 32 bits unsigned integer that memcached stored with the data
- expiration_time : expiration time in seconds, if you put 0 this means no delay
- number_if_bytes : number of bytes in the data block
- noreply : option to tell the server to not return any value
- value : the value to store and associate to the key.
This is a short view of the documentation located in your source directory
get command allows you to access the value that is associated with the key.
You can check the version of memcahed you are running by calling the
stats command in your telnet session.
Your memcached server is up and running, you can now start to use it inside your applications.
Simple Java Application with Memcached
The easiest way to use memcached from your Java applications is to use a client library. You can find many client libraries. In this example I am using spymemcached developped by the people from Couchbase.
1- Adding SpyMemcached to your Maven project
Add the repository to you pom.xml (or you setting.xml)
1 2 3 4 5 6
then the dependency to your pom.xml
1 2 3 4 5
2- Use SpyMemcache client in your application
The following code is a simple Java class that allows you to enter the key and the value and set it in the cache.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
So when executing the application you will see something like :
1 2 3 4 5 6
You can also access the object from a Telnet session:
1 2 3 4
You can use any Java class in your application, the only thing to do is to make this class serializable.
This is it for the first post about memcached and Java, I am currently working on a small example integrating Web Services call, Portlets and memcached.